我们都知道,MessageBox弹出的窗口是模式窗口,模式窗口会自动阻塞父线程的。所以如果有以下代码:

  1. MessageBox.Show("内容',"标题");

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

核心代码如下:

  1. [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet=CharSet.Auto)]
  2. private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
  3.  
  4. [DllImport("user32.dll", CharSet=CharSet.Auto)]
  5. public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
  6.  
  7. public const int WM_CLOSE = 0x10;
  8.  
  9. private void StartKiller()
  10. {
  11. Timer timer = new Timer();
  12. timer.Interval = ; //10秒启动
  13. timer.Tick += new EventHandler(Timer_Tick);
  14. timer.Start();
  15. }
  16.  
  17. private void Timer_Tick(object sender, EventArgs e)
  18. {
  19. KillMessageBox();
  20. //停止计时器
  21. ((Timer)sender).Stop();
  22. }
  23.  
  24. private void KillMessageBox()
  25. {
  26. //查找MessageBox的弹出窗口,注意MessageBox对应的标题
  27. IntPtr ptr = FindWindow(null,"标题");
  28. if(ptr != IntPtr.Zero)
  29. {
  30. //查找到窗口则关闭
  31. PostMessage(ptr,WM_CLOSE,IntPtr.Zero,IntPtr.Zero);
  32. }
  33. }

在需要的地方调用 StartKiller 方法即可达到自动关闭 MessageBox 的效果。

出处:https://www.cnblogs.com/feiyuhuo/p/5110330.html

=======================================================================

经过测试,这个方法没办法关闭下面的代码弹出窗口:

new ViewMessage().ShowDialog(this);   //我弹出的模态窗口,并且指定主窗口的类为this

我是通过下面的代码获取窗口并关闭

  1. private void ExitAllForm()
  2. {
  3. var fs = System.Windows.Forms.Application.OpenForms;
  4. for (int i = ; i < fs.Count; i++)
  5. {
  6. //........逻辑代码
  7. KillForm(fs[i].Text);
  8. }
  9. }
  10.  
  11. private void KillForm(string strTitle)
  12. {
  13. IntPtr ptr = Common.WindowsAPI.FindWindow(null, strTitle);
  14. if (ptr != IntPtr.Zero)
  15. {
  16. Common.WindowsAPI.PostMessage(ptr, 0x10, IntPtr.Zero, IntPtr.Zero);
  17. }
  18. }
  19.  
  20. /// <summary>
  21. /// 获取指定标题窗口的句柄
  22. /// </summary>
  23. /// <param name="lpClassName"></param>
  24. /// <param name="lpWindowName"></param>
  25. /// <returns></returns>
  26. [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
  27. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  28.  
  29. /// <summary>
  30. /// 向指定窗口句柄发送消息
  31. /// </summary>
  32. /// <param name="hWnd"></param>
  33. /// <param name="msg"></param>
  34. /// <param name="wParam"></param>
  35. /// <param name="lParam"></param>
  36. /// <returns></returns>
  37. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  38. public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

c# 定时关闭 MessageBox 或弹出的模态窗口的更多相关文章

  1. js在关闭页面前弹出确认提示【转载】

    最近项目中出现个bug,就是导出数据后,会提示确认导航,其实实际需求并不需要这个提示,可能是之前遗留的问题.查了下资料是在触发了onbeforeunload事件,那么剩下的就是代码组织问题了. 众所周 ...

  2. mac关闭渐隐和弹出动画效果

    苹果系统应用程序的窗口和对话框每次使用的时候都有华丽的特效,但是如果你感觉这种特效显得有点慢(MacGG闲的蛋疼),那该如何取消掉他呢? 方法很简单,打开"终端"(Finder-& ...

  3. [Selenium]通过Selenium实现在当前浏览器窗口点击一个图标之后,弹出另外一个窗口,关闭这个窗口,再回到原来的窗口进行操作

    public void clickReportIcon(){ String initialWindowHandle = driver.getWindowHandle(); //保存原始的浏览器窗口 p ...

  4. php弹出式登录窗口并获得登录后返回值

    一款bootstrap样式结合php制作的弹出式登录窗口,输入用户名和密码后,ajax传参给后台,并获得登录后返回值. hwLayer+ajax弹出登录框 $(function() { $('#for ...

  5. AOPR弹出Order Now窗口怎么办

    当我们忘记了我们自己设置的office密码的时候,需要一款office密码破解软件来帮我们破解,Advanced Office Password Recovery就是这样的一款软件,其简称AOPR.试 ...

  6. 自定义HttpModule,用于未登录用户,不弹出Windows认证窗口,而是跳转回SSO站点

    2012年的一篇随笔记录,可以学习到如何自定义HttpModule,而具体里面针对需求开发的代码,可能未必能让大伙了解到什么,可快速扫描而过. using System; using System.W ...

  7. QT QDialog如何弹出一个子窗口

    1. 假设已有一个QDialog的父窗口, 想弹出的子窗口为自己实现的myDialog : QDialog. myDialog 设计和平常的QDialog一样, childDialog : publi ...

  8. PyQt(Python+Qt)学习随笔:在一个窗口点击按钮弹出另一个窗口的实现方法及注意事项

    在Qt Designer中定义了两个窗口,一个主窗口一个弹出窗口,需要实现在主窗口点击一个按钮时弹出弹出窗口. 经老猿验证: 1.弹窗的窗口类型无特殊要求,只要是QWidget等窗口部件就可以,也可以 ...

  9. MessageBox:弹出窗口

    Ext.onReady(function () { Ext.MessageBox.alert("提示信息!","Hello World!"); }); Ext, ...

随机推荐

  1. rabbitmq设置消息优先级、队列优先级配置

    1.首先在consume之前声明队列的时候,要加上x-max-priority属性,一般为0-255,大于255出错  -----配置队列优先级 配置成功后rabbitmq显示: 2.在向exchan ...

  2. Java内存分析工具

    内存分析工具 IDEA插件(VisualVM Launcher) 执行main函数的时候,同时启动jvisualvm,实时查看资源消耗情况.如图效果: Eclipse Memory Analyzer ...

  3. 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 ...

  4. Consul服务告警之Watch机制

    熔断保护在Consul和Ocelot中都有实现,意思就是当一个服务不正常时(比如我们的一个服务实例挂了,Consul的健康检查机制检测到了),应该给系统维护人员给以告警.在Consul中,服务告警也是 ...

  5. 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'] ...

  6. 设置程序崩溃时产生 core 文件的配置

    /* 不限制 core 文件的大小 */ ulimit -c unlimited /* 使用 pid 进行命名 */ echo " > /proc/sys/kernel/core_us ...

  7. python is 和 == 区别(8)

    在python中is和==都说常用的运算符之一,主要用于检测两个变量是否相等,返回True或者False,具体区别在哪呢? 一.前言 在讲解is和==区别直接先讲解一下内置函数id(),其实在文章 p ...

  8. Windows常用命令的使用

    3.Tracert Tracert命令用来显示数据包到达目标主机所经过的路径,并显示到达每个节点的时间.该诊断实用程序将包含不同生存时间 (TTL) 值的 Internet 控制消息协议 (ICMP) ...

  9. LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)

    1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...

  10. # Java类链接模型

    动态链接和解析 每一个class都有一个常量池, 保存它自己的所有的符号引用. 每一个已经被加载的class, interface都另外有一个内部版本的常量池, 叫做runtime constant ...