c# 定时关闭 MessageBox 或弹出的模态窗口
我们都知道,MessageBox弹出的窗口是模式窗口,模式窗口会自动阻塞父线程的。所以如果有以下代码:
- MessageBox.Show("内容',"标题");
则只有关闭了MessageBox的窗口后才会运行下面的代码。而在某些场合下,我们又需要在一定时间内如果在用户还没有关闭窗口时能自动关闭掉窗口而避免程序一直停留不前。这样的话我们怎么做呢?上面也说了,MessageBox弹出的模式窗口会先阻塞掉它的父级线程。所以我们可以考虑在MessageBox前先增加一个用于“杀”掉MessageBox窗口的线程。因为需要在规定时间内“杀”掉窗口,所以我们可以直接考虑使用Timer类,然后调用系统API关闭窗口。
核心代码如下:

- [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet=CharSet.Auto)]
- private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
- [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;
- private void StartKiller()
- {
- Timer timer = new Timer();
- timer.Interval = ; //10秒启动
- timer.Tick += new EventHandler(Timer_Tick);
- timer.Start();
- }
- private void Timer_Tick(object sender, EventArgs e)
- {
- KillMessageBox();
- //停止计时器
- ((Timer)sender).Stop();
- }
- private void KillMessageBox()
- {
- //查找MessageBox的弹出窗口,注意MessageBox对应的标题
- IntPtr ptr = FindWindow(null,"标题");
- if(ptr != IntPtr.Zero)
- {
- //查找到窗口则关闭
- PostMessage(ptr,WM_CLOSE,IntPtr.Zero,IntPtr.Zero);
- }
- }

在需要的地方调用 StartKiller 方法即可达到自动关闭 MessageBox 的效果。
出处:https://www.cnblogs.com/feiyuhuo/p/5110330.html
=======================================================================
经过测试,这个方法没办法关闭下面的代码弹出窗口:
new ViewMessage().ShowDialog(this); //我弹出的模态窗口,并且指定主窗口的类为this
我是通过下面的代码获取窗口并关闭
- private void ExitAllForm()
- {
- var fs = System.Windows.Forms.Application.OpenForms;
- for (int i = ; i < fs.Count; i++)
- {
- //........逻辑代码
- KillForm(fs[i].Text);
- }
- }
- private void KillForm(string strTitle)
- {
- IntPtr ptr = Common.WindowsAPI.FindWindow(null, strTitle);
- if (ptr != IntPtr.Zero)
- {
- Common.WindowsAPI.PostMessage(ptr, 0x10, IntPtr.Zero, IntPtr.Zero);
- }
- }
- /// <summary>
- /// 获取指定标题窗口的句柄
- /// </summary>
- /// <param name="lpClassName"></param>
- /// <param name="lpWindowName"></param>
- /// <returns></returns>
- [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
- public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- /// <summary>
- /// 向指定窗口句柄发送消息
- /// </summary>
- /// <param name="hWnd"></param>
- /// <param name="msg"></param>
- /// <param name="wParam"></param>
- /// <param name="lParam"></param>
- /// <returns></returns>
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
c# 定时关闭 MessageBox 或弹出的模态窗口的更多相关文章
- js在关闭页面前弹出确认提示【转载】
最近项目中出现个bug,就是导出数据后,会提示确认导航,其实实际需求并不需要这个提示,可能是之前遗留的问题.查了下资料是在触发了onbeforeunload事件,那么剩下的就是代码组织问题了. 众所周 ...
- mac关闭渐隐和弹出动画效果
苹果系统应用程序的窗口和对话框每次使用的时候都有华丽的特效,但是如果你感觉这种特效显得有点慢(MacGG闲的蛋疼),那该如何取消掉他呢? 方法很简单,打开"终端"(Finder-& ...
- [Selenium]通过Selenium实现在当前浏览器窗口点击一个图标之后,弹出另外一个窗口,关闭这个窗口,再回到原来的窗口进行操作
public void clickReportIcon(){ String initialWindowHandle = driver.getWindowHandle(); //保存原始的浏览器窗口 p ...
- php弹出式登录窗口并获得登录后返回值
一款bootstrap样式结合php制作的弹出式登录窗口,输入用户名和密码后,ajax传参给后台,并获得登录后返回值. hwLayer+ajax弹出登录框 $(function() { $('#for ...
- AOPR弹出Order Now窗口怎么办
当我们忘记了我们自己设置的office密码的时候,需要一款office密码破解软件来帮我们破解,Advanced Office Password Recovery就是这样的一款软件,其简称AOPR.试 ...
- 自定义HttpModule,用于未登录用户,不弹出Windows认证窗口,而是跳转回SSO站点
2012年的一篇随笔记录,可以学习到如何自定义HttpModule,而具体里面针对需求开发的代码,可能未必能让大伙了解到什么,可快速扫描而过. using System; using System.W ...
- QT QDialog如何弹出一个子窗口
1. 假设已有一个QDialog的父窗口, 想弹出的子窗口为自己实现的myDialog : QDialog. myDialog 设计和平常的QDialog一样, childDialog : publi ...
- PyQt(Python+Qt)学习随笔:在一个窗口点击按钮弹出另一个窗口的实现方法及注意事项
在Qt Designer中定义了两个窗口,一个主窗口一个弹出窗口,需要实现在主窗口点击一个按钮时弹出弹出窗口. 经老猿验证: 1.弹窗的窗口类型无特殊要求,只要是QWidget等窗口部件就可以,也可以 ...
- MessageBox:弹出窗口
Ext.onReady(function () { Ext.MessageBox.alert("提示信息!","Hello World!"); }); Ext, ...
随机推荐
- rabbitmq设置消息优先级、队列优先级配置
1.首先在consume之前声明队列的时候,要加上x-max-priority属性,一般为0-255,大于255出错 -----配置队列优先级 配置成功后rabbitmq显示: 2.在向exchan ...
- Java内存分析工具
内存分析工具 IDEA插件(VisualVM Launcher) 执行main函数的时候,同时启动jvisualvm,实时查看资源消耗情况.如图效果: Eclipse Memory Analyzer ...
- Python 安装 MySQL-python ImportError: No module named 'ConfigParser'
系统: CentOS-6.4-x86_64 Python : Python 3.4.5 和 Python 3.5.2 安装 MySQL-python ,结果出错: ImportError: No mo ...
- Consul服务告警之Watch机制
熔断保护在Consul和Ocelot中都有实现,意思就是当一个服务不正常时(比如我们的一个服务实例挂了,Consul的健康检查机制检测到了),应该给系统维护人员给以告警.在Consul中,服务告警也是 ...
- nuxt/eapress 安装报错Module build failed: ValidationError: PostCSS Loader Invalid OptionsModule build failed: ValidationError: PostCSS Loader Invalid Options options['useConfigFile'] is an invalid additi
错误信息: Module build failed: ValidationError: PostCSS Loader Invalid Options options['useConfigFile'] ...
- 设置程序崩溃时产生 core 文件的配置
/* 不限制 core 文件的大小 */ ulimit -c unlimited /* 使用 pid 进行命名 */ echo " > /proc/sys/kernel/core_us ...
- python is 和 == 区别(8)
在python中is和==都说常用的运算符之一,主要用于检测两个变量是否相等,返回True或者False,具体区别在哪呢? 一.前言 在讲解is和==区别直接先讲解一下内置函数id(),其实在文章 p ...
- Windows常用命令的使用
3.Tracert Tracert命令用来显示数据包到达目标主机所经过的路径,并显示到达每个节点的时间.该诊断实用程序将包含不同生存时间 (TTL) 值的 Internet 控制消息协议 (ICMP) ...
- LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)
1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...
- # Java类链接模型
动态链接和解析 每一个class都有一个常量池, 保存它自己的所有的符号引用. 每一个已经被加载的class, interface都另外有一个内部版本的常量池, 叫做runtime constant ...