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.

http://msdn.microsoft.com/en-us/library/system.windows.interop.hwndsource.fromhwnd.aspx

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)
    {
        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;
    }
}

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 Form...as 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.