惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
J
Java Code Geeks
爱范儿
爱范儿
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
The GitHub Blog
The GitHub Blog
I
InfoQ
月光博客
月光博客
博客园 - 聂微东
博客园 - Franky
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
G
Google Developers Blog
Blog — PlanetScale
Blog — PlanetScale
L
LangChain Blog
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - ccczqh

Entity Framework对NULL值的处理 整理一下Entity Framework的查询 sqlhelper 使用事务实例 SqlHelper 类实现详细信息 SQL Server 2008R2中增加了新的智能 DateTime 数字型 C# tostring()汇总 DataGridView 列宽和行高自动调整的设定 datagridview,第一列前没有灰的一列 DataGridView取消默认选中行 dataGridView空白列,默认选中行的背景色,dataGridView中加入复选框勾选状态的更改 C# winform DataGridView 常见属性 C# dataGridView上下移动选中行 转ADO.NET Entity Framework 入门示例向导(附Demo程序下载) c#加密解密 大数据类型通过存储过程保存数据(clob,blob) C#简单游戏外挂制作(以Warcraft Ⅲ为例) [WebBrowser][Cookie] Document.Cookie 取得的Cookies不完整問題 C# DllImport的用法
WinForm 下实现一个自动关闭的MessageBox
ccczqh · 2012-07-31 · via 博客园 - ccczqh

WinForm 下实现一个自动关闭的MessageBox Author: eaglet WinForm 下我们可以调用MessageBox.Show 来显示一个消息对话框,提示用户确认等操作。在有些应用中我们需要通过程序来自动关闭这个消息对话框而不是由用户点击确认按钮来关闭。然而.Net framework 没有为我们提供自动关闭MessageBox 的方法,要实现这个功能,我们需要使用Window API 来完成。 首先我们需要找到这个消息对话框的窗口句柄,一个比较简单的方法就是用 FindWindow API 来查找对应的窗体句柄。 [DllImport("user32.dll", SetLastError =true)] staticextern IntPtr FindWindow(string lpClassName, string lpWindowName); 这个API调用可以通过窗口的类名或者窗口标题的名字来查找窗口句柄。 接下来我们还需要找到一个 API 来关闭对话框,这里我使用 EndDialog [DllImport("user32.dll")] staticexternbool EndDialog(IntPtr hDlg, out IntPtr nResult); 有了这两个API函数,我们就可以来关闭消息对话框了。思路是在调用MessageBox.Show 前启动一个后台工作线程,这个工作线程等待一定时间后开始查找消息对话框的窗口句柄,找到后调用EndDialog API 函数关闭这个消息对话框。不过这个方法有个问题,就是如果同时又多个同名的消息对话框(可能不一定是这个应用的),这样做可能会关错窗口,如何解决这个问 题,我还没有想出比较好的方法,如果大家有更好的方法解决这个问题,不妨一起讨论讨论。 我根据这个思路编写了延时关闭消息对话框的函数 publicvoid ShowMessageBoxTimeout(string text, string caption, MessageBoxButtons buttons, int timeout) { ThreadPool.QueueUserWorkItem(new WaitCallback(CloseMessageBox), new CloseState(caption, timeout)); MessageBox.Show(text, caption,buttons); } 这个函数中timeout 参数单位是毫秒,其他参数和MessageBox.Show的参数含义是一样的,这里不再详细说明。 这个函数中首先利用线程池调用一个工作线程 CloseMessageBox ,并将对话框的标题和延时时间通过CloseState这个类传递给CloseMessageBox函数。 CloseState 的定义如下: privateclass CloseState { privateint _Timeout; ///

/// In millisecond ///

publicint Timeout { get { return _Timeout; } } privatestring _Caption; ///

/// Caption of dialog ///

publicstring Caption { get { return _Caption; } } public CloseState(string caption, int timeout) { _Timeout = timeout; _Caption = caption; } } 最后就是CloseMessageBox函数了,直接看代码吧 privatevoid CloseMessageBox(object state) { CloseState closeState = state as CloseState; Thread.Sleep(closeState.Timeout); IntPtr dlg = FindWindow(null, closeState.Caption); if (dlg != IntPtr.Zero) { IntPtr result; EndDialog(dlg, out result); } }