MessageBoxEx.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.Runtime.InteropServices;
  6. namespace 优软MES.PublicMethod {
  7. public class MessageBoxEx
  8. {
  9. private static IWin32Window _owner;
  10. private static HookProc _hookProc;
  11. private static IntPtr _hHook;
  12. public static DialogResult Show(string text)
  13. {
  14. Initialize();
  15. return MessageBox.Show(text);
  16. }
  17. public static DialogResult Show(string text, string caption)
  18. {
  19. Initialize();
  20. return MessageBox.Show(text, caption);
  21. }
  22. public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
  23. {
  24. Initialize();
  25. return MessageBox.Show(text, caption, buttons);
  26. }
  27. public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
  28. {
  29. Initialize();
  30. return MessageBox.Show(text, caption, buttons, icon);
  31. }
  32. public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton)
  33. {
  34. Initialize();
  35. return MessageBox.Show(text, caption, buttons, icon, defButton);
  36. }
  37. public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton, MessageBoxOptions options)
  38. {
  39. Initialize();
  40. return MessageBox.Show(text, caption, buttons, icon, defButton, options);
  41. }
  42. public static DialogResult Show(IWin32Window owner, string text)
  43. {
  44. _owner = owner;
  45. Initialize();
  46. return MessageBox.Show(owner, text);
  47. }
  48. public static DialogResult Show(IWin32Window owner, string text, string caption)
  49. {
  50. _owner = owner;
  51. Initialize();
  52. return MessageBox.Show(owner, text, caption);
  53. }
  54. public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
  55. {
  56. _owner = owner;
  57. Initialize();
  58. return MessageBox.Show(owner, text, caption, buttons);
  59. }
  60. public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
  61. {
  62. _owner = owner;
  63. Initialize();
  64. return MessageBox.Show(owner, text, caption, buttons, icon);
  65. }
  66. public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton)
  67. {
  68. _owner = owner;
  69. Initialize();
  70. return MessageBox.Show(owner, text, caption, buttons, icon, defButton);
  71. }
  72. public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton, MessageBoxOptions options)
  73. {
  74. _owner = owner;
  75. Initialize();
  76. return MessageBox.Show(owner, text, caption, buttons, icon,
  77. defButton, options);
  78. }
  79. public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
  80. public delegate void TimerProc(IntPtr hWnd, uint uMsg, UIntPtr nIDEvent, uint dwTime);
  81. public const int WH_CALLWNDPROCRET = 12;
  82. public enum CbtHookAction : int
  83. {
  84. HCBT_MOVESIZE = 0,
  85. HCBT_MINMAX = 1,
  86. HCBT_QS = 2,
  87. HCBT_CREATEWND = 3,
  88. HCBT_DESTROYWND = 4,
  89. HCBT_ACTIVATE = 5,
  90. HCBT_CLICKSKIPPED = 6,
  91. HCBT_KEYSKIPPED = 7,
  92. HCBT_SYSCOMMAND = 8,
  93. HCBT_SETFOCUS = 9
  94. }
  95. [DllImport("user32.dll")]
  96. private static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle lpRect);
  97. [DllImport("user32.dll")]
  98. private static extern int MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  99. [DllImport("User32.dll")]
  100. public static extern UIntPtr SetTimer(IntPtr hWnd, UIntPtr nIDEvent, uint uElapse, TimerProc lpTimerFunc);
  101. [DllImport("User32.dll")]
  102. public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
  103. [DllImport("user32.dll")]
  104. public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
  105. [DllImport("user32.dll")]
  106. public static extern int UnhookWindowsHookEx(IntPtr idHook);
  107. [DllImport("user32.dll")]
  108. public static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);
  109. [DllImport("user32.dll")]
  110. public static extern int GetWindowTextLength(IntPtr hWnd);
  111. [DllImport("user32.dll")]
  112. public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int maxLength);
  113. [DllImport("user32.dll")]
  114. public static extern int EndDialog(IntPtr hDlg, IntPtr nResult);
  115. [StructLayout(LayoutKind.Sequential)]
  116. public struct CWPRETSTRUCT
  117. {
  118. public IntPtr lResult;
  119. public IntPtr lParam;
  120. public IntPtr wParam;
  121. public uint message;
  122. public IntPtr hwnd;
  123. };
  124. static MessageBoxEx()
  125. {
  126. _hookProc = new HookProc(MessageBoxHookProc);
  127. _hHook = IntPtr.Zero;
  128. }
  129. private static void Initialize()
  130. {
  131. if (_hHook != IntPtr.Zero)
  132. {
  133. throw new NotSupportedException("multiple calls are not supported");
  134. }
  135. if (_owner != null)
  136. {
  137. _hHook = SetWindowsHookEx(WH_CALLWNDPROCRET, _hookProc, IntPtr.Zero, AppDomain.GetCurrentThreadId());
  138. }
  139. }
  140. private static IntPtr MessageBoxHookProc(int nCode, IntPtr wParam, IntPtr lParam)
  141. {
  142. if (nCode < 0)
  143. {
  144. return CallNextHookEx(_hHook, nCode, wParam, lParam);
  145. }
  146. CWPRETSTRUCT msg = (CWPRETSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPRETSTRUCT));
  147. IntPtr hook = _hHook;
  148. if (msg.message == (int)CbtHookAction.HCBT_ACTIVATE)
  149. {
  150. try
  151. {
  152. CenterWindow(msg.hwnd);
  153. }
  154. finally
  155. {
  156. UnhookWindowsHookEx(_hHook);
  157. _hHook = IntPtr.Zero;
  158. }
  159. }
  160. return CallNextHookEx(hook, nCode, wParam, lParam);
  161. }
  162. private static void CenterWindow(IntPtr hChildWnd)
  163. {
  164. Rectangle recChild = new Rectangle(0, 0, 0, 0);
  165. bool success = GetWindowRect(hChildWnd, ref recChild);
  166. int width = recChild.Width - recChild.X;
  167. int height = recChild.Height - recChild.Y;
  168. Rectangle recParent = new Rectangle(0, 0, 0, 0);
  169. success = GetWindowRect(_owner.Handle, ref recParent);
  170. Point ptCenter = new Point(0, 0);
  171. ptCenter.X = recParent.X + ((recParent.Width - recParent.X) / 2);
  172. ptCenter.Y = recParent.Y + ((recParent.Height - recParent.Y) / 2);
  173. Point ptStart = new Point(0, 0);
  174. ptStart.X = (ptCenter.X - (width / 2));
  175. ptStart.Y = (ptCenter.Y - (height / 2));
  176. ptStart.X = (ptStart.X < 0) ? 0 : ptStart.X;
  177. ptStart.Y = (ptStart.Y < 0) ? 0 : ptStart.Y;
  178. int result = MoveWindow(hChildWnd, ptStart.X, ptStart.Y, width,
  179. height, false);
  180. }
  181. }
  182. }