自动关闭的 MessageBox

/// <summary>
/// 自动关闭Messbox
/// </summary>
public class MessageBoxAutoClose
{
    System.Threading.Timer _timeoutTimer;
    string _caption;
    public MessageBoxAutoClose(string text, string caption, int timeout)
    {
        _caption = caption;
        _timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
            null, timeout, System.Threading.Timeout.Infinite);
        MessageBox.Show(text, caption);
    }
    public MessageBoxAutoClose(string text, string caption, MessageBoxButtons msgButton, int timeout)
    {
        _caption = caption;
        _timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
            null, timeout, System.Threading.Timeout.Infinite);
        MessageBox.Show(text, caption, msgButton);
    }
    public MessageBoxAutoClose(string text, string caption, MessageBoxButtons msgButton, MessageBoxIcon msgIcon, int timeout)
    {
        _caption = caption;
        _timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
            null, timeout, System.Threading.Timeout.Infinite);
        MessageBox.Show(text, caption, msgButton, msgIcon);
    }
    /// <summary>
    /// 提示关闭
    /// </summary>
    /// <param name="text">显示提示内容</param>
    /// <param name="caption">标题</param>
    /// <param name="timeout">设定指定毫秒后关闭</param>
    public static void Show(string text, string caption, int timeout = 2000)
    {
        new MessageBoxAutoClose(text, caption, timeout);
    }
    /// <summary>
    /// 提示关闭
    /// </summary>
    /// <param name="text">显示提示内容</param>
    /// <param name="caption">标题</param>
    /// <param name="msgButton">提示按钮</param>
    /// <param name="timeout">设定指定毫秒后关闭</param>
    public static void Show(string text, string caption, MessageBoxButtons msgButton, int timeout = 2000)
    {
        new MessageBoxAutoClose(text, caption, msgButton, timeout);
    }
    /// <summary>
    /// 提示关闭
    /// </summary>
    /// <param name="text">显示提示内容</param>
    /// <param name="caption">标题</param>
    /// <param name="msgButton">提示按钮</param>
    /// <param name="msgIcon">提示图标</param>
    /// <param name="timeout">设定指定毫秒后关闭</param>
    public static void Show(string text, string caption, MessageBoxButtons msgButton, MessageBoxIcon msgIcon, int timeout = 2000)
    {
        new MessageBoxAutoClose(text, caption, msgButton, msgIcon, timeout);
    }
    void OnTimerElapsed(object state)
    {
        IntPtr mbWnd = FindWindow(null, _caption);
        if (mbWnd != IntPtr.Zero)
            SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
        _timeoutTimer.Dispose();
    }
    const int WM_CLOSE = 0x0010;
    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}


知识共享许可协议
《自动关闭的 MessageBox》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     152.19ms