C#:Win32 Hooks(二)让MessageBox自动显示在父窗体中心

前言:

在WinForm里,无论你是否指定owner,MessageBox总是显示在屏幕中心,而不是父窗体中心。要想让其显示在父窗体中心,你不得不借助强大的Win32 Hooks。

源代码:

MessageBoxPlus.cs

/* ---------------------------------------------------------- 文件名称:MessageBoxPlus.cs 作者:秦建辉 MSN:[email protected] QQ:36748897 博客:http://blog.csdn.net/jhqin 开发环境: Visual Studio V2010 .NET Framework 4 Client Profile 版本历史: V1.0 2012年05月07日 让MessageBox自动显示在父窗体中心 ------------------------------------------------------------ */ using System; using System.Windows.Forms; namespace Splash.Windows.Forms { public class MessageBoxPlus { public static DialogResult Show(IWin32Window owner, String text) { owner.CenterChild(); return MessageBox.Show(owner, text); } public static DialogResult Show(IWin32Window owner, String text, String caption) { owner.CenterChild(); return MessageBox.Show(owner, text, caption); } public static DialogResult Show(IWin32Window owner, String text, String caption, MessageBoxButtons buttons) { owner.CenterChild(); return MessageBox.Show(owner, text, caption, buttons); } public static DialogResult Show(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon) { owner.CenterChild(); return MessageBox.Show(owner, text, caption, buttons, icon); } public static DialogResult Show(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton) { owner.CenterChild(); return MessageBox.Show(owner, text, caption, buttons, icon, defButton); } public static DialogResult Show(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defButton, MessageBoxOptions options) { owner.CenterChild(); return MessageBox.Show(owner, text, caption, buttons, icon, defButton, options); } public static DialogResult Show(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, String helpFilePath) { owner.CenterChild(); return MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, options, helpFilePath); } public static DialogResult Show(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, String helpFilePath, String keyword) { owner.CenterChild(); return MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, options, helpFilePath, keyword); } public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, string helpFilePath, HelpNavigator navigator) { owner.CenterChild(); return MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, options, helpFilePath, navigator); } public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, string helpFilePath, HelpNavigator navigator, Object param) { owner.CenterChild(); return MessageBox.Show(owner, text, caption, buttons, icon, defaultButton, options, helpFilePath, navigator, param); } } }


你可能感兴趣的:(C#:Win32 Hooks(二)让MessageBox自动显示在父窗体中心)