winform 关于Messagebox自动定时关闭

简介: using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public class MessageBoxTimeOut
    {
        private string _caption;

        public void Show(string text, string caption)
        {
            Show(3000, text, caption);
        }
        public void Show(int timeout, string text, string caption)
        {
            Show(timeout, text, caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public void Show(int timeout, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            this._caption = caption;
            StartTimer(timeout);
            MessageBox.Show(text, caption, buttons, icon);
        }
        private void StartTimer(int interval)
        {
            Timer timer = new Timer();
            timer.Interval = interval;
            timer.Tick += new EventHandler(Timer_Tick);
            timer.Enabled = true;
        }
        private 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)]
        public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
        public const int WM_CLOSE = 0x10;
        private void KillMessageBox()
        {
            //查找MessageBox的弹出窗口,注意对应标题
            IntPtr ptr = FindWindow(null, this._caption);
            if (ptr != IntPtr.Zero)
            {
                //查找到窗口则关闭
                PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
            }
        }
    }
}
View Code

 

调用:

 MessageBoxTimeOut mb = new MessageBoxTimeOut();
 mb.Show("产品编码不存在!", "提示(窗体3秒后自动关闭...)");
View Code

 

目录
相关文章
|
C++
VC 定时自动关闭MessageBox弹出对话框 (2010-12-15 15:28:06)转载▼
标签: messagebox vc it 分类: VC/MFC void CTestDlg::OnOK() {  // TODO: 在此添加控件通知处理程序代码  SetTimer(NULL,2000,NULL);          //设置一个...
1422 0
|
安全
winform关闭窗口 取消关闭操作
医药遇到一个bug就是 关闭窗口 弹出后点击取消 程序还是结束了 于是百度了一下代码 粘贴上就可以取消 ,然后继续对程序的操作。
986 0
|
C++ Windows API
VC创建定时关闭的MessageBox
1、第一种方法:用微软提供的官方文档 From : http://support.microsoft.com/kb/181934/en-us/ Generally, when you want to display a message box for a limited amount ...
1033 0
|
JavaScript 前端开发
定时自动关闭弹出窗口
1.window对象的setTimeOut方法     window对象的setTimeOut方法用于延迟执行某一操作,其语法是:     SetTimeOut(express,secdelay[,language])     其中express是一个字符串,可包含热呵呵对函数、方法或者单个J...
776 0
Close2Tray将程序关闭到系统栏
经常使用一些大型软件,每次启动时间很长,但又不是时刻在用,那最好的办法是什么?是把它关闭了,下次重新启动吗?还是最小化到任务栏,让任务栏变得非常挤?来使用Close2Tray看看.把它放在系统栏.
|
JavaScript 前端开发
关闭IE窗口时执行事件
//关闭窗口时自动退出 function  window.onbeforeunload(){  if(event.clientX>360&&event.clientY
653 0
|
关系型数据库 Oracle
关闭peeking功能
链接: http://www.itpub.net/381890.html 如果实在很烦bind peeking ,没问题,Oracle提供了关掉该功能的方法 _optim_peek_user_binds show parameters peek NAME ...
613 0