2
votes

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

1
Please add the code which demonstrates windows relationship. It is not clear from the description...DmitryG

1 Answers

4
votes

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

IntPtr ownerFormHandle = new WindowInteropHelper(wpfWindow).Owner

To get handle of any window(WinFroms or WPF) which owns the specific window (WinFroms 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);
}