BaseUtil.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using DevExpress.XtraEditors;
  2. using DevExpress.XtraEditors.Repository;
  3. using System;
  4. using System.Data;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using UAS_DeviceMonitor.DataOperate;
  9. using DevExpress.Utils;
  10. using UAS_DeviceMonitor.CustomerControl.ValueLabel;
  11. namespace UAS_DeviceMonitor.PublicMethod
  12. {
  13. class BaseUtil
  14. {
  15. [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
  16. public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
  17. public static void FillComBoxEditWidthDataTable(RepositoryItemComboBox combo, string TextField, string ValueField, DataTable dt)
  18. {
  19. combo.Items.Clear();
  20. for (int i = 0; i < dt.Rows.Count; i++)
  21. {
  22. ComboBoxData item = new ComboBoxData();
  23. item.Value = dt.Rows[i][ValueField].ToString();
  24. item.Text = dt.Rows[i][TextField].ToString();
  25. combo.Items.Add(item);
  26. }
  27. }
  28. public static void FillComBoxEditWidthDataTable(ComboBoxEdit combo, string TextField, string ValueField, DataTable dt)
  29. {
  30. combo.Properties.Items.Clear();
  31. for (int i = 0; i < dt.Rows.Count; i++)
  32. {
  33. ComboBoxData item = new ComboBoxData();
  34. item.Value = dt.Rows[i][ValueField].ToString();
  35. item.Text = dt.Rows[i][TextField].ToString();
  36. combo.Properties.Items.Add(item);
  37. }
  38. combo.SelectedIndex = 0;
  39. }
  40. public static void FillComBoxEditWidthDataTable(ComboBoxEdit combo, string TextField, string ValueField, DataTable dt, bool AddAll)
  41. {
  42. combo.Properties.Items.Clear();
  43. if (AddAll)
  44. {
  45. ComboBoxData item = new ComboBoxData();
  46. item.Value = "全部";
  47. item.Text = "全部";
  48. combo.Properties.Items.Add(item);
  49. }
  50. for (int i = 0; i < dt.Rows.Count; i++)
  51. {
  52. ComboBoxData item = new ComboBoxData();
  53. item.Value = dt.Rows[i][ValueField].ToString();
  54. item.Text = dt.Rows[i][TextField].ToString();
  55. combo.Properties.Items.Add(item);
  56. }
  57. combo.SelectedIndex = 0;
  58. }
  59. public static DataTable ToDataTable(DataRow[] rows)
  60. {
  61. if (rows == null || rows.Length == 0) return new DataTable();
  62. DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
  63. foreach (DataRow row in rows)
  64. tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
  65. return tmp;
  66. }
  67. public static string GetComboxEditValue(ComboBoxEdit ComBox)
  68. {
  69. if (ComBox.SelectedItem == null)
  70. return ComBox.Text;
  71. else
  72. return (ComBox.SelectedItem as ComboBoxData).Value;
  73. }
  74. /// <summary>
  75. /// 获取LRC
  76. /// </summary>
  77. /// <param name="SQL"></param>
  78. public static string getLRC(string SENDMESSAGE)
  79. {
  80. string message = SENDMESSAGE.Trim();
  81. if (message.Length % 2 != 0)
  82. {
  83. message = message + "0";
  84. }
  85. int LRC = 0x0;
  86. for (int i = 0; i < message.Length; i = i + 2)
  87. {
  88. int inside = int.Parse(message.Substring(i, 1).ToString() + message.Substring(i + 1, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  89. LRC += inside;
  90. }
  91. string _LRC = string.Format("{0:X2}", LRC);
  92. string LRCre = "";
  93. for (int i = 0; i < _LRC.Length; i++)
  94. {
  95. int index;
  96. index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  97. LRCre += string.Format("{0:X}", index);
  98. }
  99. LRCre = string.Format("{0:X}", int.Parse(LRCre, System.Globalization.NumberStyles.HexNumber) + 1);
  100. return LRCre;
  101. }
  102. public static string ByteToHexadecimalString(byte[] b, int length)
  103. {
  104. string returnStr = "";
  105. if (b != null)
  106. {
  107. for (int i = 0; i < length; i++)
  108. {
  109. returnStr += Convert.ToString(b[i], 16);//ToString("X2") 为C#中的字符串格式控制符
  110. }
  111. }
  112. return returnStr;
  113. }
  114. public static void CleanMemory()
  115. {
  116. GC.Collect();
  117. GC.WaitForPendingFinalizers();
  118. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  119. {
  120. SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
  121. }
  122. }
  123. private static ToolTipControllerShowEventArgs args;
  124. public static ToolTipController MyToolTipClt { get; private set; }
  125. public static void NewToolTip(Control ctl, string title, string content, int showTime, ToolTipType toolTipType, ToolTipLocation tipLocation, bool isAutoHide, ToolTipIconType tipIconType, ImageList imgList, int imgIndex)
  126. {
  127. try
  128. {
  129. MyToolTipClt = new ToolTipController();
  130. args = MyToolTipClt.CreateShowArgs();
  131. content = (string.IsNullOrEmpty(content) ? "???" : content);
  132. title = string.IsNullOrEmpty(title) ? "温馨提示" : title;
  133. MyToolTipClt.ImageList = imgList;
  134. MyToolTipClt.ImageIndex = (imgList == null ? 0 : imgIndex);
  135. args.AutoHide = isAutoHide;
  136. MyToolTipClt.ShowBeak = true;
  137. MyToolTipClt.ShowShadow = true;
  138. MyToolTipClt.Rounded = true;
  139. MyToolTipClt.AutoPopDelay = (showTime == 0 ? 2000 : showTime);
  140. MyToolTipClt.SetToolTip(ctl, content);
  141. MyToolTipClt.SetTitle(ctl, title);
  142. MyToolTipClt.SetToolTipIconType(ctl, tipIconType);
  143. MyToolTipClt.Active = true;
  144. MyToolTipClt.HideHint();
  145. MyToolTipClt.ShowHint(content, title, ctl, tipLocation);
  146. }
  147. catch (Exception)
  148. {
  149. }
  150. }
  151. /// <summary>
  152. /// 传入控件的集合和DataTable,通过判断控件的名称和数据源的列的描述来匹配,支持单层的GroupBox和Panel
  153. /// </summary>
  154. /// <param name="collection"></param>
  155. /// <param name="dt"></param>
  156. public static void SetFormValue(Control.ControlCollection collection, DataTable dt)
  157. {
  158. //DataTable存在数据才进行赋值操作
  159. if (dt.Rows.Count > 0)
  160. {
  161. for (int i = 0; i < collection.Count; i++)
  162. {
  163. //如果含有子控件则进行递归调用
  164. if (collection[i].Controls.Count > 0)
  165. SetFormValue(collection[i].Controls, dt);
  166. string controlName = collection[i].Name;
  167. string controlsTag = collection[i].Tag == null ? "" : collection[i].Tag.ToString();
  168. //默认给TextBox和Label赋值
  169. if (collection[i] is TextBox || collection[i] is ValueLabel || collection[i] is NumericUpDown)
  170. {
  171. for (int j = 0; j < dt.Columns.Count; j++)
  172. {
  173. if (controlName.ToUpper() == dt.Columns[j].Caption.ToUpper() || controlsTag.ToUpper() == dt.Columns[j].Caption.ToUpper())
  174. {
  175. //字段含有Status内容的才进行转换
  176. if (controlName.ToUpper().Contains("STATUS") || controlsTag.ToUpper().Contains("STATUS"))
  177. {
  178. //对审批状态进行判断
  179. switch (dt.Rows[0][j].ToString().ToUpper())
  180. {
  181. case "ENTERING":
  182. collection[i].Text = "在录入";
  183. break;
  184. case "UNAPPROVED":
  185. collection[i].Text = "未批准";
  186. break;
  187. case "COMMITED":
  188. collection[i].Text = "已提交";
  189. break;
  190. case "APPROVE":
  191. collection[i].Text = "已批准";
  192. break;
  193. case "AUDITED":
  194. collection[i].Text = "已审核";
  195. break;
  196. case "STARTED":
  197. collection[i].Text = "已下放";
  198. break;
  199. case "UNCHECK":
  200. collection[i].Text = "待检验";
  201. break;
  202. case "CHECKING":
  203. collection[i].Text = "检验中";
  204. break;
  205. default:
  206. collection[i].Text = dt.Rows[0][j].ToString();
  207. break;
  208. }
  209. }
  210. else
  211. collection[i].Text = dt.Rows[0][j].ToString();
  212. }
  213. }
  214. }
  215. //对封装在GroupBox的和Panel的控件进行批量赋值
  216. if (collection[i] is GroupControl || collection[i] is Panel)
  217. {
  218. for (int j = 0; j < collection[i].Controls.Count; j++)
  219. {
  220. controlName = collection[i].Controls[j].Name;
  221. if (collection[i].Controls[j] is TextBox || collection[i].Controls[j] is ValueLabel)
  222. {
  223. for (int k = 0; k < dt.Columns.Count; k++)
  224. {
  225. if (controlName.ToUpper() == dt.Columns[k].Caption.ToUpper())
  226. {
  227. collection[i].Controls[j].Text = dt.Rows[0][k].ToString();
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. //如果没有记录的话,将dt中含有的列的对应值设置为空
  236. else
  237. {
  238. for (int i = 0; i < collection.Count; i++)
  239. {
  240. if (collection[i] is TextBox || collection[i] is ValueLabel)
  241. {
  242. for (int j = 0; j < dt.Columns.Count; j++)
  243. {
  244. if (collection[i].Name.ToUpper() == dt.Columns[j].Caption.ToUpper())
  245. {
  246. collection[i].Text = "";
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }