Comm.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Speech.Synthesis;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace Core.StlMes.Client.LgResMgt.Mcms
  9. {
  10. public static class Comm
  11. {
  12. public static void Speak(this string msg)
  13. {
  14. SpeechSynthesizer voice = new SpeechSynthesizer
  15. {
  16. Rate = 1,
  17. Volume = 100
  18. };
  19. voice.SpeakCompleted += (sender, args) => voice.Dispose();
  20. //设置语速,[-10,10]
  21. //设置音量,[0,100]
  22. voice.SpeakAsync("1 " + msg);
  23. }
  24. /// <summary>
  25. /// 安全刷新界面控件 当为子线程时委托刷新界面
  26. /// </summary>
  27. /// <param name="control"></param>
  28. /// <param name="action"></param>
  29. public static void SafeRefreshControl(this System.Windows.Forms.Control control, Action action)
  30. {
  31. if (control.InvokeRequired)
  32. control.Invoke(action);
  33. else
  34. action();
  35. }
  36. /// <summary>
  37. /// 刷新Grid数据并根据数据调整Grid列宽
  38. /// </summary>
  39. /// <param name="ultraGrid">需要处理的Grid</param>
  40. public static void RefreshAndAutoSize(Infragistics.Win.UltraWinGrid.UltraGrid ultraGrid, string[] unSizeColumns = null)
  41. {
  42. try
  43. {
  44. ultraGrid.DataBind();
  45. ultraGrid.UpdateData();
  46. ultraGrid.Refresh();
  47. foreach (Infragistics.Win.UltraWinGrid.UltraGridBand band in ultraGrid.DisplayLayout.Bands)
  48. {
  49. foreach (Infragistics.Win.UltraWinGrid.UltraGridColumn column in band.Columns)
  50. {
  51. if (column.Hidden) continue;
  52. if (unSizeColumns == null || !unSizeColumns.Contains(column.Key))
  53. column.PerformAutoResize(Infragistics.Win.UltraWinGrid.PerformAutoSizeType.AllRowsInBand, true);
  54. }
  55. }
  56. ultraGrid.Refresh();
  57. }
  58. catch { }
  59. }
  60. public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, string[] buttonTitles)
  61. {
  62. MessageForm frm = new MessageForm(buttons, buttonTitles);
  63. frm.Show();
  64. frm.WatchForActivate = true;
  65. DialogResult result = MessageBox.Show(frm, text, caption, buttons);
  66. frm.Close();
  67. return result;
  68. }
  69. public static DialogResult Show(string text, string caption, MessageBoxButtons buttons,
  70. MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, string[] buttonTitles)
  71. {
  72. MessageForm frm = new MessageForm(buttons, buttonTitles);
  73. frm.Show();
  74. frm.WatchForActivate = true;
  75. DialogResult result = MessageBox.Show(frm, text, caption, buttons, icon, defaultButton);
  76. frm.Close();
  77. return result;
  78. }
  79. private class MessageForm : Form
  80. {
  81. IntPtr _handle;
  82. MessageBoxButtons _buttons;
  83. string[] _buttonTitles = null;
  84. bool _watchForActivate = false;
  85. public bool WatchForActivate
  86. {
  87. get { return _watchForActivate; }
  88. set { _watchForActivate = value; }
  89. }
  90. public MessageForm(MessageBoxButtons buttons, string[] buttonTitles)
  91. {
  92. _buttons = buttons;
  93. _buttonTitles = buttonTitles;
  94. // Hide self form, and don't show self form in task bar.
  95. this.Text = "";
  96. this.StartPosition = FormStartPosition.CenterScreen;
  97. this.Location = new Point(-32000, -32000);
  98. this.ShowInTaskbar = false;
  99. }
  100. protected override void OnShown(EventArgs e)
  101. {
  102. base.OnShown(e);
  103. // Hide self form, don't show self form even in task list.
  104. NativeWin32API.SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, 0, 0, 659);
  105. }
  106. protected override void WndProc(ref System.Windows.Forms.Message m)
  107. {
  108. if (_watchForActivate && m.Msg == 0x0006)
  109. {
  110. _watchForActivate = false;
  111. _handle = m.LParam;
  112. CheckMsgbox();
  113. }
  114. base.WndProc(ref m);
  115. }
  116. private void CheckMsgbox()
  117. {
  118. if (_buttonTitles == null || _buttonTitles.Length == 0)
  119. return;
  120. // Button title index
  121. int buttonTitleIndex = 0;
  122. // Get the handle of control in current window.
  123. IntPtr h = NativeWin32API.GetWindow(_handle, GW_CHILD);
  124. // Set those custom titles to the three buttons(Default title are: Yes, No and Cancle).
  125. while (h != IntPtr.Zero)
  126. {
  127. if (NativeWin32API.GetWindowClassName(h).Equals("Button"))
  128. {
  129. if (_buttonTitles.Length > buttonTitleIndex)
  130. {
  131. // Changes the text of the specified window's title bar (if it has one).
  132. // If the specified window is a control, the text of the control is changed.
  133. // However, SetWindowText cannot change the text of a control in another application.
  134. NativeWin32API.SetWindowText(h, _buttonTitles[buttonTitleIndex]);
  135. buttonTitleIndex++;
  136. }
  137. }
  138. // Get the handle of next control in current window.
  139. h = NativeWin32API.GetWindow(h, GW_HWNDNEXT);
  140. }
  141. }
  142. }
  143. public const int GW_CHILD = 5;
  144. public const int GW_HWNDNEXT = 2;
  145. public class NativeWin32API
  146. {
  147. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  148. public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, int flags);
  149. [DllImport("user32.dll")]
  150. public static extern IntPtr GetWindow(IntPtr hWnd, Int32 wCmd);
  151. [DllImport("user32.dll")]
  152. public static extern bool SetWindowText(IntPtr hWnd, string lpString);
  153. [DllImport("user32.dll")]
  154. public static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
  155. public static string GetWindowClassName(IntPtr handle)
  156. {
  157. StringBuilder sb = new StringBuilder(256);
  158. // Retrieves the name of the class to which the specified window belongs
  159. GetClassNameW(handle, sb, sb.Capacity);
  160. return sb.ToString();
  161. }
  162. }
  163. }
  164. }