C#在Winform中实现弹出一个消息窗口过3秒自动关闭

C#在Winform中实现弹出一个消息窗口过3秒自动关闭

  • 背景
  • 代码实现
  • 调用示例

背景

在Winform应用程序开发中,很多时候我们需要弹出一个消息提示(模式的),又希望不用自己手动去关闭,在这种场景下我们可以开发一个超时消息提示框类,然后进行调用。

代码实现

具体代码封装如下:
MessageBoxTimeout.cs

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);
            }
        }
    }
}


调用示例

简单的调用示例如下:

MessageBoxTimeOut.Show("你好,我是超时消息框,3秒后自动关闭!","提示", 3000);

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值