C# messagebox 居中父窗体

Posted njflash

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# messagebox 居中父窗体相关的知识,希望对你有一定的参考价值。

在右边项目上点击右键->Add->class,添加MessageBoxEx类:

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

调用办法

MessageBoxEx.Show(this, "Please fix the validation errors before saving.", "Validation Errors");

 

以上是关于C# messagebox 居中父窗体的主要内容,如果未能解决你的问题,请参考以下文章

C# 窗体中如何居中label标签??有没有属性可以设置?

C# 对Panel设置Mouseclick事件 4RadioButton触发跳出Messagebox

C++:如何将 MessageBox 居中?

C# 线程中怎么调用有方法的窗体 窗体一闪就没有了 为啥线程中调用MessageBox.Show("");没事 紧急

C#中子窗体操作父窗体的变量

C#里如何点击“取消(button2)”按钮后关闭窗体2,并返回窗体1?