.Net下Winform程序让MessageBox.Show显示在父窗体中间

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


  1. 调用方式:
  1. DialogResult dr = MessageBoxEx.Show(this,"是否要删除此数据?""删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);  
  2.   
  3. if (dr == DialogResult.Cancel)  
  4. {  
  5.     return;  
  6. }  


转自:http://blog.csdn.net/aspnet2002web/article/details/28173289
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值