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 = 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 的效果。  

 

出处: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 = 0; 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);

 

转载于:https://www.cnblogs.com/mq0036/p/10927610.html

C#中,`MessageBox`类没有提供直接的方法来实现弹后自动关闭的功能。不过,你可以通过结合使用`Timer`类来实现这个效果。以下是一个示例代码: ```csharp using System; using System.Windows.Forms; class Program { static void Main(string[] args) { // 创建一个Timer实例 Timer timer = new Timer(); // 设置定时器的间隔(单位:毫秒) timer.Interval = 3000; // 3秒钟 // 注册定时器的Tick事件处理程序 timer.Tick += Timer_Tick; // 显示MessageBox MessageBox.Show("这是一个自动关闭MessageBox"); // 启动定时器 timer.Start(); Application.Run(); } static void Timer_Tick(object sender, EventArgs e) { // 停止定时器 Timer timer = (Timer)sender; timer.Stop(); // 关闭MessageBox MessageBox.Close(); } } ``` 在上面的示例中,我们首先创建了一个`Timer`实例,并设置间隔为3秒钟。然后,在弹`MessageBox`之后,启动定时器。当定时器的Tick事件发生时,我们停止定时器,并通过`MessageBox.Close()`方法关闭`MessageBox`。 需要注意的是,为了使`Application.Run()`方法保持运行状态,我们使用了`System.Windows.Forms`命名空间。这样可以保持应用程序运行,直到手动关闭它。 请注意,在一些特定情况下,例如在控制台应用程序中使用`MessageBox`时,它可能会阻止应用程序继续执行,直到用户关闭`MessageBox`。在这种情况下,你可能需要考虑使用其他的UI组件或自定义对话框来实现自动关闭的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值