winform 实现MessageBox 定时消失
2018-03-13 15:25
399 查看
//定时关闭的 MessageBox public class MessageBoxTimeOut { //MessageBox 标题 private string _caption; /// <summary> /// MessageBox.Show()重写 /// </summary>MessageBox.Show()具有多达20多个重载方法,可以按照具体需求进行重载(加入timeout 参数) /// <param name="text">显示内容</param> /// <param name="caption">标题</param> /// <param name="timeout">显示时间(秒)</param> public void Show(string text, string caption, int timeout) { this._caption = caption; StartTimer(timeout); MessageBox.Show(text, caption); } /// <summary> /// 定时器 /// </summary> /// <param name="interval"></param> private void StartTimer(int interval) { Timer timer = new Timer(); timer.Interval = interval*1000; timer.Tick += new EventHandler(Timer_Tick); timer.Enabled = true; } /// <summary> /// 定时器执行方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Timer_Tick(object sender, EventArgs e) { KillMessageBox(); //停止计时器 ((Timer)sender).Enabled = false; } //注入user32.dll 引用FindWindow方法 [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)] private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); //注入user32.dll 引用PostMessage方法 [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); public const int WM_CLOSE = 0x10; /// <summary> /// 关闭窗口 /// </summary> private void KillMessageBox() { //查找MessageBox的弹出窗口,注意对应标题 IntPtr ptr = FindWindow(null, this._caption); if (ptr != IntPtr.Zero) { //查找到窗口则关闭 PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); } } }引用:1:初始化 //定时消失的MessageBox public MessageBoxTimeOut _MessageBoxTimeOut = new MessageBoxTimeOut();2:调用 _MessageBoxTimeOut.Show("请先(扫码或密码)登录支付宝!", "提示信息",2);
相关文章推荐
- 未公开函数MessageBoxTimeOut 实现定时消息(ZT) MFC实现MessageBox自动消失
- WinForm 下实现一个自动关闭的MessageBox
- Bootstrap的popover(弹出框)2秒后定时消失的实现代码
- (转)WinForm 下实现一个自动关闭的MessageBox
- WinForm中实现自动关闭的MessageBox(CSharp)
- WinForm 下实现一个自动关闭的MessageBox
- WinForm 下实现一个自动关闭的MessageBox
- WinForm 下实现一个自动关闭的MessageBox
- winform 关于Messagebox自动定时关闭和自定义提示框总按钮上文本的问题的整理
- C#实现winform自动关闭MessageBox对话框的方法
- WinForm中使MessageBox实现可以自动关闭功能
- QT 实现弹出提示框定时消失
- WinForm 下实现一个自动关闭的MessageBox
- Android编程实现popupwindow定时消失的方法
- wince/WinForm下实现一个自动关闭的MessageBox
- C# WinForm中实现MessageBox.Show()的常用方法
- WinForm 下实现一个自动关闭的MessageBox
- WinForm 下实现一个自动关闭的MessageBox
- WinForm 下实现一个自动关闭的MessageBox
- wince/WinForm下实现一个自动关闭的MessageBox