[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
const int WM_CLOSE = 0x0010; // 关闭窗口的消息代码
const int SW_HIDE = 0;
/// <summary>
/// 关闭窗口
/// </summary>
/// <returns></returns>
public async Task Close()
{
await Task.Run(() =>
{
while (true)
{
// 找到弹窗的句柄
IntPtr hWnd = FindWindow(null, "test"); // "窗口标题"替换为需要关闭的弹框标题
if (hWnd != IntPtr.Zero)
{
// 关闭窗口
PostMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
Thread.Sleep(500);
}
});
}