前言:
在WinForm裡,無論你是否指定owner,MessageBox總是顯示在屏幕中心,而不是父窗體中心。要想讓其顯示在父窗體中心,你不得不借助強大的Win32 Hooks。
源代碼:
MessageBoxPlus.cs
[csharp]
文件名稱:MessageBoxPlus.cs
開發環境:
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);
}
}
}
摘自 秦建輝的專欄