WindowsForm实现警告消息框的实例代码
程序员文章站
2022-04-29 14:32:22
警告消息框主要是用来向用户户展示诸如警告、异常、完成和提示消息。一般实现的效果就是从系统窗口右下角弹出,然后加上些简单的显示和消失的动画。创建警告框窗口首先我们创建一个警告框窗口(form),将窗口设...
警告消息框主要是用来向用户户展示诸如警告、异常、完成和提示消息。一般实现的效果就是从系统窗口右下角弹出,然后加上些简单的显示和消失的动画。
创建警告框窗口
首先我们创建一个警告框窗口(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; } } }
以上就是windowsform实现警告消息框的实例代码的详细内容,更多关于windowsform实现警告消息框的资料请关注其它相关文章!
上一篇: 脆滑肉是哪个地方的菜?脆滑肉的种类有哪些?怎么样能让肉脆滑呢?
下一篇: 详细了解C# 枚举与位枚举