winform 实现MessageBox 定时消失

 
     
 //定时关闭的 MessageBox     public class MessageBoxTimeOut     {         //MessageBox 标题         private string _caption;
        /// <summary>         /// MessageBox.Show()重写         /// </summary>MessageBox.Show()具有多达20多个重载方法,可以按照具体需求进行重载(加入timeout 参数)         /// <param name="text">显示内容</param>         /// <param name="caption">标题</param>         /// <param name="timeout">显示时间(秒)</param>         public void Show(string text, string caption, int timeout)         {             this._caption = caption;             StartTimer(timeout);             MessageBox.Show(text, caption);         }
        /// <summary>         /// 定时器         /// </summary>         /// <param name="interval"></param>         private void StartTimer(int interval)         {             Timer timer = new Timer();             timer.Interval = interval*1000;             timer.Tick += new EventHandler(Timer_Tick);             timer.Enabled = true;         }
        /// <summary>         /// 定时器执行方法         /// </summary>         /// <param name="sender"></param>         /// <param name="e"></param>         private void Timer_Tick(object sender, EventArgs e)         {             KillMessageBox();             //停止计时器               ((Timer)sender).Enabled = false;         }
        //注入user32.dll 引用FindWindow方法         [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]         private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
        //注入user32.dll 引用PostMessage方法         [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;
        /// <summary>         /// 关闭窗口         /// </summary>         private void KillMessageBox()         {             //查找MessageBox的弹出窗口,注意对应标题               IntPtr ptr = FindWindow(null, this._caption);             if (ptr != IntPtr.Zero)             {                 //查找到窗口则关闭                   PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);             }         }     }

引用:
1:初始化

  //定时消失的MessageBox
        public MessageBoxTimeOut _MessageBoxTimeOut = new MessageBoxTimeOut();

2:调用
     _MessageBoxTimeOut.Show("请先(扫码或密码)登录支付宝!", "提示信息",2);
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值