新凡

  博客园 :: 首页 :: :: 联系 :: 订阅 :: 管理 ::

MessageBoxEx

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.Runtime.InteropServices;
  5 using System.Windows.Forms;
  6 using System.Drawing;
  7 
  8 namespace JToolDemo
  9 {
 10     struct RECT
 11     {
 12         public int left;
 13         public int top;
 14         public int right;
 15         public int bottom;
 16     };
 17     //实现MessageBox居中owner窗体显示 
 18     class MessageBoxEx
 19     {
 20         public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
 21         [DllImport("user32.dll")]
 22         private static extern IntPtr SetWindowsHookEx(int hookid,
 23             HookProc pfnhook, IntPtr hinst, int threadid);
 24         [DllImport("user32.dll")]
 25         private static extern IntPtr CallNextHookEx(IntPtr hhook,
 26             int code, IntPtr wparam, IntPtr lparam);
 27         [DllImport("kernel32.dll")]
 28         private static extern IntPtr GetModuleHandle(string modName);
 29         [DllImport("user32.dll")]
 30         private static extern bool UnhookWindowsHookEx(IntPtr hhook);
 31         [DllImport("user32.dll")]
 32         private static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
 33         [DllImport("user32.dll")]
 34         private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
 35         [DllImport("user32.dll")]
 36         private static extern bool MoveWindow(
 37             IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
 38         private const int WH_CBT = 5;
 39         private const int HCBT_ACTIVATE = 5;
 40         private const int GW_OWNER = 4;
 41         private static IntPtr hookHandle = IntPtr.Zero;
 42         private static RECT GetOwnerRect(IntPtr hwnd)
 43         {
 44             RECT ownerRect = new RECT();
 45             IntPtr ownerHwnd = GetWindow(hwnd, GW_OWNER);
 46             GetWindowRect(ownerHwnd, ref ownerRect);
 47             return ownerRect;
 48         }
 49         private static IntPtr CBTHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
 50         {
 51             switch (nCode)
 52             {
 53                 case HCBT_ACTIVATE:
 54                     RECT vRectangle = new RECT();
 55                     RECT ownerRect = GetOwnerRect(wParam);
 56                     GetWindowRect(wParam, ref vRectangle);
 57                     int width = vRectangle.right - vRectangle.left;
 58                     int height = vRectangle.bottom - vRectangle.top;
 59                     int ownerWidth = ownerRect.right - ownerRect.left;
 60                     int ownerHeight = ownerRect.bottom - ownerRect.top;
 61                     int left = Math.Max(ownerRect.left + (ownerWidth - width) / 2, 0);
 62                     int top = Math.Max(ownerRect.top + (ownerHeight - height) / 2, 0);
 63                     MoveWindow(wParam,
 64                         left,
 65                         top,
 66                         width, height, false);
 67                     UnhookWindowsHookEx(hookHandle);
 68                     break;
 69             }
 70             return CallNextHookEx(hookHandle, nCode, wParam, lParam);
 71         }
 72         private static void Lock()
 73         {
 74             hookHandle = SetWindowsHookEx(WH_CBT, new HookProc(CBTHookCallback),
 75                 GetModuleHandle(null), 0);
 76         }
 77         //根据需要重载 
 78         public static DialogResult Show(string text)
 79         {
 80             Lock();
 81             return MessageBox.Show(text);
 82         }
 83         public static DialogResult Show(IWin32Window owner, string text)
 84         {
 85             Lock();
 86             return MessageBox.Show(owner, text);
 87         }
 88         public static DialogResult Show(string text, string caption)
 89         {
 90             Lock();
 91             return MessageBox.Show(text, caption);
 92         }
 93         public static DialogResult Show(IWin32Window owner, string text, string caption)
 94         {
 95             Lock();
 96             return MessageBox.Show(owner, text, caption);
 97         }
 98         public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
 99         {
100             Lock();
101             return MessageBox.Show(text, caption, buttons);
102         }
103         public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
104         {
105             Lock();
106             return MessageBox.Show(owner, text, caption, buttons);
107         }
108         public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
109         {
110             Lock();
111             return MessageBox.Show(text, caption, buttons, icon);
112         }
113         public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
114         {
115             Lock();
116             return MessageBox.Show(owner, text, caption, buttons, icon);
117         }
118         public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
119         {
120             Lock();
121             return MessageBox.Show(text, caption, buttons, icon, defaultButton);
122         }
123     }
124 }
125 
126    

 

posted on 2013-04-23 22:33  新凡  阅读(462)  评论(0)    收藏  举报