簡體   English   中英

如何使用c#銷毀第三方WPF子窗口?

[英]How to destroy a Third Party WPF Child Window using c#?

我正在嘗試銷毀由第三方應用程序創建的子窗口。 我試圖使用WinApi來實現這一點,但似乎DestroyWindow(hwnd)不能解決問題。 我確實可以使用它,但是什么也沒發生,子窗口仍然存在。 我試圖使用CloseWindow(hwnd)只是為了驗證可以修改子窗口,是的,CloseWindow(hwnd)最小化了子窗口,但我想關閉它。

這是我的代碼:

[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:訪問被拒絕。 有什么解決辦法嗎?

win32 API DestroyWindow的文檔非常清楚:

線程無法使用DestroyWindow破壞由其他線程創建的窗口。

您也許可以在第三方應用程序中注入DLL,鈎住正確的線程,然后從此處發出DestroyWindow調用,但我不建議這樣做,因為它可能具有怪異的副作用(讀取:崩潰)。 某些應用程序的窗戶在腳下被破壞時,反應不佳。

正確的方法可能是使用常規Windows消息(如WM_CLOSE )或SendInput或UI Automation來偽造用戶交互。

如果用戶無法關閉該窗口,則說明您不走運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM