Pavel Voronin
Pavel Voronin

Reputation: 13995

How to get the owner of the window if its owner is set via WindowInteropHelper?

I have parent window with its owner set via WindowInteropHelper. Parent.Owner equals null in this case. Is there any way to get the handle of the form owning the window?

I need this for dialog windows. When I try to set closed dialog window as the owner I get the exception. Thus I want to set dialog's owner to be the owner of another dialog. But it is WinForm and DialogWindow.Owner equals null.

WinForm (via WindowInteropHelper-> WPF Parent Dialog (closed) -> WPF Child Dialog

Upvotes: 4

Views: 5104

Answers (1)

DmitryG
DmitryG

Reputation: 17848

To get handle of the WinForms form that owns the specific WPF Window you can use the following code:

IntPtr ownerFormHandle = new WindowInteropHelper(wpfWindow).Owner

To get handle of any window (WinForms or WPF) that owns the specific window (WinForms or WPF) you can use the following code:

IntPtr ownerHandle = WinAPI.GetWindow(handle, WinAPI.GW_OWNER);
//...
public static class WinAPI {
    public const uint GW_OWNER = 4;
    [System.Runtime.InteropServices.DllImport("User32.dll")]
    public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
}

Upvotes: 6

Related Questions