C# MessageBox扩展-自定义按钮名称

一、布局弹窗Form,用3个panel把界面分为3层:

        1、Panel panel1放一个Label lbTitle用于显示标题

        2、Panel panel2放一个Label lbMsg用于显示内容

        3、Panel panel3放3个Button,按钮返回:左边true,右边false,中间忽略

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label lbTitle;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.Label lbMsg;
        private System.Windows.Forms.Panel panel3;
        private System.Windows.Forms.Button btnYesLeft;
        private System.Windows.Forms.Button btnNoRight;
        private System.Windows.Forms.Button btnIgnoreMiddle;

 

二、控件代码

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

namespace CoupledSweep
{
    /// <summary>
    /// 扩展弹窗
    /// </summary>
    public partial class MessageBoxEx : Form
    {
        private const int WM_SYSCOMMAND = 0x0112;//该变量表示将向Windows发送的消息类型
        private const int SC_MOVE = 0xF010;//该变量表示发送消息的附加消息
        private const int HTCAPTION = 0x0002;//该变量表示发送消息的附加消息
        [DllImport("user32.dll")]//从当前线程中的窗口释放鼠标捕获(释放被当前线程中某个窗口捕获的光标),并恢复正常的鼠标输入处理
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]//向指定的窗体发送Windows消息
        public static extern bool SendMessage(IntPtr hwdn, int wMsg, int mParam, int lParam);

        /// <summary>
        /// 
        /// </summary>
        public MessageBoxEx()
        {
            InitializeComponent();
        }

        private MessageBoxButtons messageBoxButtons = MessageBoxButtons.OK;

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="mgs">内容</param>
        /// <param name="titel">标题</param>
        /// <param name="buttons">显示的按钮</param>
        /// <returns></returns>
        public static DialogResult Show(string mgs, string titel = "提示!", MessageBoxButtons buttons = MessageBoxButtons.OK)
        {
            MessageBoxEx messageBox = new MessageBoxEx();
            messageBox.messageBoxButtons = buttons;
            messageBox.ButtonShow(buttons);
            messageBox.lbTitle.Text = titel;
            messageBox.lbMsg.Text = mgs;
            return messageBox.ShowDialog();
        }


        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="mgs">内容</param>
        /// <param name="titel">标题</param>
        /// <param name="text">按钮上显示的text,数组长度3,如:{"确定", "忽略", "取消"}</param>
        /// <param name="buttons">显示的按钮</param>
        /// <returns></returns>
        public static DialogResult Show(string mgs, string titel, string[] text, MessageBoxButtons buttons = MessageBoxButtons.OK)
        {
            MessageBoxEx messageBox = new MessageBoxEx();
            messageBox.messageBoxButtons = buttons;
            messageBox.ButtonShow(buttons, text);
            messageBox.lbTitle.Text = titel;
            messageBox.lbMsg.Text = mgs;
            return messageBox.ShowDialog();
        }

        private void ButtonShow(MessageBoxButtons buttons)
        {
            btnYesLeft.Font = new Font("微软雅黑", 27.75f, FontStyle.Bold);
            btnIgnoreMiddle.Font = new Font("微软雅黑", 27.75f, FontStyle.Bold);
            btnNoRight.Font = new Font("微软雅黑", 27.75f, FontStyle.Bold);
            switch (buttons)
            {
                case MessageBoxButtons.AbortRetryIgnore:
                    btnNoRight.Visible = true;
                    btnYesLeft.Visible = true;
                    btnIgnoreMiddle.Visible = true;

                    btnNoRight.Text = "终止";
                    btnYesLeft.Text = "重试";
                    btnIgnoreMiddle.Text = "忽略";
                    break;
                case MessageBoxButtons.OKCancel:
                    btnNoRight.Visible = true;
                    btnYesLeft.Visible = true;
                    btnIgnoreMiddle.Visible = false;

                    btnNoRight.Text = "取消";
                    btnYesLeft.Text = "确定";
                    btnIgnoreMiddle.Text = "忽略";
                    break;
                case MessageBoxButtons.RetryCancel:
                    btnNoRight.Visible = true;
                    btnYesLeft.Visible = true;
                    btnIgnoreMiddle.Visible = false;

                    btnNoRight.Text = "取消";
                    btnYesLeft.Text = "重试";
                    btnIgnoreMiddle.Text = "忽略";
                    break;
                case MessageBoxButtons.YesNo:
                    btnNoRight.Visible = true;
                    btnYesLeft.Visible = true;
                    btnIgnoreMiddle.Visible = false;

                    btnNoRight.Text = "退出";
                    btnYesLeft.Text = "继续";
                    btnIgnoreMiddle.Text = "忽略";
                    break;
                case MessageBoxButtons.YesNoCancel:
                    btnNoRight.Visible = true;
                    btnYesLeft.Visible = true;
                    btnIgnoreMiddle.Visible = true;

                    btnNoRight.Text = "否";
                    btnYesLeft.Text = "是";
                    btnIgnoreMiddle.Text = "取消";
                    break;
                default:
                    btnNoRight.Visible = false;
                    btnYesLeft.Visible = true;
                    btnIgnoreMiddle.Visible = false;

                    btnNoRight.Text = "取消";
                    btnYesLeft.Text = "确定";
                    btnIgnoreMiddle.Text = "忽略";
                    break;
            }
        }

        private void ButtonShow(MessageBoxButtons buttons, string[] text)
        {
            if (text.Length != 3)
            {
                text = new string[] { "确定", "忽略", "取消" };
            }

            btnYesLeft.Visible = true;
            btnNoRight.Visible = true;
            btnYesLeft.Font = new Font("微软雅黑", 27.75f, FontStyle.Bold);
            btnIgnoreMiddle.Font = new Font("微软雅黑", 27.75f, FontStyle.Bold);
            btnNoRight.Font = new Font("微软雅黑", 27.75f, FontStyle.Bold);
            switch (buttons)
            {
                case MessageBoxButtons.AbortRetryIgnore:
                    btnIgnoreMiddle.Visible = true;
                    break;
                case MessageBoxButtons.OKCancel:
                    btnIgnoreMiddle.Visible = false;
                    break;
                case MessageBoxButtons.RetryCancel:
                    btnIgnoreMiddle.Visible = false;
                    break;
                case MessageBoxButtons.YesNo:
                    btnIgnoreMiddle.Visible = false;
                    break;
                case MessageBoxButtons.YesNoCancel:
                    btnIgnoreMiddle.Visible = true;
                    break;
                default:
                    btnNoRight.Visible = false;
                    btnIgnoreMiddle.Visible = false;
                    break;
            }
            btnNoRight.Text = text[2];
            btnYesLeft.Text = text[0];
            btnIgnoreMiddle.Text = text[1];

            if (text[0].Length >= 3)
            {
                btnYesLeft.Font = new Font("微软雅黑", 15.75f, FontStyle.Bold);
            }
            if (text[1].Length >= 3)
            {
                btnIgnoreMiddle.Font = new Font("微软雅黑", 15.75f, FontStyle.Bold);
            }
            if (text[2].Length >= 3)
            {
                btnNoRight.Font = new Font("微软雅黑", 15.75f, FontStyle.Bold);
            }
        }
        private void ButtonResult(object sender, MessageBoxButtons buttons = MessageBoxButtons.OK)
        {
            Button btn = (Button)sender;
            switch (buttons)
            {
                case MessageBoxButtons.AbortRetryIgnore:
                    if (btn.Name == "btnNoRight")
                    {
                        this.DialogResult = DialogResult.Abort;
                    }
                    else if (btn.Name == "btnYesLeft")
                    {
                        this.DialogResult = DialogResult.Retry;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.Ignore;
                    }
                    break;
                case MessageBoxButtons.OKCancel:
                    if (btn.Name == "btnNoRight")
                    {
                        this.DialogResult = DialogResult.Cancel;
                    }
                    else if (btn.Name == "btnYesLeft")
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.Ignore;
                    }
                    break;
                case MessageBoxButtons.RetryCancel:
                    if (btn.Name == "btnNoRight")
                    {
                        this.DialogResult = DialogResult.Cancel;
                    }
                    else if (btn.Name == "btnYesLeft")
                    {
                        this.DialogResult = DialogResult.Retry;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.Ignore;
                    }
                    break;
                case MessageBoxButtons.YesNo:
                    if (btn.Name == "btnNoRight")
                    {
                        this.DialogResult = DialogResult.No;
                    }
                    else if (btn.Name == "btnYesLeft")
                    {
                        this.DialogResult = DialogResult.Yes;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.Ignore;
                    }
                    break;
                case MessageBoxButtons.YesNoCancel:
                    if (btn.Name == "btnNoRight")
                    {
                        this.DialogResult = DialogResult.No;
                    }
                    else if (btn.Name == "btnYesLeft")
                    {
                        this.DialogResult = DialogResult.Yes;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.Cancel;
                    }
                    break;
                default:
                    if (btn.Name == "btnNoRight")
                    {
                        this.DialogResult = DialogResult.Cancel;
                    }
                    else if (btn.Name == "btnYesLeft")
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        this.DialogResult = DialogResult.Ignore;
                    }
                    break;
            }
        }
        private void btnYesLeft_Click(object sender, EventArgs e)
        {
            ButtonResult(sender, messageBoxButtons);
            this.Dispose();
        }

        private void btnIgnoreMiddle_Click(object sender, EventArgs e)
        {
            ButtonResult(sender, messageBoxButtons);
            this.Dispose();
        }

        private void btnNoRight_Click(object sender, EventArgs e)
        {
            ButtonResult(sender, messageBoxButtons);
            this.Dispose();
        }

        private void MessageBoxEx_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)//左键按下移动,拖拽调整大小
            {
                // MousePosition的参考点是屏幕的左上角,表示鼠标当前相对于屏幕左上角的坐标。this.Left和this.Top的参考点也是屏幕
                if (Cursor == Cursors.SizeNWSE) // 倾斜拖拽 
                {
                    // 改变窗体宽和高的代码,其宽高为鼠标屏幕位置减去窗体的Left,Top距离
                    this.Width = MousePosition.X - this.Left;
                    this.Height = MousePosition.Y - this.Top;
                }
                else if (Cursor == Cursors.SizeWE) // 水平拖拽
                {
                    Width = MousePosition.X - this.Left;
                }
                else if (Cursor == Cursors.SizeNS) // 垂直拖拽
                {
                    Height = MousePosition.Y - this.Top;
                }
            }

            int maxPixels = 10;
            //鼠标移动过程中,坐标时刻在改变 
            //当鼠标移动时横坐标距离窗体右边缘5像素以内且纵坐标距离下边缘也在5像素以内时,要将光标变为倾斜的箭头形状
            if (e.Location.X > this.Width - maxPixels && e.Location.Y > this.Height - maxPixels)
            {
                this.Cursor = Cursors.SizeNWSE; // 右下角 双向对角线光标
            }
            //当鼠标移动时横坐标距离窗体右边缘5像素以内时,要将光标变为双向水平箭头形状
            else if (e.Location.X > this.Width - maxPixels)
            {
                this.Cursor = Cursors.SizeWE; // 双向水平光标
            }
            //当鼠标移动时纵坐标距离窗体下边缘5像素以内时,要将光标变为垂直水平箭头形状
            else if (e.Location.Y > this.Height - maxPixels)
            {
                this.Cursor = Cursors.SizeNS; // 双向垂直光标

            }
            else//否则,以外的窗体区域,鼠标星座均为单向箭头(默认) 
            {
                this.Cursor = Cursors.Arrow;
                if (e.Button == MouseButtons.Left)
                {
                    ReleaseCapture(); // 释放鼠标捕获
                                      // 向Windows发送拖动窗体的消息,下面的消息仅在左键按下时有效
                    SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
                }
            }
        }

        protected override void WndProc(ref Message m)
        {
            const int WM_NCHITTEST = 0x84;
            const int HTCLIENT = 0x01;
            const int HTCAPTION = 0x02;
            if (m.Msg == WM_NCHITTEST)
            {
                this.DefWndProc(ref m);
                if (m.Result.ToInt32() == HTCLIENT)
                    m.Result = new IntPtr(HTCAPTION);
                else
                    base.WndProc(ref m);
            }
            else
            {
                base.WndProc(ref m);
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值