一:
我们都知道,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 = 10000; //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.Show()方法即可达到自动关闭 MessageBox 的效果。
转载:https://www.cnblogs.com/feiyuhuo/p/5110330.html
二:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace Wongoing.Basic { /// <summary> /// 自动超时消息提示框 /// </summary> public class MessageBoxTimeOut { /// <summary> /// 标题 /// </summary> private static string _caption; /// <summary> /// 显示消息框 /// </summary> /// <param name="text">消息内容</param> /// <param name="caption">标题</param> /// <param name="timeout">超时时间,单位:毫秒</param> public static void Show(string text, string caption, int timeout) { _caption = caption; StartTimer(timeout); MessageBox.Show(text, caption); } /// <summary> /// 显示消息框 /// </summary> /// <param name="text">消息内容</param> /// <param name="caption">标题</param> /// <param name="timeout">超时时间,单位:毫秒</param> /// <param name="buttons">消息框上的按钮</param> public static void Show(string text, string caption, int timeout, MessageBoxButtons buttons) { _caption = caption; StartTimer(timeout); MessageBox.Show(text, caption, buttons); } /// <summary> /// 显示消息框 /// </summary> /// <param name="text">消息内容</param> /// <param name="caption">标题</param> /// <param name="timeout">超时时间,单位:毫秒</param> /// <param name="buttons">消息框上的按钮</param> /// <param name="icon">消息框上的图标</param> public static void Show(string text, string caption, int timeout, MessageBoxButtons buttons, MessageBoxIcon icon) { _caption = caption; StartTimer(timeout); MessageBox.Show(text, caption, buttons, icon); } /// <summary> /// 显示消息框 /// </summary> /// <param name="owner">消息框所有者</param> /// <param name="text">消息内容</param> /// <param name="caption">标题</param> /// <param name="timeout">超时时间,单位:毫秒</param> public static void Show(IWin32Window owner, string text, string caption, int timeout) { _caption = caption; StartTimer(timeout); MessageBox.Show(owner, text, caption); } /// <summary> /// 显示消息框 /// </summary> /// <param name="owner">消息框所有者</param> /// <param name="text">消息内容</param> /// <param name="caption">标题</param> /// <param name="timeout">超时时间,单位:毫秒</param> /// <param name="buttons">消息框上的按钮</param> public static void Show(IWin32Window owner, string text, string caption, int timeout, MessageBoxButtons buttons) { _caption = caption; StartTimer(timeout); MessageBox.Show(owner, text, caption, buttons); } /// <summary> /// 显示消息框 /// </summary> /// <param name="owner">消息框所有者</param> /// <param name="text">消息内容</param> /// <param name="caption">标题</param> /// <param name="timeout">超时时间,单位:毫秒</param> /// <param name="buttons">消息框上的按钮</param> /// <param name="icon">消息框上的图标</param> public static void Show(IWin32Window owner, string text, string caption, int timeout, MessageBoxButtons buttons, MessageBoxIcon icon) { _caption = caption; StartTimer(timeout); MessageBox.Show(owner, text, caption, buttons, icon); } private static void StartTimer(int interval) { Timer timer = new Timer(); timer.Interval = interval; timer.Tick += new EventHandler(Timer_Tick); timer.Enabled = true; } private static void Timer_Tick(object sender, EventArgs e) { KillMessageBox(); //停止计时器 ((Timer)sender).Enabled = false; } [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)] private extern static IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", CharSet = CharSet.Auto)] private extern static int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); private const int WM_CLOSE = 0x10; private static void KillMessageBox() { //查找MessageBox的弹出窗口,注意对应标题 IntPtr ptr = FindWindow(null, _caption); if (ptr != IntPtr.Zero) { //查找到窗口则关闭 PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); } } } }
在需要的地方调用代码内的Show()方法,填写方法的不同参数实现相应功能。
转载:https://coding123.blog.csdn.net/article/details/106643568