BaseUtil.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 DataTable ToDataTable(DataRow[] rows)
  40. {
  41. if (rows == null || rows.Length == 0) return new DataTable();
  42. DataTable tmp = rows[0].Table.Clone(); // 复制DataRow的表结构
  43. foreach (DataRow row in rows)
  44. tmp.Rows.Add(row.ItemArray); // 将DataRow添加到DataTable中
  45. return tmp;
  46. }
  47. public static string GetComboxEditValue(ComboBoxEdit ComBox)
  48. {
  49. if (ComBox.SelectedItem == null)
  50. return ComBox.Text;
  51. else
  52. return (ComBox.SelectedItem as ComboBoxData).Value;
  53. }
  54. /// <summary>
  55. /// 获取LRC
  56. /// </summary>
  57. /// <param name="SQL"></param>
  58. public static string getLRC(string SENDMESSAGE)
  59. {
  60. string message = SENDMESSAGE.Trim();
  61. if (message.Length % 2 != 0)
  62. {
  63. message = message + "0";
  64. }
  65. int LRC = 0x0;
  66. for (int i = 0; i < message.Length; i = i + 2)
  67. {
  68. int inside = int.Parse(message.Substring(i, 1).ToString() + message.Substring(i + 1, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  69. LRC += inside;
  70. }
  71. string _LRC = string.Format("{0:X2}", LRC);
  72. string LRCre = "";
  73. for (int i = 0; i < _LRC.Length; i++)
  74. {
  75. int index;
  76. index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
  77. LRCre += string.Format("{0:X}", index);
  78. }
  79. LRCre = string.Format("{0:X}", int.Parse(LRCre, System.Globalization.NumberStyles.HexNumber) + 1);
  80. return LRCre;
  81. }
  82. public static string ByteToHexadecimalString(byte[] b, int length)
  83. {
  84. string returnStr = "";
  85. if (b != null)
  86. {
  87. for (int i = 0; i < length; i++)
  88. {
  89. returnStr += Convert.ToString(b[i], 16);//ToString("X2") 为C#中的字符串格式控制符
  90. }
  91. }
  92. return returnStr;
  93. }
  94. public static void CleanMemory()
  95. {
  96. GC.Collect();
  97. GC.WaitForPendingFinalizers();
  98. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  99. {
  100. SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
  101. }
  102. }
  103. private static ToolTipControllerShowEventArgs args;
  104. public static ToolTipController MyToolTipClt { get; private set; }
  105. public static void NewToolTip(Control ctl, string title, string content, int showTime, ToolTipType toolTipType, ToolTipLocation tipLocation, bool isAutoHide, ToolTipIconType tipIconType, ImageList imgList, int imgIndex)
  106. {
  107. try
  108. {
  109. MyToolTipClt = new ToolTipController();
  110. args = MyToolTipClt.CreateShowArgs();
  111. content = (string.IsNullOrEmpty(content) ? "???" : content);
  112. title = string.IsNullOrEmpty(title) ? "温馨提示" : title;
  113. MyToolTipClt.ImageList = imgList;
  114. MyToolTipClt.ImageIndex = (imgList == null ? 0 : imgIndex);
  115. args.AutoHide = isAutoHide;
  116. MyToolTipClt.ShowBeak = true;
  117. MyToolTipClt.ShowShadow = true;
  118. MyToolTipClt.Rounded = true;
  119. MyToolTipClt.AutoPopDelay = (showTime == 0 ? 2000 : showTime);
  120. MyToolTipClt.SetToolTip(ctl, content);
  121. MyToolTipClt.SetTitle(ctl, title);
  122. MyToolTipClt.SetToolTipIconType(ctl, tipIconType);
  123. MyToolTipClt.Active = true;
  124. MyToolTipClt.HideHint();
  125. MyToolTipClt.ShowHint(content, title, ctl, tipLocation);
  126. }
  127. catch (Exception)
  128. {
  129. }
  130. }
  131. }
  132. }