C# MessageBox自動(dòng)關(guān)閉
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
本文以一個(gè)簡(jiǎn)單的小例子,介紹如何讓MessageBox彈出的對(duì)話框,在幾秒鐘內(nèi)自動(dòng)關(guān)閉。特別是一些第三方插件(如:dll)彈出的對(duì)話框,最為適用。本文僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正。 概述在程序中MessageBox彈出的對(duì)話框,用于向用戶展示消息,這是一個(gè)模式窗口,可阻止應(yīng)用程序中的其他操作,直到用戶將其關(guān)閉。但是有時(shí)候在自動(dòng)化程序中,如果彈出對(duì)話框,程序?qū)?huì)中斷,等待人工的干預(yù),這是一個(gè)非常不好的交互體驗(yàn),如果程序能夠自動(dòng)幫我們點(diǎn)擊其中一個(gè)按鈕,讓對(duì)話框消失,該有多好。 原理通過對(duì)話框的標(biāo)題查找對(duì)話框,獲取對(duì)話框的句柄,然后對(duì)話框發(fā)送指令。 涉及知識(shí)點(diǎn)
示例截圖如下:關(guān)鍵代碼核心代碼如下: 1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Linq; 5 using System.Runtime.InteropServices; 6 using System.Text; 7 using System.Threading; 8 using System.Threading.Tasks; 9 10 namespace DemoMessageBox 11 { 12 /// <summary> 13 /// 作者:Alan.hsiang 14 /// 日期:2018-04-18 15 /// 描述:通過WinAPI進(jìn)行查找窗口,并對(duì)窗口進(jìn)行操作 16 /// </summary> 17 public class MessageBoxHelper 18 { 19 /// <summary> 20 /// 查找窗口 21 /// </summary> 22 /// <param name="hwnd">窗口句柄</param> 23 /// <param name="title">窗口標(biāo)題</param> 24 /// <returns></returns> 25 [DllImport("user32.dll", CharSet = CharSet.Auto)] 26 static extern IntPtr FindWindow(IntPtr hwnd, string title); 27 28 /// <summary> 29 /// 移動(dòng)窗口 30 /// </summary> 31 /// <param name="hwnd">窗口句柄</param> 32 /// <param name="x">起始位置X</param> 33 /// <param name="y">起始位置Y</param> 34 /// <param name="nWidth">窗口寬度</param> 35 /// <param name="nHeight">窗口高度</param> 36 /// <param name="rePaint">是否重繪</param> 37 [DllImport("user32.dll", CharSet = CharSet.Auto)] 38 static extern void MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, bool rePaint); 39 40 /// <summary> 41 /// 獲取窗口矩形 42 /// </summary> 43 /// <param name="hwnd">窗口句柄</param> 44 /// <param name="rect"></param> 45 /// <returns></returns> 46 [DllImport("user32.dll", CharSet = CharSet.Auto)] 47 static extern bool GetWindowRect(IntPtr hwnd, out Rectangle rect); 48 49 /// <summary> 50 /// 向窗口發(fā)送信息 51 /// </summary> 52 /// <param name="hwnd">窗口句柄</param> 53 /// <param name="msg">信息</param> 54 /// <param name="wParam">高字節(jié)</param> 55 /// <param name="lParam">低字節(jié)</param> 56 /// <returns></returns> 57 [DllImport("user32.dll", CharSet = CharSet.Auto)] 58 static extern int PostMessage(IntPtr hwnd, int msg, uint wParam, uint lParam); 59 60 public const int WM_CLOSE = 0x10; //關(guān)閉命令 61 62 public const int WM_KEYDOWN = 0x0100;//按下鍵 63 64 public const int WM_KEYUP = 0x0101;//按鍵起來 65 66 public const int VK_RETURN = 0x0D;//回車鍵 67 68 public static bool IsWorking = false; 69 70 /// <summary> 71 /// 對(duì)話框標(biāo)題 72 /// </summary> 73 public static string[] titles = new string[4] { "請(qǐng)選擇", "提示", "錯(cuò)誤", "警告" }; 74 75 /// <summary> 76 /// 查找和移動(dòng)窗口 77 /// </summary> 78 /// <param name="title">窗口標(biāo)題</param> 79 /// <param name="x">起始位置X</param> 80 /// <param name="y">起始位置Y</param> 81 public static void FindAndMoveWindow(string title, int x, int y) 82 { 83 Thread t = new Thread(() => 84 { 85 IntPtr msgBox = IntPtr.Zero; 86 while ((msgBox = FindWindow(IntPtr.Zero, title)) == IntPtr.Zero) ; 87 Rectangle r = new Rectangle(); 88 GetWindowRect(msgBox, out r); 89 MoveWindow(msgBox, x, y, r.Width - r.X, r.Height - r.Y, true); 90 }); 91 t.Start(); 92 } 93 94 /// <summary> 95 /// 查找和關(guān)閉窗口 96 /// </summary> 97 /// <param name="title">標(biāo)題</param> 98 private static void FindAndKillWindow(string title) 99 { 100 IntPtr ptr = FindWindow(IntPtr.Zero, title); 101 if (ptr != IntPtr.Zero) 102 { 103 int ret = PostMessage(ptr, WM_CLOSE, 0, 0); 104 Thread.Sleep(1000); 105 ptr = FindWindow(IntPtr.Zero, title); 106 if (ptr != IntPtr.Zero) 107 { 108 PostMessage(ptr, WM_KEYDOWN, VK_RETURN, 0); 109 PostMessage(ptr, WM_KEYUP, VK_RETURN, 0); 110 } 111 } 112 } 113 114 /// <summary> 115 /// 查找和關(guān)閉窗口 116 /// </summary> 117 public static void FindAndKillWindow() 118 { 119 Thread t = new Thread(() => 120 { 121 while (IsWorking) 122 { 123 //按標(biāo)題查找 124 foreach (string title in titles) 125 { 126 FindAndKillWindow(title); 127 } 128 Thread.Sleep(3000); 129 } 130 }); 131 t.Start(); 132 } 133 } 134 } 備注 關(guān)于源碼,請(qǐng)點(diǎn)擊鏈接自行下載。 關(guān)于PostMessage和SendMessage的區(qū)分,請(qǐng)點(diǎn)鏈接 該文章在 2021/1/29 18:20:42 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |