WindowsForm实现警告消息框的实例代码
作者:zhuanghamiao 发布时间:2023-05-25 00:00:54
标签:WindowsForm,实现,警告消息框
警告消息框主要是用来向用户户展示诸如警告、异常、完成和提示消息。一般实现的效果就是从系统窗口右下角弹出,然后加上些简单的显示和消失的动画。
创建警告框窗口
首先我们创建一个警告框窗口(Form),将窗口设置为无边框(FormBoderStyle=None),添加上图片和内容显示控件
创建好警告框后,我们先让他能够从窗口右下角显示出来,
public partial class AlertMessageForm : Form
{
public AlertMessageForm()
{
InitializeComponent();
}
private int x, y;
public void Show(string message)
{
this.StartPosition = FormStartPosition.Manual;
this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
this.Location = new Point(x, y);
labelContent.Text = message;
this.Show();
}
}
警告框显示和关闭动画
添加一个计时器,通过时钟控制窗口背景渐入和淡出
// 警告框的行为(显示,停留,退出)
public enum AlertFormAction
{
Start,
Wait,
Close
}
public partial class AlertMessageForm : Form
{
public AlertMessageForm()
{
InitializeComponent();
}
private int x, y;
private AlertFormAction action;
private void timer1_Tick(object sender, EventArgs e)
{
switch (action)
{
case AlertFormAction.Start:
timer1.Interval = 50;//警告显示的时间
this.Opacity += 0.1;
if (this.Opacity == 1.0)
{
action = AlertFormAction.Wait;
}
break;
case AlertFormAction.Wait:
timer1.Interval = 3000;//警告框停留时间
action = AlertFormAction.Close;
break;
case AlertFormAction.Close:
timer1.Interval = 50;//警告退出的时间
this.Opacity -= 0.1;
if (this.Opacity == 0.0)
{
this.Close();
}
break;
default:
break;
}
}
public void Show(string message)
{
//设置窗口启始位置
this.StartPosition = FormStartPosition.Manual;
this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
this.Location = new Point(x, y);
labelContent.Text = message;
this.Opacity = 0.0;
this.Show();
action = AlertFormAction.Start;
//启动时钟
timer1.Start();
}
}
处理多种不同类型的警告框
添加AlertType枚举,让警告框显示不同类型的消息,根据消息类型变换不同的消息主题颜色,并未Show方法添加警告框类型参数
public enum AlertType
{
Info,
Success,
Warning,
Error
}
// 设置警告框主题
private void SetAlertTheme(AlertType type)
{
switch (type)
{
case AlertType.Info:
this.pictureBox1.Image = Properties.Resources.info;
this.BackColor = Color.RoyalBlue;
break;
case AlertType.Success:
this.pictureBox1.Image = Properties.Resources.success;
this.BackColor = Color.SeaGreen;
break;
case AlertType.Warning:
this.pictureBox1.Image = Properties.Resources.warning;
this.BackColor = Color.DarkOrange;
break;
case AlertType.Error:
this.pictureBox1.Image = Properties.Resources.error;
this.BackColor = Color.DarkRed;
break;
default:
break;
}
}
// 显示警告框
public void Show(string message, AlertType type){
// ...
SetAlertTheme(type);
}
处理多个警告框重叠问题
当然,完成上面的处理是不够的,当有多个消息的时候,消息框会重叠在一起;多个消息时,需要将消息窗口按一定的规则排列,这里我们设置每个消息窗口间隔一定的距离
public void Show(string message, AlertType type)
{
// 设置窗口启始位置
this.StartPosition = FormStartPosition.Manual;
// 设置程序每个打开的消息窗口的位置,超过10个就不做处理,这个可以根据自己的需求设定
string fname;
for (int i = 1; i < 10; i++)
{
fname = "alert" + i.ToString();
AlertMessageForm alert = (AlertMessageForm)Application.OpenForms[fname];
if (alert == null)
{
this.Name = fname;
this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height * i - 5 * i;
this.Location = new Point(x, y);
break;
}
}
labelContent.Text = message;
this.Opacity = 0.0;
SetAlertTheme(type);
this.Show();
action = AlertFormAction.Start;
//启动时钟
timer1.Start();
}
鼠标悬停警告框处理
想要警告框停留的时间长一些,一中方式是直接设置警告框停留的时间长一些,另一种方式是通过判断鼠标在警告框窗口是否悬停,所以可以通过鼠标的悬停和离开事件进行处理
private void AlertMessageForm_MouseMove(object sender, MouseEventArgs e)
{
this.Opacity = 1.0;
timer1.Interval = int.MaxValue;//警告框停留时间
action = AlertFormAction.Close;
}
private void AlertMessageForm_MouseLeave(object sender, EventArgs e)
{
this.Opacity = 1.0;
timer1.Interval = 3000;//警告框停留时间
action = AlertFormAction.Close;
}
警告框的完整代码
public enum AlertType
{
Info,
Success,
Warning,
Error
}
public enum AlertFormAction
{
Start,
Wait,
Close
}
public partial class AlertMessageForm : Form
{
public AlertMessageForm()
{
InitializeComponent();
}
private int x, y;
private AlertFormAction action;
private void timer1_Tick(object sender, EventArgs e)
{
switch (action)
{
case AlertFormAction.Start:
timer1.Interval = 50;//警告显示的时间
this.Opacity += 0.1;
if (this.Opacity == 1.0)
{
action = AlertFormAction.Wait;
}
break;
case AlertFormAction.Wait:
timer1.Interval = 3000;//警告框停留时间
action = AlertFormAction.Close;
break;
case AlertFormAction.Close:
timer1.Interval = 50;//警告关闭的时间
this.Opacity -= 0.1;
if (this.Opacity == 0.0)
{
this.Close();
}
break;
default:
break;
}
}
public void Show(string message, AlertType type)
{
// 设置窗口启始位置
this.StartPosition = FormStartPosition.Manual;
// 设置程序每个打开的消息窗口的位置,超过10个就不做处理,这个可以根据自己的需求设定
string fname;
for (int i = 1; i < 10; i++)
{
fname = "alert" + i.ToString();
AlertMessageForm alert = (AlertMessageForm)Application.OpenForms[fname];
if (alert == null)
{
this.Name = fname;
this.x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
this.y = Screen.PrimaryScreen.WorkingArea.Height - this.Height * i - 5 * i;
this.Location = new Point(x, y);
break;
}
}
labelContent.Text = message;
this.Opacity = 0.0;
SetAlertTheme(type);
this.Show();
action = AlertFormAction.Start;
//启动时钟
timer1.Start();
}
private void AlertMessageForm_MouseMove(object sender, MouseEventArgs e)
{
this.Opacity = 1.0;
timer1.Interval = int.MaxValue;//警告框停留时间
action = AlertFormAction.Close;
}
private void AlertMessageForm_MouseLeave(object sender, EventArgs e)
{
this.Opacity = 1.0;
timer1.Interval = 3000;//警告框停留时间
action = AlertFormAction.Close;
}
private void buttonClose_Click(object sender, EventArgs e)
{
// 注销鼠标事件
this.MouseLeave-= new System.EventHandler(this.AlertMessageForm_MouseLeave);
this.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.AlertMessageForm_MouseMove);
timer1.Interval = 50;//警告关闭的时间
this.Opacity -= 0.1;
if (this.Opacity == 0.0)
{
this.Close();
}
}
// 设置警告框主题
private void SetAlertTheme(AlertType type)
{
switch (type)
{
case AlertType.Info:
this.pictureBox1.Image = Properties.Resources.info;
this.BackColor = Color.RoyalBlue;
break;
case AlertType.Success:
this.pictureBox1.Image = Properties.Resources.success;
this.BackColor = Color.SeaGreen;
break;
case AlertType.Warning:
this.pictureBox1.Image = Properties.Resources.warning;
this.BackColor = Color.DarkOrange;
break;
case AlertType.Error:
this.pictureBox1.Image = Properties.Resources.error;
this.BackColor = Color.DarkRed;
break;
default:
break;
}
}
}
来源:https://www.cnblogs.com/zhuanghamiao/p/winform-alert.html


猜你喜欢
- 本文实例为大家分享了Intent实现页面跳转的两种的方法,供大家参考,具体内容如下下图中两个不同的方法就是两种页面之间跳转的情况1).跳转不
- 本文的目的是把json串转成map键值对存储,而且只存储叶节点的数据比如json数据如下:{responseHeader:{status:0
- 本文介绍了springcloud Feign的Hystrix支持,分享给大家,具体如下:一、Feign client中加入Hystrix的f
- 最近在做一个移动端HTML5的应用,使用到了上传功能,起初使用传统的上传方式上传手机拍照的照片,由于手机拍照出来的照片一般都是好几MB,所以
- 自动生成的代码报错解决办法:把自动xml文件中自动生成的二级缓存注释掉来源:https://blog.csdn.net/weixin_447
- 本篇将从以下几个方面讲述反射的知识:class 的使用方法的反射构造函数的反射成员变量的反射一、什么是class类在面向对象的世界里,万物皆
- 在开发中,我们会遇到将不同组织架构合并成tree这种树状结构,那么如果做呢?实际上,我们也可以理解为如何将拥有父子关系的list转成树形结构
- 前言:Android Studio中把项目的lib库提交到Jcenter仓库中,需要使用到Bintray,Bintray是jCenter的提
- 本文实例为大家分享了java实现购物车功能的具体代码,供大家参考,具体内容如下1 需要实现1、实现淘淘商城的购物车功能2 购物车功能2.1
- ArrayList和LinkedList都实现了List接口,有以下的不同点:1、ArrayList是基于索引的数据接口,它的底层是数组。它
- Java异常是Java提供的一种识别及响应错误的一致性机制。Java异常机制可以使程序中异常处理代码和正常业务代码分离,保证程序代码更加优雅
- BigDecimal的舍入模式(RoundingMode)BigDecimal.divide方法中必须设置roundingMode,不然会报
- Maven可以使用mvn package指令对项目进行打包,如果使用java -jar xxx.jar执行运行jar文件,会出现"
- 一、介绍pair是将2个数据组合成一组数据,当需要这样的需求时就可以使用pair。当然你也可以自定义一个结构体struct。不过大家都是为了
- 1、背景最近用到了Spring Cloud Alibaba开发微服务,在开发的过程中发现,当我们的服务上线或下线的时候,我们的Spring
- 工具类-java精确到小数点后6位验证要求,必须精确到小数点后6位,但是后面都是0的话,double会省略0,正则验证不通过,所以有了下面解
- 前言:小编引入的图片和文字描述都是来自于尚硅谷的视频讲解,在此感谢尚硅谷的老师,同时也结合 seata文档官方文档进行整合项目地址(gite
- 1.嵌套函数业务开发中,我们可能会遇到这样一个场景:一个函数只会被某一处多次调用,且不想让这个函数在该类的其他地方调用,这个时候就需要对这个
- 一、VSCode安装EmmyLua 二、添加配置文件三、设置配置文件执行完第二步会弹出添加好的launch.json配置文件,这个
- 需要记录日志的地方包括:进入方法的时候,传参的时候,统计执行时间,方法返回参数的时候,退出语句块的时候,出现异常的时候,等等。先来体验不使用