C# WinForm中实现MessageBox.Show()的常用方法

     MessageBox.Show (String) 在C#中用来显示具有指定文本的消息框。在C# WinForm仅仅一步就可以实现弹出对话框实现了消息提示功能了。下面我们一起来总结一下这个强大的消息提示神器吧。用反编译软件.NET Reflector 进行查看MessageBox类,如下:

【PS】由于元数据文件较多,大家耐心看看,体会一下其中奥妙,具体实例请下拉到文章最后部分哦。

// Generated by .NET Reflector from C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll
namespace System.Windows.Forms
{
    using System;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
   
    public class MessageBox
    {
        private const int HELP_BUTTON = 0x4000;
        [ThreadStatic]
        private static System.Windows.Forms.HelpInfo[] helpInfoTable;
        private const int IDABORT = 3;
        private const int IDCANCEL = 2;
        private const int IDIGNORE = 5;
        private const int IDNO = 7;
        private const int IDOK = 1;
        private const int IDRETRY = 4;
        private const int IDYES = 6;
        //这里表示Show中有多少参数
        private MessageBox()
        {
        }
       
        private static void PopHelpInfo()
        {
            if (helpInfoTable != null)
            {
                if (helpInfoTable.Length == 1)
                {
                    helpInfoTable = null;
                }
                else
                {
                    int length = helpInfoTable.Length - 1;
                    System.Windows.Forms.HelpInfo[] destinationArray = new System.Windows.Forms.HelpInfo[length];
                    Array.Copy(helpInfoTable, destinationArray, length);
                    helpInfoTable = destinationArray;
                }
            }
        }
       
        private static void PushHelpInfo(System.Windows.Forms.HelpInfo hpi)
        {
            System.Windows.Forms.HelpInfo[] infoArray;
            int length = 0;
            if (helpInfoTable == null)
            {
                infoArray = new System.Windows.Forms.HelpInfo[length + 1];
            }
            else
            {
                length = helpInfoTable.Length;
                infoArray = new System.Windows.Forms.HelpInfo[length + 1];
                Array.Copy(helpInfoTable, infoArray, length);
            }
            infoArray[length] = hpi;
            helpInfoTable = infoArray;
        }
        //下面用黄颜色背景的表示参数个数和形式
        public static DialogResult Show(string text)
        { 
            return ShowCore(null, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
        }
       
        public static DialogResult Show(string text, string caption)
        {
            return ShowCore(null, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
        }
       
        public static DialogResult Show(IWin32Window owner, string text)
        {
            return ShowCore(owner, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
        }
       
        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
        {
            return ShowCore(null, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
        }
       
        public static DialogResult Show(IWin32Window owner, string text, string caption)
        {
            return ShowCore(owner, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
        }
       
        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            return ShowCore(null, text, caption, buttons, icon, MessageBoxDefaultButton.Button1, 0, false);
        }
       
        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons)
        {
            return ShowCore(owner, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
        }
       
        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            return ShowCore(null, text, caption, buttons, icon, defaultButton, 0, false);
        }
       
        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            return ShowCore(owner, text, caption, buttons, icon, MessageBoxDefaultButton.Button1, 0, false);
        }
       
        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
        {
            return ShowCore(null, text, caption, buttons, icon, defaultButton, options, false);
        }
       
        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            return ShowCore(owner, text, caption, buttons, icon, defaultButton, 0, false);
        }
       
        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, bool displayHelpButton)
        {
            return ShowCore(null, text, caption, buttons, icon, defaultButton, options, displayHelpButton);
        }
       
        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, string helpFilePath)
        {
            System.Windows.Forms.HelpInfo hpi = new System.Windows.Forms.HelpInfo(helpFilePath);
            return ShowCore(null, text, caption, buttons, icon, defaultButton, options, hpi);
        }
       
        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
        {
            return ShowCore(owner, text, caption, buttons, icon, defaultButton, options, false);
        }
       
        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, string helpFilePath, string keyword)
        {
            System.Windows.Forms.HelpInfo hpi = new System.Windows.Forms.HelpInfo(helpFilePath, keyword);
            return ShowCore(null, text, caption, buttons, icon, defaultButton, options, hpi);
        }
       
        public static DialogResultShow(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, string helpFilePath, HelpNavigator navigator)
        {
            System.Windows.Forms.HelpInfo hpi = new System.Windows.Forms.HelpInfo(helpFilePath, navigator);
            return ShowCore(null, text, caption, buttons, icon, defaultButton, options, hpi);
        }
       
        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, string helpFilePath)
        {
            System.Windows.Forms.HelpInfo hpi = new System.Windows.Forms.HelpInfo(helpFilePath);
            return ShowCore(owner, text, caption, buttons, icon, defaultButton, options, hpi);
        }
       
        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, string helpFilePath, HelpNavigator navigator, object param)
        {
            System.Windows.Forms.HelpInfo hpi = new System.Windows.Forms.HelpInfo(helpFilePath, navigator, param);
            return ShowCore(null, text, caption, buttons, icon, defaultButton, options, hpi);
        }
       
        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, string helpFilePath, string keyword)
        {
            System.Windows.Forms.HelpInfo hpi = new System.Windows.Forms.HelpInfo(helpFilePath, keyword);
            return ShowCore(owner, text, caption, buttons, icon, defaultButton, options, hpi);
        }
       
        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, string helpFilePath, HelpNavigator navigator)
        {
            System.Windows.Forms.HelpInfo hpi = new System.Windows.Forms.HelpInfo(helpFilePath, navigator);
            return ShowCore(owner, text, caption, buttons, icon, defaultButton, options, hpi);
        }
       
        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, string helpFilePath, HelpNavigator navigator, object param)
        {
            System.Windows.Forms.HelpInfo hpi = new System.Windows.Forms.HelpInfo(helpFilePath, navigator, param);
            return ShowCore(owner, text, caption, buttons, icon, defaultButton, options, hpi);
        }
       
        private static DialogResult ShowCore(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, bool showHelp)
        {
            DialogResult result;
            if (!System.Windows.Forms.ClientUtils.IsEnumValid(buttons, (int) buttons, 0, 5))
            {
                throw new InvalidEnumArgumentException("buttons", (int) buttons, typeof(MessageBoxButtons));
            }
            if (!WindowsFormsUtils.EnumValidator.IsEnumWithinShiftedRange(icon, 4, 0, 4))
            {
                throw new InvalidEnumArgumentException("icon", (int) icon, typeof(MessageBoxIcon));
            }
            if (!WindowsFormsUtils.EnumValidator.IsEnumWithinShiftedRange(defaultButton, 8, 0, 2))
            {
                throw new InvalidEnumArgumentException("defaultButton", (int) defaultButton, typeof(DialogResult));
            }
            if (!SystemInformation.UserInteractive && ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0))
            {
                throw new InvalidOperationException(System.Windows.Forms.SR.GetString("CantShowModalOnNonInteractive"));
            }
            if ((owner != null) && ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0))
            {
                throw new ArgumentException(System.Windows.Forms.SR.GetString("CantShowMBServiceWithOwner"), "options");
            }
            if (showHelp && ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0))
            {
                throw new ArgumentException(System.Windows.Forms.SR.GetString("CantShowMBServiceWithHelp"), "options");
            }
            if ((options & ~(MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign)) != 0)
            {
                System.Windows.Forms.IntSecurity.UnmanagedCode.Demand();
            }
            System.Windows.Forms.IntSecurity.SafeSubWindows.Demand();
            int type = showHelp ? 0x4000 : 0;
            type |= ((buttons | ((MessageBoxButtons) ((int) icon))) | ((MessageBoxButtons) ((int) defaultButton))) | ((MessageBoxButtons) ((int) options));
            IntPtr zero = IntPtr.Zero;
            if (showHelp || ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0))
            {
                if (owner == null)
                {
                    zero = UnsafeNativeMethods.GetActiveWindow();
                }
                else
                {
                    zero = Control.GetSafeHandle(owner);
                }
            }
            IntPtr userCookie = IntPtr.Zero;
            if (Application.UseVisualStyles)
            {
                if ((UnsafeNativeMethods.GetModuleHandle("shell32.dll") == IntPtr.Zero) && (UnsafeNativeMethods.LoadLibrary("shell32.dll") == IntPtr.Zero))
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(), System.Windows.Forms.SR.GetString("LoadDLLError", new object[] { "shell32.dll" }));
                }
                userCookie = UnsafeNativeMethods.ThemingScope.Activate();
            }
            Application.BeginModalMessageLoop();
            try
            {
                result = Win32ToDialogResult(SafeNativeMethods.MessageBox(new HandleRef(owner, zero), text, caption, type));
            }
            finally
            {
                Application.EndModalMessageLoop();
                UnsafeNativeMethods.ThemingScope.Deactivate(userCookie);
            }
            UnsafeNativeMethods.SendMessage(new HandleRef(owner, zero), 7, 0, 0);
            return result;
        }
       
        private static DialogResult ShowCore(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, System.Windows.Forms.HelpInfo hpi)
        {
            DialogResult none = DialogResult.None;
            try
            {
                PushHelpInfo(hpi);
                none = ShowCore(owner, text, caption, buttons, icon, defaultButton, options, true);
            }
            finally
            {
                PopHelpInfo();
            }
            return none;
        }
       
        private static DialogResult Win32ToDialogResult(int value)
        {
            switch (value)
            {
                case 1:
                    return DialogResult.OK;
               
                case 2:
                    return DialogResult.Cancel;
               
                case 3:
                    return DialogResult.Abort;
               
                case 4:
                    return DialogResult.Retry;
               
                case 5:
                    return DialogResult.Ignore;
               
                case 6:
                    return DialogResult.Yes;
               
                case 7:
                    return DialogResult.No;
            }
            return DialogResult.No;
        }
       
        internal static System.Windows.Forms.HelpInfo HelpInfo
        {
            get
            {
                if ((helpInfoTable != null) && (helpInfoTable.Length > 0))
                {
                    return helpInfoTable[helpInfoTable.Length - 1];
                }
                return null;
            }
        }
    }
}

 **************************************************************************************

**********华丽丽的分割线**【MSDN的重载方法介绍】粗体为常用**************

MessageBox.Show (String) 显示具有指定文本的消息框。 .NET Compact Framework 支持。 

MessageBox.Show (IWin32Window, String) 在指定对象的前面显示具有指定文本的消息框。 

MessageBox.Show (String, String) 显示具有指定文本和标题的消息框。 .NET Compact Framework 支持。 

MessageBox.Show (IWin32Window, String, String) 在指定对象的前面显示具有指定文本和标题的消息框。 

MessageBox.Show (String, String, MessageBoxButtons) 显示具有指定文本、标题和按钮的消息框。 

MessageBox.Show (IWin32Window, String, String, MessageBoxButtons) 在指定对象的前面显示具有指定文本、标题和按钮的消息框。 

MessageBox.Show (String, String, MessageBoxButtons, MessageBoxIcon) 显示具有指定文本、标题、按钮和图标的消息框。 

MessageBox.Show (IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon) 在指定对象的前面显示具有指定文本、标题、按钮和图标的消息框。 

MessageBox.Show (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton) 显示具有指定文本、标题、按钮、图标和默认按钮消息框。 

MessageBox.Show (IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton) 在指定对象的前面显示具有指定文本、标题、按钮、图标和默认按钮的消息框。 

MessageBox.Show (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions) 显示具有指定文本、标题、按钮、图标、默认按钮和选项的消息框。 

MessageBox.Show (IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions) 在指定对象的前面显示具有指定文本、标题、按钮、图标、默认按钮和选项的消息框。 

MessageBox.Show (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, Boolean) 显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和"帮助"按钮的消息框。 

MessageBox.Show (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String) 使用指定的帮助文件显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和"帮助"按钮的消息框。 

MessageBox.Show (IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String) 使用指定的帮助文件显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和"帮助"按钮的消息框。 

MessageBox.Show (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator) 使用指定的帮助文件和 HelpNavigator 显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和"帮助"按钮的消息框。 

MessageBox.Show (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, String) 使用指定的帮助文件和帮助关键字[guan jian zi]显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和"帮助"按钮的消息框。 

MessageBox.Show (IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator) 使用指定的帮助文件和 HelpNavigator 显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和"帮助"按钮的消息框。 

MessageBox.Show (IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, String) 使用指定的帮助文件和帮助关键字[guan jian zi]显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和"帮助"按钮的消息框。 

MessageBox.Show (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator, Object) 使用指定的帮助文件、HelpNavigator 和帮助主题显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和"帮助"按钮的消息框。 

MessageBox.Show (IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator, Object) 使用指定的帮助文件、HelpNavigator 和帮助主题显示一个具有指定文本、标题、按钮、图标、默认按钮、选项和"帮助"按钮的消息框。   

 

            无奈的分割线 【转载网上百度文库的一篇文章】实例演示

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("  1  个参数 " );
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" 2 个参数。。 ",  "提示" );
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" 3 个参数。。。 ", " 提示", MessageBoxButtons.YesNoCancel);
        } 


        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" 4 个参数。。。  ", " 提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" 5 个参数。。 。  ", " 提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button2);
        }

            private void button6_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" 6 个参数。。。  ", " 提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading      //ServiceNotification//.RightAlign   // 标题向右对齐。);

        }

        private void button7_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" 7 个参数。。帮助菜单不可用。。。。。  ", " 示", MessageBoxButtons.OKCancel,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button2,
MessageBoxOptions.RightAlign, true   // 标题向右对齐。。。。。
   );

        }
        private void button8_Click(object sender, EventArgs e)
        {
            MessageBox.Show(" 7 个参数。帮助菜单    可用。   ", " 提示",MessageBoxButtons.OKCancel,
MessageBoxIcon.Warning,MessageBoxDefaultButton.Button2,MessageBoxOptions.RightAlign  ,   // 要使用默认风格,此处参数可设为 0    
@"C:\Documents and Settings\Administrator\桌面\新建文本文档.txt"
);
        }

}
}

 

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值