确定活动窗口是否为WPF窗口(Determine whether active window is WPF window)
我有GetActiveWindow的UI线程的活动窗口。 如果它是WPF弹出窗口,我需要关闭窗口。
如何判断窗口是否为wpf窗口?
I have got the the active window of a UI thread with GetActiveWindow. I need to close the window if it is a WPF pop up window.
How can I determine whether the window is a wpf window or not?
最满意答案
使用HwndSource 。
http://msdn.microsoft.com/en-us/library/system.windows.interop.hwndsource.fromhwnd.aspx
如下:
IntPtr hwnd = GetActivewWindow(); HwndSource hwndsrc = HwndSource.FromHwnd(hwnd); // Use any variation on this code if (hwndsrc != null && hwndsrc.RootVisual != null) { Window window = hwndsrc.RootVisual as Window; if (window != null) { window.Close(); } // UPDATE: I've added looking for a "Popup" window as well // because your question mentions "pop up window"...but // not sure if you meant a top-level Window, or a Popup... // ....Popup windows have HWND too! Popup popupwindow = hwndsrc.RootVisual as Popup; if (popupwindow != null) { popupwindow.IsOpen = false; } }Use HwndSource.
http://msdn.microsoft.com/en-us/library/system.windows.interop.hwndsource.fromhwnd.aspx
As follows:
IntPtr hwnd = GetActivewWindow(); HwndSource hwndsrc = HwndSource.FromHwnd(hwnd); // Use any variation on this code if (hwndsrc != null && hwndsrc.RootVisual != null) { Window window = hwndsrc.RootVisual as Window; if (window != null) { window.Close(); } // UPDATE: I've added looking for a "Popup" window as well // because your question mentions "pop up window"...but // not sure if you meant a top-level Window, or a Popup... // ....Popup windows have HWND too! Popup popupwindow = hwndsrc.RootVisual as Popup; if (popupwindow != null) { popupwindow.IsOpen = false; } }更多推荐
发布评论