当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现winform自动关闭MessageBox对话框的方法

C#实现winform自动关闭MessageBox对话框的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了c#实现winform自动关闭messagebox对话框的方法。分享给大家供大家参考。具体实现方法如下: using system; using

本文实例讲述了c#实现winform自动关闭messagebox对话框的方法。分享给大家供大家参考。具体实现方法如下:

using system; 
using system.collections.generic; 
using system.componentmodel; 
using system.data; 
using system.drawing; 
using system.text; 
using system.windows.forms; 
using system.runtime.interopservices; 
namespace windowsapplication1 
{ 
 public partial class autodeletemessagebox : form 
 { 
  [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; 
  public autodeletemessagebox() 
  { 
   initializecomponent(); 
  } 
  private void button1_click(object sender, eventargs e) 
  { 
   startkiller(); 
   messagebox.show("3秒钟后自动关闭messagebox窗口", "messagebox"); 
  } 
  private void startkiller() 
  { 
   timer timer = new timer(); 
   timer.interval = 3000; //3秒启动 
   timer.tick += new eventhandler(timer_tick); 
   timer.start(); 
  } 
  private void timer_tick(object sender, eventargs e) 
  { 
   killmessagebox(); 
   //停止timer 
   ((timer)sender).stop(); 
  } 
  private void killmessagebox() 
  { 
   //按照messagebox的标题,找到messagebox的窗口 
   intptr ptr = findwindow(null, "messagebox"); 
   if (ptr != intptr.zero) 
   { 
    //找到则关闭messagebox窗口 
    postmessage(ptr, wm_close, intptr.zero, intptr.zero); 
   } 
  } 
 } 
}

希望本文所述对大家的c#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网