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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM