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
0
投稿
猜你喜欢
- 在上一篇文章中我介绍了使用Intent显式来实现页面向下跳转,接下来这篇文章主要介绍的是使用Intent隐式来实现向上跳转,什么意思呢,就是
- 前言若你的工程还没有进行基础配置,请查看我的博文Unity 之 ShaderGraph入门使用详解,按照步骤操作完成配置即可,还能顺便学习一
- 批量添加一对多中间表建立中间表A,一个id对应多个lid;传入两条参数long id;//单个数值List lid;//集合数值dao层语句
- 本文主要介绍了java8 stream自定义分组求和并排序的实现,分享给大家,具体如下: public static void
- 这里以list为介绍:private static readonly T[] s_emptyArray = new T[0];public
- mybatis-plus自动配置mapper.xml与java接口映射本来没有mybatis-plus的话,这个工作是通过mybatis-s
- 五一期间原计划是写两篇文章,看一本技术类书籍,结果这五天由于自律性过于差,禁不住各种诱惑,我连电脑都没打开过,计划完美宣告失败。所以在这能看
- 新建一个表:create table abc(id int IDENTITY(1,1) NOT NULL,name nvarchar(100
- 今天给大家介绍下用Java swing开发一款音乐播放器, * 酷狗音乐播放器,完整源码地址在最下方,本文只列出部分源码,因为源码很多,全部贴
- 一、WebSocket简介WebSocket协议通过在客户端和服务端之间提供全双工通信来进行Web和服务器的交互功能。在WebSocket应
- 1、spring-cloud-starter-alibaba-nacos-discovery 这里依赖报红,无法引入,或显示无法找到,更换版
- Bean三种自定义初始化和销毁一. 三种方法概述在配置类中指定 @Bean(initMethod = “init&
- 一、前言我们经常会接触各种池化的技术或者概念,包括对象池、连接池、线程池等,池化技术最大的好处就是实现对象的重复利用,尤其是创建和使用大对象
- 本文实例讲述了C#中委托用法。分享给大家供大家参考。具体分析如下:这里演示了如何使用匿名委托来计算员工的薪水奖金。使用匿名委托简化了程序,因
- 在application.properties中填写中文信息,在读取该文件时会出现中文乱码问题。比如:application.propert
- 1.首先在 build.gradle 里导入包implementation 'com.github.PhilJay:MPAndroi
- 引言Java届很难得有读百十行代码就能增加修炼的机会,这里有一个。通常,我在看书的时候一般不写代码,因为我的脑袋被设定成单线程的,一旦同时喂
- 本文实例讲述了C#中ExecuteNonQuery()返回值注意点。对于C#数据库程序设计有一定的借鉴价值。分享给大家供大家参考之用。具体分
- DatagramSocket只允许数据报发送给指定的目标地址,而MulticastSocket可以将数据报以广播的方式发送至多个客户端。其主
- 导语相信大家无论是做前端还是做后端的,都被接口接口文档所折磨过,前端抱怨接口文档和后端给的不一致,后端抱怨写接口文档很麻烦,所以Swagge