1. 程式人生 > >C#技巧【Winform程式讓MessageBox.Show顯示在父窗體中間】

C#技巧【Winform程式讓MessageBox.Show顯示在父窗體中間】

Winform程式讓MessageBox.Show顯示在父窗體中間

下面的寫法,預設是顯示在螢幕的中間。

  1. DialogResult dr = MessageBox.Show("是否要刪除此資料?""刪除確認", MessageBoxButtons.OKCancel,MessageBoxIcon.Information,MessageBoxDefaultButton.Button2);  
  2. if (dr == DialogResult.Cancel)  
  3. {  
  4.     return;  
  5. }  

要讓MessageBoxEx.Show顯示在父窗體中間,需要重新寫個下面的類,呼叫WINAPI函式。

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