c# - 判断事件窗口是否为 WPF 窗口

标签 c# .net wpf winforms

我使用 GetActiveWindow 获得了 UI 线程的事件窗口。如果它是 WPF 弹出窗口,我需要关闭窗口。

如何判断窗口是否为wpf窗口?

最佳答案

使用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;
    }
}

关于c# - 判断事件窗口是否为 WPF 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12106672/

相关文章:

wpf - 如何通过可视化树从 ContextMenu menuitem 访问控件?

c# - Entity Framework 6 - 引用表 - 如何编写自定义导航属性逻辑?

c# - 从 C# WPF 应用程序中的代码初始化静态资源

c# - float 解析 : Is there a Catch All algorithm?

c# - WPF 文本框和空格

c# - 如何使用 Control.DrawToBitmap 将 Form 渲染为没有装饰(标题栏、边框)的 Bitmap?

c# - ToString() 方法的意义是什么?

c# - 使用Java上传文件

c# - 取消 BlockingCollection.GetConsumingEnumerable() 并处理剩余的内容

c# - 在 WPF FluentRibbon Suite 控件中隐藏功能区选项卡标题