使用API自动关闭模式窗体

 没什么话说,方法如下,以供参考:
  1.  [DllImport("user32", EntryPoint = "FindWindow")]
  2.         public static extern IntPtr FindWindowA(string lpClassName, string lpWindowName);
  3.         [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
  4.         private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
  5.         [DllImport("User32.dll", EntryPoint = "SendMessage")]
  6.         private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
  7.         private const int WM_CLOSE = 0x0010;
  8.         private const int WM_GETTEXT = 0x000D;
  9.         private const int WM_SETTEXT = 0x000C;
  10.         private const int WM_CLICK = 0x00F5;
  11.         private int _autoCloseTime = 3;
  12.         public string MsgTitle =string.Empty;
  13.         string lpszClass_Submit = "Button"//需要查找的Button的类名 
  14.         string lpszName_Submit = "确定"//需要查找的Button的标题 
  15.         private Timer timer1;
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.             MsgTitle = "还剩下" + _autoCloseTime.ToString() + "秒";
  20.             timer1 = new System.Windows.Forms.Timer();
  21.             timer1.Tick += new EventHandler(timer1_Tick);
  22.             timer1.Interval = 1000;
  23.         }
  24.         private void KillMessageBox(Timer timer)
  25.         {
  26.             //查找MessageBox的弹出窗口,注意对应标题
  27.             IntPtr hWnd = IntPtr.Zero;
  28.             if(_autoCloseTime == 3)
  29.             {
  30.                 hWnd = FindWindowA(null, MsgTitle);
  31.             }
  32.             else
  33.             {
  34.                 hWnd = FindWindowA(null"还剩下" + (_autoCloseTime + 1).ToString() + "秒");
  35.             }
  36.             if (hWnd != IntPtr.Zero)
  37.             {
  38.                 string str =  "还剩下" + _autoCloseTime.ToString() + "秒";
  39.                 SendMessage(hWnd, WM_SETTEXT, (IntPtr)0, str);
  40.                 _autoCloseTime --;
  41.                 if (_autoCloseTime < 0)
  42.                 {
  43.                     IntPtr EdithWnd = new IntPtr(0);
  44.                     EdithWnd = FindWindowEx(hWnd,EdithWnd, lpszClass_Submit, lpszName_Submit);
  45.                     if (!EdithWnd.Equals(IntPtr.Zero))
  46.                     {
  47.                         SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");
  48.                     } 
  49.                     查找到窗口则关闭
  50.                     //SendMessage(hWnd, WM_CLOSE, IntPtr.Zero, "");
  51.                     //停止计时器
  52.                     timer.Stop();
  53.                     _autoCloseTime = 3;
  54.                 }
  55.             }
  56.         }
  57.         private void StartKiller()
  58.         {
  59.             timer1.Enabled = true;
  60.         }
  61.         private void timer1_Tick(object sender, EventArgs e)
  62.         {
  63.             KillMessageBox(sender as Timer);
  64.         }
  65.         private void button1_Click(object sender, EventArgs e)
  66.         {
  67.             StartKiller();
  68.             DialogResult result = MessageBox.Show("若不响应,程序将在3秒后自动关闭", MsgTitle, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  69.             if (result == DialogResult.OK)
  70.             {
  71.                 timer1.Stop();
  72.                 _autoCloseTime = 3;
  73.                 MessageBox.Show("关闭");
  74.             }
  75.         }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值