自定义c# MessageBox 弹出框中的button的Text

使用winform 自带的messagebox无法更改MessageBoxButtons的text。可以自定义一个form实现自定的button的文本

代码示例:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace Example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //测试
            DialogResult = MessageBoxEX.Show("此单据生产已完成", "提示", MessageBoxButtons.YesNoCancel, new string[] { "关机", "切换单据" });

        }
    }
    public class MessageBoxEX
    {
        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons, string[] buttonTitles)
        {
            MessageForm frm = new MessageForm(buttons, buttonTitles);
            frm.Show();
            frm.WatchForActivate = true;
            DialogResult result = MessageBox.Show(frm, text, caption, buttons);
            frm.Close();

            return result;
        }

        public static DialogResult Show(string text, string caption, MessageBoxButtons buttons,
            MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, string[] buttonTitles)
        {
            MessageForm frm = new MessageForm(buttons, buttonTitles);
            frm.Show();
            frm.WatchForActivate = true;
            DialogResult result = MessageBox.Show(frm, text, caption, buttons, icon, defaultButton);
            frm.Close();

            return result;
        }

        class MessageForm : Form
        {
            IntPtr _handle;
            MessageBoxButtons _buttons;
            string[] _buttonTitles = null;

            bool _watchForActivate = false;

            public bool WatchForActivate
            {
                get { return _watchForActivate; }
                set { _watchForActivate = value; }
            }

            public MessageForm(MessageBoxButtons buttons, string[] buttonTitles)
            {
                _buttons = buttons;
                _buttonTitles = buttonTitles;

                // Hide self form, and don't show self form in task bar.
                this.Text = "";
                this.StartPosition = FormStartPosition.CenterScreen;
                this.Location = new Point(-32000, -32000);
                this.ShowInTaskbar = false;
            }

            protected override void OnShown(EventArgs e)
            {
                base.OnShown(e);
                // Hide self form, don't show self form even in task list.
                NativeWin32API.SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, 0, 0, 659);
            }

            protected override void WndProc(ref System.Windows.Forms.Message m)
            {
                if (_watchForActivate && m.Msg == 0x0006)
                {
                    _watchForActivate = false;
                    _handle = m.LParam;
                    CheckMsgbox();
                }
                base.WndProc(ref m);
            }

            private void CheckMsgbox()
            {
                if (_buttonTitles == null || _buttonTitles.Length == 0)
                    return;

                // Button title index
                int buttonTitleIndex = 0;
                // Get the handle of control in current window.
                IntPtr h = NativeWin32API.GetWindow(_handle, GW_CHILD);

                // Set those custom titles to the three buttons(Default title are: Yes, No and Cancle).
                while (h != IntPtr.Zero)
                {
                    if (NativeWin32API.GetWindowClassName(h).Equals("Button"))
                    {
                        if (_buttonTitles.Length > buttonTitleIndex)
                        {
                            // Changes the text of the specified window's title bar (if it has one). 
                            // If the specified window is a control, the text of the control is changed. 
                            // However, SetWindowText cannot change the text of a control in another application.
                            NativeWin32API.SetWindowText(h, _buttonTitles[buttonTitleIndex]);

                            buttonTitleIndex++;
                        }
                    }

                    // Get the handle of next control in current window.
                    h = NativeWin32API.GetWindow(h, GW_HWNDNEXT);
                }
            }
        }


        public const int GW_CHILD = 5;
        public const int GW_HWNDNEXT = 2;

        public class NativeWin32API
        {
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, int flags);
            [DllImport("user32.dll")]
            public static extern IntPtr GetWindow(IntPtr hWnd, Int32 wCmd);
            [DllImport("user32.dll")]
            public static extern bool SetWindowText(IntPtr hWnd, string lpString);
            [DllImport("user32.dll")]
            public static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);

            public static string GetWindowClassName(IntPtr handle)
            {
                StringBuilder sb = new StringBuilder(256);

                // Retrieves the name of the class to which the specified window belongs
                GetClassNameW(handle, sb, sb.Capacity);
                return sb.ToString();
            }
        }

    }

}

效果图

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
C#中,`MessageBox`类没有提供直接的方法来实现弹出后自动关闭的功能。不过,你可以通过结合使用`Timer`类来实现这个效果。以下是一个示例代码: ```csharp using System; using System.Windows.Forms; class Program { static void Main(string[] args) { // 创建一个Timer实例 Timer timer = new Timer(); // 设置定时器的间隔(单位:毫秒) timer.Interval = 3000; // 3秒钟 // 注册定时器的Tick事件处理程序 timer.Tick += Timer_Tick; // 显示MessageBox MessageBox.Show("这是一个自动关闭的MessageBox"); // 启动定时器 timer.Start(); Application.Run(); } static void Timer_Tick(object sender, EventArgs e) { // 停止定时器 Timer timer = (Timer)sender; timer.Stop(); // 关闭MessageBox MessageBox.Close(); } } ``` 在上面的示例中,我们首先创建了一个`Timer`实例,并设置间隔为3秒钟。然后,在弹出`MessageBox`之后,启动定时器。当定时器的Tick事件发生时,我们停止定时器,并通过`MessageBox.Close()`方法关闭`MessageBox`。 需要注意的是,为了使`Application.Run()`方法保持运行状态,我们使用了`System.Windows.Forms`命名空间。这样可以保持应用程序运行,直到手动关闭它。 请注意,在一些特定情况下,例如在控制台应用程序中使用`MessageBox`时,它可能会阻止应用程序继续执行,直到用户关闭`MessageBox`。在这种情况下,你可能需要考虑使用其他的UI组件或自定义对话框来实现自动关闭的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值