您的位置:首页 > 其它

获取弹出框的句柄,关闭弹出框

2015-11-12 17:16 260 查看
大家可能有过这样的需求,有的弹出框可能需要手动关闭,这样非常麻烦,我参考相关资料,用C# 程序自动关闭弹出框的例子,供大家参考

//获取弹出框的句柄,并隐藏函数。
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加密文件,它有个时间验证,特别麻烦,所以就想了,一招来关闭弹出框。谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: