private void UpdateZOrderOfThisAndOwner() { if (_updatingZOrder) { return; } try { _updatingZOrder = true; var windowInteropHelper = new WindowInteropHelper(this); var handle = windowInteropHelper.Handle; foreach (var shadowWindow in LoadedShadowWindows) { if (NativeMethods.GetWindow(new WindowInteropHelper(shadowWindow).Handle, 3) != handle) { NativeMethods.SetWindowPos(new WindowInteropHelper(shadowWindow).Handle, handle, 0, 0, 0, 0, 19); } handle = new WindowInteropHelper(shadowWindow).Handle; } var owner = windowInteropHelper.Owner; if (!(owner != IntPtr.Zero)) { return; } UpdateZOrderOfOwner(owner); } finally { _updatingZOrder = false; } }
private static void UpdateZOrderOfOwner(IntPtr hwndOwner) { var lastOwnedWindow = IntPtr.Zero; NativeMethods.EnumThreadWindows(NativeMethods.GetCurrentThreadId(), (hwnd, lParam) => { if (NativeMethods.GetWindow(hwnd, 4) == hwndOwner) { lastOwnedWindow = hwnd; } return(true); }, IntPtr.Zero); if (!(lastOwnedWindow != IntPtr.Zero) || !(NativeMethods.GetWindow(hwndOwner, 3) != lastOwnedWindow)) { return; } NativeMethods.SetWindowPos(hwndOwner, lastOwnedWindow, 0, 0, 0, 0, 19); }