using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace JY.Inspection { public class MessageBoxTimeOut { private string _caption; //public void Alert(string msg, FrmAlert.enmType type) //{ // FrmAlert frm = new FrmAlert(); // frm.ShowAlert(msg, type); //} public void Show(string text, FrmAlert.enmType type) { this._caption = "信息提示"; StartTimer(3000); //Alert(text, type); MessageBox.Show(text,"信息提示"); } 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); } } } }