确定活动窗口是否为WPF窗口(Determine whether active window is WPF window)

编程入门 行业动态 更新时间:2024-10-28 18:34:12
确定活动窗口是否为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; } }

更多推荐

本文发布于:2023-04-28 01:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329609.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗口   WPF   Determine   active   window

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!