Determinar si la ventana activa es una ventana 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?

preguntado el 24 de agosto de 12 a las 10:08

1 Respuestas

Utilice las HwndSource.

Como sigue:

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)

    // 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;

Respondido el 17 de junio de 13 a las 23:06

How does the code ensures that it is a WPF window and the window does not correspond to a form? - maanu

It won't correspond to a Windows HwndSource will only return something if it's with a HWND which is connected to some WPF Visuals. In the example I showed checking whether that RootVisual was a WPF Window, as that was the thing you want to check for. - Csmith

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.