我想摧毁由第三方应用程序创建的子窗口。我试图使用WinApi来实现这一点,但似乎DestroyWindow(hwnd)并没有做到这一点。我确实得到了一个处理工作,但没有任何反应,子窗口仍然存在。我试图使用CloseWindow(hwnd)来验证子窗口是否可以修改,是的,CloseWindow(hwnd)最小化子窗口,但我想关闭它。如何使用c#销毁第三方WPF子窗口?
这是我的代码:
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DestroyWindow(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseWindow(IntPtr hWnd);
private static void CloseChildWindow();
{
IntPtr _hwnd = IntPtr.Zero;
if (WindowController.FindWindow(null, "System Alert") != IntPtr.Zero)
{
_hwnd = WindowController.FindWindow(null, "System Alert");
DestroyWindow(_hwnd); //Returns error code 5: Access Denied
CloseWindow(_hwnd); //Minimizes the window therefore _hwnd is correct
}
}
上正在发生的事情或解决方法,将不胜感激任何想法,在此先感谢。
EDIT1:Destroywindow返回代码错误5:访问被拒绝。解决方法的任何想法?
你是否检查过你的'DestroyWindow'调用是否返回错误代码? – Brandon
返回错误代码错误代码5:访问被拒绝,看起来像我无法销毁它,出于这个原因,将更新问题。 –
这可能是(和IIRC:是),因为您的调用应用程序不是负责创建窗口的应用程序。您可能需要获得某种上下文/许可才能执行此操作。我不确定所需的步骤 – Brandon