BaseUtil.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. namespace UAS_DeviceMonitor.PublicMethod
  11. {
  12. class BaseUtil
  13. {
  14. [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
  15. public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
  16. public static void FillComBoxEditWidthDataTable(RepositoryItemComboBox combo, string TextField, string ValueField, DataTable dt)
  17. {
  18. combo.Items.Clear();
  19. for (int i = 0; i < dt.Rows.Count; i++)
  20. {
  21. ComboBoxData item = new ComboBoxData();
  22. item.Value = dt.Rows[i][ValueField].ToString();
  23. item.Text = dt.Rows[i][TextField].ToString();
  24. combo.Items.Add(item);
  25. }
  26. }
  27. public static void FillComBoxEditWidthDataTable(ComboBoxEdit combo, string TextField, string ValueField, DataTable dt)
  28. {
  29. combo.Properties.Items.Clear();
  30. for (int i = 0; i < dt.Rows.Count; i++)
  31. {
  32. ComboBoxData item = new ComboBoxData();
  33. item.Value = dt.Rows[i][ValueField].ToString();
  34. item.Text = dt.Rows[i][TextField].ToString();
  35. combo.Properties.Items.Add(item);
  36. }
  37. combo.SelectedIndex = 0;
  38. }
  39. public static void FillComBoxEditWidthDataTable(ComboBoxEdit combo, string TextField, string ValueField, DataTable dt, bool AddAll)
  40. {
  41. combo.Properties.Items.Clear();
  42. if (AddAll)
  43. {
  44. ComboBoxData item = new ComboBoxData();
  45. item.Value = "全部";
  46. item.Text = "全部";
  47. combo.Properties.Items.Add(item);
  48. }
  49. for (int i = 0; i < dt.Rows.Count; i++)
  50. {
  51. ComboBoxData item = new ComboBoxData();
  52. item.Value = dt.Rows[i][ValueField].ToString();
  53. item.Text = dt.Rows[i][TextField].ToString();
  54. combo.Properties.Items.Add(item);
  55. }
  56. combo.SelectedIndex = 0;
  57. }
  58. public static DataTable ToDataTable(DataRow[] rows)
  59. {
  60. if (rows == null || rows.Length == 0) return new DataTable();
  61. DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
  62. foreach (DataRow row in rows)
  63. tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
  64. return tmp;
  65. }
  66. public static string GetComboxEditValue(ComboBoxEdit ComBox)
  67. {
  68. if (ComBox.SelectedItem == null)
  69. return ComBox.Text;
  70. else
  71. return (ComBox.SelectedItem as ComboBoxData).Value;
  72. }
  73. /// <summary>
  74. /// 获取LRC
  75. /// </summary>
  76. /// <param name="SQL"></param>
  77. public static string getLRC(string SENDMESSAGE)
  78. {
  79. string message = SENDMESSAGE.Trim();
  80. if (message.Length % 2 != 0)
  81. {
  82. message = message + "0";
  83. }
  84. int LRC = 0x0;
  85. for (int i = 0; i < message.Length; i = i + 2)
  86. {
  87. int inside = int.Parse(message.Substring(i, 1).ToString() + message.Substring(i + 1, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  88. LRC += inside;
  89. }
  90. string _LRC = string.Format("{0:X2}", LRC);
  91. string LRCre = "";
  92. for (int i = 0; i < _LRC.Length; i++)
  93. {
  94. int index;
  95. index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  96. LRCre += string.Format("{0:X}", index);
  97. }
  98. LRCre = string.Format("{0:X}", int.Parse(LRCre, System.Globalization.NumberStyles.HexNumber) + 1);
  99. return LRCre;
  100. }
  101. public static string ByteToHexadecimalString(byte[] b, int length)
  102. {
  103. string returnStr = "";
  104. if (b != null)
  105. {
  106. for (int i = 0; i < length; i++)
  107. {
  108. returnStr += Convert.ToString(b[i], 16);//ToString("X2") 为C#中的字符串格式控制符
  109. }
  110. }
  111. return returnStr;
  112. }
  113. public static void CleanMemory()
  114. {
  115. GC.Collect();
  116. GC.WaitForPendingFinalizers();
  117. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  118. {
  119. SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
  120. }
  121. }
  122. private static ToolTipControllerShowEventArgs args;
  123. public static ToolTipController MyToolTipClt { get; private set; }
  124. public static void NewToolTip(Control ctl, string title, string content, int showTime, ToolTipType toolTipType, ToolTipLocation tipLocation, bool isAutoHide, ToolTipIconType tipIconType, ImageList imgList, int imgIndex)
  125. {
  126. try
  127. {
  128. MyToolTipClt = new ToolTipController();
  129. args = MyToolTipClt.CreateShowArgs();
  130. content = (string.IsNullOrEmpty(content) ? "???" : content);
  131. title = string.IsNullOrEmpty(title) ? "温馨提示" : title;
  132. MyToolTipClt.ImageList = imgList;
  133. MyToolTipClt.ImageIndex = (imgList == null ? 0 : imgIndex);
  134. args.AutoHide = isAutoHide;
  135. MyToolTipClt.ShowBeak = true;
  136. MyToolTipClt.ShowShadow = true;
  137. MyToolTipClt.Rounded = true;
  138. MyToolTipClt.AutoPopDelay = (showTime == 0 ? 2000 : showTime);
  139. MyToolTipClt.SetToolTip(ctl, content);
  140. MyToolTipClt.SetTitle(ctl, title);
  141. MyToolTipClt.SetToolTipIconType(ctl, tipIconType);
  142. MyToolTipClt.Active = true;
  143. MyToolTipClt.HideHint();
  144. MyToolTipClt.ShowHint(content, title, ctl, tipLocation);
  145. }
  146. catch (Exception)
  147. {
  148. }
  149. }
  150. }
  151. }