private static IntPtr FindTopLevelWindow(IntPtr hWnd) { while (hWnd != IntPtr.Zero && ScreenLocation.IsChildWindow(hWnd)) { hWnd = NativeMethods.GetParent(hWnd); } return(hWnd); }
public static HwndSource FindTopLevelHwndSource(UIElement element) { HwndSource hwndSource = (HwndSource)PresentationSource.FromVisual((Visual)element); if (hwndSource != null && ScreenLocation.IsChildWindow(hwndSource.Handle)) { hwndSource = HwndSource.FromHwnd(ScreenLocation.FindTopLevelWindow(hwndSource.Handle)); } return(hwndSource); }