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

您所在的位置:网站首页 wpf美化message C#自定义好看的消息提示窗口MessageBox

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

2024-03-26 03:04| 来源: 网络整理| 查看: 265

效果

优点

模态对话框,原来的主窗口无法点击必须先响应消息提示窗口不需要为 该消息窗口的实例添加按钮点击事件,响应消息窗口以后可以立刻返回用户点击了确定还是取消调用该消息窗口的线程,必须等待用户响应了消息窗口才能继续执行下面的代码,和原生的MessageBox类一样 缺点需要新建实例再使用 不能像原生的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; } }

持续更新中…



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3