您的位置:首页 > 其它

winform 实现MessageBox 定时消失

2018-03-13 15:25 399 查看
 //定时关闭的 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);
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  winform