[DllImport("user32.dll")] public static extern int GetParent(int hwnd); public int GetParentWindowHandle(Visual child) { HwndSource presentationSource = (HwndSource)PresentationSource.FromVisual(child); int parentHandle = presentationSource.Handle.ToInt32(); int handle = parentHandle; while (parentHandle != 0) { handle = parentHandle; parentHandle = ApplicationHelperInterop.GetParent(parentHandle); } return handle; }
You can then loop through the System.Windows.Forms.Application.OpenForms collection to find the WinForm matching the return value of the GetParentWindowHandle method above.
Alex D.
source share