獲取彈出框的句柄,關閉彈出框


     大家可能有過這樣的需求,有的彈出框可能需要手動關閉,這樣非常麻煩,我參考相關資料,用C# 程序自動關閉彈出框的例子,供大家參考

 1 //獲取彈出框的句柄,並隱藏函數。
 2 using System.Runtime.InteropServices;//這個是必須的命名空間。
 3 class SearchWindow
 4     {
 5 
 6         private const int WM_Close = 0x0010;
 7         [DllImport("User32.dll ", EntryPoint = "FindWindow")]
 8         private static extern IntPtr FindWindow(string lpClassName,
 9         string lpWindowName);
10         [DllImport("user32.dll", EntryPoint = "SendMessageA")]
11         private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);
12         public SearchWindow()
13         {
14                 
15         }
16         public void closeWindow(string lpClassName, string lpWindowName)
17         {
18             
19            IntPtr  Mhandle= FindWindow(null, lpWindowName);
20            if (Mhandle != IntPtr.Zero)
21                SendMessage(Mhandle, WM_Close, IntPtr.Zero, null);
22            else
23            {
24                return;
25            }
26         }
27     }
28 ////隱藏控制台
29 
30 using System.Runtime.InteropServices;//這個是必須的命名空間。
31 
32 
33 class ShadeConsole
34     {
35         [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
36         static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
37         [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
38         public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
39         public void shade()
40         {
41             Console.Title = "consoleWin";
42             IntPtr cwInptr = FindWindow("ConsoleWindowClass", "consoleWin");
43             if (cwInptr != IntPtr.Zero)
44             {
45                 ShowWindow(cwInptr, 0);
46             }
47         }
48     }
49 
50 ///多線程的實現,關閉到messagebox或者其他的窗體;
51 
52 using System.Threading;//多線程必要的。
53 
54 public static class MultiThread
55     {
56         public static void dowork()
57         {   
58            
59             ThreadPool.QueueUserWorkItem(new WaitCallback(s => {
60                 while (true)
61                 {
62                     SearchWindow sss = new SearchWindow();
63                     // Thread.Sleep(200);
64                     s = null;
65                     sss.closeWindow("DidiSoft.Pgp.PGPLib", "DidiSoft OpenPGP Library for .NET"); 
66                 }
67             }));
68         }
69 }
View Code

/////用法簡介:

 

1》調用 MultiThread. Dowork();

2》       這彈出框前調用     Messagebox.show(“message”,”title”));

3》      因為彈出框會阻塞主線程。所以其他的線程調用要在主線程之前啟動,讓他一直垂詢主線程。去獲得句柄。

Tip: 這是使用開源的的pgp加密文件,它有個時間驗證,特別麻煩,所以就想了,一招來關閉彈出框。謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM