show.cs 7.1 KB

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