Winform自定义MessageBox
在使用Winform的MessageBox作为消息提示时,会有一些不方便的地方。如,MessageBox消息框弹出之后,如果不关闭就会阻塞函数的继续执行;不能自动关闭等。因此,子丰就自己仿照MessageBox的样式写了一个InformationBox提示框,可以实现自动关闭、永远显示在所有窗口的最上方等。首先,贴出几张图片展示一下InformationBox的效果;然后,具体介绍InformationBox的实现方式。
程序下载:点击打开链接
http://download.csdn.net/detail/softimite_zifeng/9731930
1. InformationBox增加了自动关闭功能。消息框的右下角会进行倒计时,倒计时时间可以根据自己的需要设定。倒计到0时,消息框自动关闭。当然,在倒计时的过程中,单击“确定”按钮或窗口关闭按钮,同样能够关闭消息框。
2. InformationBox对“确定”按钮设置了快捷键Ctrl+O。也就是说只需要按下Ctrl+O就相当于单击了“确定”按钮。
3. InformationBox的倒计时实现暂停和开始。只需要双击右下角的暂停或开始按钮即可。这样方便用户有足够的时间对较长或较复杂的提示消息进行查看。
4. 和MessageBox一样,InformationBox可以更改消息框显示的图标,包括消息、错误、询问、警告这4种图标。并 且根据不同的图标,在消息框弹出时,其提示音也不同。
5. InformationBox能够根据标题和内容的长度的增大,自动改变提示框的宽度;并且当内容的长度增大一定程度时,能够实现自动转行。基本做到了与MessageBox无差别。
下面具体介绍一下InformationBox的实现方式:
1. 新建Windows窗口,进行如下的设计。
2. 实现“确定”按钮的快捷方式Ctrl+O。只需要给窗口在的KeyPress事件中编写即可,注意,这里需要将窗口的KeyPreview属性设置为True,否则,无法检测到键盘按下Ctrl+O。
//快捷键
private void InformationBox_KeyPress(object sender, KeyPressEventArgs e)
{e.Handled = true;if (e.KeyChar == 15) //确定:Ctrl + O{OKButton.PerformClick();}
}
3. 定义一个枚举(InformationBoxIcon)用于图标的选择。
//图标
public enum InformationBoxIcon
{Error = 0, //错误Information = 1, //消息Question = 2, //询问Warning = 3 //警告
}
4. 使用线程自动关闭提示框,定义线程函数autoCloseThread。
//自动关闭线程
private void autoCloseThread(object timeObj)
{try{int time = 10 * 5; //默认时间try{time = (int)timeObj * 5;}catch{time = 10 * 5;}while (time > 0){if(!pause){Thread.Sleep(200);time--;//更新倒计时if (time % 5 == 0){TimeLab2.Invoke((EventHandler)(delegate{TimeLab2.Text = (time / 5).ToString();}));}}}//Thread.Sleep(1000);TimeLab3.Invoke((EventHandler)(delegate{TimeLab1.Visible = false;TimeLab2.Visible = false;TimeLab3.Visible = false;TimeLab4.Visible = true;}));Thread.Sleep(500);this.Invoke((EventHandler)(delegate{autoclose = true; //自动关闭this.Close();}));}catch { }
}
5. 重写窗口InformationBox的Show函数,实现窗口的布局以及自动关闭线程。
//显示
public void Show(IWin32Window owner, string text = "", string caption = "", InformationBoxIcon icon = InformationBoxIcon.Information, int secondsTimeout = 10)
{if (text == null)text = "";if (caption == null)caption = "";if (secondsTimeout < 2) //最小倒计时为2secondsTimeout = 2;if (secondsTimeout > 99) //最大倒计时为99secondsTimeout = 99;//提示框的宽和高testLab.Text = caption; //标题长度比较int width = 206;width = (testLab.Width + 50 > width) ? testLab.Width + 50 : width; //最小宽度为206testLab.Text = "";this.Text = caption;width = (width < 498) ? width : 498; //最大宽度为498InformationTxt.Text = text;width = (498 - 380 + InformationTxt.Width > width) ? 498 - 380 + InformationTxt.Width : width; //内容长度比较。这里不需要比较最大宽度,由于InformationTxt设置了最大宽度390int height = 188;if (InformationTxt.Height / 17 > 2) //最小高度为188{height += 17 * (InformationTxt.Height / 17 - 2); //内容高度比较}this.Size = new System.Drawing.Size(width, height); //重绘提示框大小//倒计时TimeLab2.Text = secondsTimeout.ToString();//按钮提示OKTip.SetToolTip(OKButton, OKButton.Text + " 快捷键: Ctrl+O"); //Ctrl+O:15PauseTip.SetToolTip(PausePb, "双击暂停倒计时");//图标,提示音if(icon == InformationBoxIcon.Error){IconPb.Image = Properties.Resources.ErrorIcon;System.Media.SystemSounds.Hand.Play();}else if (icon == InformationBoxIcon.Information){IconPb.Image = Properties.Resources.InformationIcon;System.Media.SystemSounds.Asterisk.Play();}else if (icon == InformationBoxIcon.Question){IconPb.Image = Properties.Resources.QuestionIcon;System.Media.SystemSounds.Beep.Play();}else if (icon == InformationBoxIcon.Warning){IconPb.Image = Properties.Resources.WarningIcon;System.Media.SystemSounds.Exclamation.Play();}//显示在所有窗体的最上方this.TopMost = true;//弹出提示框base.Show(owner);//开启倒计时timeoutThread = new Thread(autoCloseThread);timeoutThread.IsBackground = true;timeoutThread.Start(secondsTimeout);
}
6. 实现双击暂停或开始倒计时。
//双击暂停或开始
private void pausePb_DoubleClick(object sender, EventArgs e)
{//判断当前状态是否暂停if (pause){pause = false;PausePb.Image = Properties.Resources.PauseIcon;PauseTip.RemoveAll();PauseTip.SetToolTip(PausePb, "双击暂停倒计时");}else{pause = true;PausePb.Image = Properties.Resources.StartIcon;PauseTip.RemoveAll();PauseTip.SetToolTip(PausePb, "双击开始倒计时");}
}
7. 编写窗口的FormClosing事件。当倒计时还没有结束时关闭窗口,就将自动关闭窗口线程杀死。
//关闭的操作
private void InformationBox_FormClosing(object sender, FormClosingEventArgs e)
{if (timeoutThread != null){if (timeoutThread.IsAlive && !autoclose){try{timeoutThread.Abort();}catch { }}}this.Dispose();
}
Winform自定义MessageBox相关推荐
- [转] WinForm自定义函数FindControl实现按名称查找控件
原文地址 WinForm自定义函数FindControl实现按名称查找控件 本文所述实例实现WinForm自定义函数FindControl实现按名称查找控件的功能,在C#程序开发中有一定的实用价值. ...
- WPF 自定义 MessageBox (相对完善版 v1.0.0.6)
基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当你不得不弹出一个消息框通知用户消息时(虽然很不建议在程序中频繁 ...
- 解决winform自定义窗体在扩展显示器最大化不能充满屏幕
参考:(60条消息) winForm在多显示器(主显示器 + 扩展显示器)上显示最大化和还原操作._梦的远方~的博客-CSDN博客 原因: 我开发用笔记本,缩放设置为150%:同时外接一个扩展屏,缩放 ...
- C# Winform 自定义 日程日历控件
C# Winform 自定义 日程日历控件 下载地址: https://download.csdn.net/download/dilidida/33627515
- C#自定义MessageBox 按钮的Text
运行效果: 代码: using System; using System.Drawing; using System.Runtime.InteropServices; using System.Tex ...
- miniui自定义messageBox框
1miniui的MessageBox官网 2miniui自定义messageBox框 1.miniui的MessageBox官网 http://www.miniui.com/demo/#src=mes ...
- C#WinFormMessageBox(C#),XtraMessageBox(devexpress),自定义MessageBox
1.自定义MessageBox C#自定义控件之-自定义MessageBox_微尘物联-联通无限-CSDN博客_c# 自定义messagebox var dr= new MessageBoxEx.Me ...
- PyQt自定义MessageBox
本来想直接继承QMessageBox,但是继承之后窗口的大小.stylesheet什么的都不太好搞定,于是重新做了一个,参考:QT自定义messagebox弹窗,使用PyQt重写 布局: 自动生成代码 ...
- winform 关于Messagebox自动定时关闭和自定义提示框总按钮上文本的问题的整理
如果要改变Messagebox上按钮的文本和自动关闭Messagebox提示框,一种方法是自定义一个winform窗口模仿替代Messagebox,变通的实现效果,另一种方法是通过调用系统的API来实 ...
最新文章
- linked-list-cycle-ii (数学证明)
- 018_SpringBoot异常处理方式-ExceptionHandle注解处理异常
- 北斗导航 | 多模多频实时GNSS软件接收机
- 《JavaScript高级程序设计》阅读笔记(七):ECMAScript中的语句
- Java11 新特性
- Python操作Redis(转)
- UVA10733 The Colored Cubes【Polya定理】
- sql2005下载地址
- 如何在路由上封掉某个ip或某个断口
- 2006年最一针见血的199句话!经典
- Win10操作系统下,如何确定电脑位数(图文教程)
- docset文件,aviary sdk,一个bug
- Clickhouse的 MySQL ELT和interval 功能等同实现 roundDown
- php gd 坐标,【PHP】GD库笔记 初探GD库的坐标
- nn.Squential与nn.ModuleList区别
- Win10邮箱管理QQ邮箱+163邮箱
- k/3 Cloud如何从菜鸟成长一名合格的ERP开发工程师
- 回归预测 | MATLAB实现MPR多元多项式回归
- STM32夺命100问,你知道几个?
- SpringBoot中业务层标准开发和快速开发(大全)
热门文章
- 一个简单的游戏源代码
- 中文写程序,何陋之有?
- 记录一次扇贝网前端实习面试
- 全新UI任务悬赏抢单源码-附带简单安装教程
- 【bzoj1123】[POI2008]BLO
- Gradle build failed to produce an .apk file. It‘s likely that this file was generated under XXX
- C/C++文件输入输出(详细介绍)
- C语言程序设计(第三版)
- 用python读取股票基本信息_python-使用pandas datareader获取股票信息
- 同步手绘板——PC端实现画板