获取弹出框的句柄,关闭弹出框
2015-11-12 17:16
260 查看
大家可能有过这样的需求,有的弹出框可能需要手动关闭,这样非常麻烦,我参考相关资料,用C# 程序自动关闭弹出框的例子,供大家参考
View Code
/////用法简介:
1》调用 MultiThread. Dowork();
2》 这弹出框前调用 Messagebox.show(“message”,”title”));
3》 因为弹出框会阻塞主线程。所以其他的线程调用要在主线程之前启动,让他一直垂询主线程。去获得句柄。
Tip: 这是使用开源的的pgp加密文件,它有个时间验证,特别麻烦,所以就想了,一招来关闭弹出框。谢谢!
//获取弹出框的句柄,并隐藏函数。 using System.Runtime.InteropServices;//这个是必须的命名空间。 class SearchWindow { private const int WM_Close = 0x0010; [DllImport("User32.dll ", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "SendMessageA")] private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam); public SearchWindow() { } public void closeWindow(string lpClassName, string lpWindowName) { IntPtr Mhandle= FindWindow(null, lpWindowName); if (Mhandle != IntPtr.Zero) SendMessage(Mhandle, WM_Close, IntPtr.Zero, null); else { return; } } } ////隐藏控制台 using System.Runtime.InteropServices;//这个是必须的命名空间。 class ShadeConsole { [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)] static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow); [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); public void shade() { Console.Title = "consoleWin"; IntPtr cwInptr = FindWindow("ConsoleWindowClass", "consoleWin"); if (cwInptr != IntPtr.Zero) { ShowWindow(cwInptr, 0); } } } ///多线程的实现,关闭到messagebox或者其他的窗体; using System.Threading;//多线程必要的。 public static class MultiThread { public static void dowork() { ThreadPool.QueueUserWorkItem(new WaitCallback(s => { while (true) { SearchWindow sss = new SearchWindow(); // Thread.Sleep(200); s = null; sss.closeWindow("DidiSoft.Pgp.PGPLib", "DidiSoft OpenPGP Library for .NET"); } })); } }
View Code
/////用法简介:
1》调用 MultiThread. Dowork();
2》 这弹出框前调用 Messagebox.show(“message”,”title”));
3》 因为弹出框会阻塞主线程。所以其他的线程调用要在主线程之前启动,让他一直垂询主线程。去获得句柄。
Tip: 这是使用开源的的pgp加密文件,它有个时间验证,特别麻烦,所以就想了,一招来关闭弹出框。谢谢!
相关文章推荐
- android即时消息处理机制
- HashMap和Hashtable
- Java .class文件保护原理
- Android数据层解析层解耦问题
- Kafka+Spark Streaming+Redis实时计算整合实践
- yii2 api restful 验证 速度控制
- UITableView分割线式样 与 颜色设置
- 【es6】字符串扩展
- Linux笔记一 -- CentOS7
- 常用宏OC
- 在UWP应用中加入Cortana语音指令集
- js中typeof与instanceof区别
- 微软100道算法题------求子数组的最大和
- php中session定期自动清理的方法
- 递归将整数n转换为字符串
- SNMP协议
- 01 Time Complexity
- 记录:游戏开发中那些小坑坑
- 新的开始hello csdn
- Android小记:如何自学Android