Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wpf

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

like image 984
Pavel Voronin Avatar asked Oct 11 '25 14:10

Pavel Voronin


1 Answers

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);
}
like image 63
DmitryG Avatar answered Oct 14 '25 05:10

DmitryG