当前位置:编程学习 > C#/ASP.NET >>

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

前言:
在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); 
        } 
    } 

 

摘自 秦建辉的专栏

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,