C#自定义好看的消息提示窗口MessageBox

本文介绍了一种自定义模态对话框的实现,该对话框在显示时阻止主窗口交互,提供了确定和取消选项。用户无需为消息窗口添加额外的按钮事件,可以直接获取用户操作。此外,文章展示了消息窗口在不同状态下的效果,并提供了详细的代码实现,包括按钮初始化、窗口布局和事件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果

优点

  1. 模态对话框,原来的主窗口无法点击必须先响应消息提示窗口
  2. 不需要为 该消息窗口的实例添加按钮点击事件,响应消息窗口以后可以立刻返回用户点击了确定还是取消
  3. 调用该消息窗口的线程,必须等待用户响应了消息窗口才能继续执行下面的代码,和原生的MessageBox类一样
    缺点
  4. 需要新建实例再使用 不能像原生的MessageBox类直接利用MessageBox.show()使用

(下面通过点击按钮1 显示消息窗口来展示效果)
消息窗口出现前的效果
在这里插入图片描述
消息窗口出现后的效果
在这里插入图片描述
鼠标移动到消息窗口按钮的效果图
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
同时鼠标可以 自由移动消息窗口

用法

在这里插入图片描述

        private void button1_Click(object sender, EventArgs e2)
        {

            using(MyMessageBox1 myMessageBox1 = new MyMessageBox1())
            {
                string hh = myMessageBox1.showMessage("我i说的话发生发撒");
                if(hh == "confirm")
                {
                    Console.WriteLine("点击了确定");
                }
                else if(hh == "cancel")
                {
                    Console.WriteLine("点击了取消或叉叉");
                }
            }

        }

源代码

命名空间

using System;
using System.Windows.Forms;
using System.Drawing;

消息窗口代码


    public class MyMessageBox1: Form
    {
        #region 初始化按钮
        private void button_init(Button button)
        {
            button.BackColor = Color.White;
            button.FlatStyle = FlatStyle.Flat;
            button.Font = new Font("微软雅黑", 10);
            button.FlatAppearance.BorderColor = Color.Black;
            button.FlatAppearance.MouseOverBackColor = Color.Black;
            button.FlatAppearance.MouseDownBackColor = Color.Black;
            button.MouseEnter += button_WinSignIn_SignIn_MouseEnter;
            button.MouseLeave += button_Lev;
        }
        private void button_WinSignIn_SignIn_MouseEnter(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            button.ForeColor = Color.White;
        }
        private void button_Lev(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            button.ForeColor = Color.Black;
        }
        #endregion

        Button[] buttons = new Button[2];
        Label[] labels = new Label[1];
        public MyMessageBox1()
        {
            this.Size = new Size(320, 200);
            this.BackColor = Color.White;
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            //this.ControlBox = false;
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;


            labels[0] = new Label();
            labels[0].Text = "";
            labels[0].Anchor = AnchorStyles.None;
            labels[0].AutoSize = true;
            labels[0].Font = new Font("微软雅黑", 12);
            this.Controls.Add(labels[0]);

            buttons[0] = new Button();
            buttons[0].Text = "确定";
            button_init(buttons[0]);
            buttons[0].Font = new Font("微软雅黑", 10);
            buttons[0].Width = 150;
            buttons[0].Height = 50;
            buttons[0].Location = new Point(1, this.Height - 90);
            buttons[0].FlatAppearance.BorderColor = Color.White;
            buttons[0].Click += MyMessageBox1_Click1;
            buttons[0].Anchor = AnchorStyles.None;
            this.Controls.Add(buttons[0]);

            buttons[1] = new Button();
            buttons[1].Location = new Point(153, buttons[0].Location.Y);
            buttons[1].Text = "取消";
            button_init(buttons[1]);
            buttons[1].Font = new Font("微软雅黑", 10);
            buttons[1].Width = 150;
            buttons[1].Height = 50;
            buttons[1].FlatAppearance.BorderColor = Color.White;
            buttons[1].Click += MyMessageBox1_Click;
            buttons[1].Anchor = AnchorStyles.None;
            this.Controls.Add(buttons[1]);
        }
        //当点击右上角叉叉的时候默认返回cancel
        string theClickButton = "cancel";
        private void MyMessageBox1_Click1(object sender, EventArgs e)
        {
            theClickButton = "confirm";
            this.Close();
        }

        private void MyMessageBox1_Click(object sender, EventArgs e)
        {
            theClickButton = "cancel";
            this.Close();
        }

        public string showMessage(string text)
        {

            labels[0].Text = text;
            labels[0].Location = new Point(this.Width / 2 - labels[0].Width / 2-10, this.Height / 2 - labels[0].Height / 2-45);
            //ShowDialog相当于线程暂停 此处的话就需要close或者dispose以后才会 return theClickButton;
            this.ShowDialog();
            return theClickButton;
        }

    }

持续更新中…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值