日期:2014-05-18 浏览次数:20863 次
[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 System.ComponentModel.Container component = null; protected override void Dispose( bool disposing ) { if( disposing ) { if (component != null) { component.Dispose(); } } base.Dispose( disposing ); } private void timer1_Tick(object sender, EventArgs e) { KillMessageBox(); //停止计时器 ((Timer)sender).Stop(); } private void StartKiller() { Timer timer = new Timer(); timer.Interval = 10000; //10秒启动 timer.Tick += new EventHandler(timer1_Tick); timer.Start(); } private void KillMessageBox() { //查找MessageBox的弹出窗口,注意对应标题 IntPtr ptr = FindWindow(null,"MessageBox"); if(ptr != IntPtr.Zero) { //查找到窗口则关闭 PostMessage(ptr,WM_CLOSE,IntPtr.Zero,IntPtr.Zero); } }