文章出處
文章列表
public class CloseForm { [DllImport("user32", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "FindWindowEx")] private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern void SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, int lParam); private const int WM_CLOSE = 0x10;//關閉 private const int BM_CLICK = 0xF5;//點擊 private DateTime dt = DateTime.Now;//當前時間 private string MsgTitle { get; set; } public void CloseTitleForm(string title) { this.MsgTitle = title; System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer(); timer1.Tick += new EventHandler(timer1_Tick); timer1.Interval = 1000; timer1.Enabled = true; //MessageBox.Show("若不回應的話,X秒後此 MsgBox 會自動關閉", MsgTitle); //timer1.Enabled = false; } private void timer1_Tick(object sender, EventArgs e) { IntPtr hWnd = FindWindow(null, MsgTitle); //窗體按鈕 IntPtr childHwnd = FindWindowEx(hWnd, IntPtr.Zero, null, "是(&Y)"); if (childHwnd != IntPtr.Zero) { //模擬點擊 是(&Y) SendMessage(childHwnd, BM_CLICK, IntPtr.Zero, 0); } else { //沒有找到按鈕則關閉 SendMessage(hWnd, WM_CLOSE, IntPtr.Zero, 0); } if (DateTime.Now.Subtract(dt).TotalSeconds > 10) { //10秒后停止執行 ((System.Windows.Forms.Timer)sender).Stop(); } } }
文章列表
全站熱搜