From f9e244272e984d29b903c1328c9f4f230c6c49dc Mon Sep 17 00:00:00 2001 From: Kang Yu Date: Sun, 19 Sep 2021 16:54:33 -0700 Subject: [PATCH] fix random issue on Window 11 that unminimized full screen window is placed at wrong screen, without afecting multi-screen rdp session (#127) --- .../PersistentWindowProcessor.cs | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Ninjacrab.PersistentWindows.Solution/Ninjacrab.PersistentWindows.Common/PersistentWindowProcessor.cs b/Ninjacrab.PersistentWindows.Solution/Ninjacrab.PersistentWindows.Common/PersistentWindowProcessor.cs index a0faa0d..3fa3448 100755 --- a/Ninjacrab.PersistentWindows.Solution/Ninjacrab.PersistentWindows.Common/PersistentWindowProcessor.cs +++ b/Ninjacrab.PersistentWindows.Solution/Ninjacrab.PersistentWindows.Common/PersistentWindowProcessor.cs @@ -937,7 +937,8 @@ namespace Ninjacrab.PersistentWindows.Common { if (prevDisplayMetrics.IsFullScreen) { - RestoreFullScreenWindow(hwnd); //the window was minimized from full screen status + //the window was minimized from full screen status + RestoreFullScreenWindow(hwnd, prevDisplayMetrics.ScreenPosition); } else if (!IsFullScreen(hwnd)) { @@ -2216,7 +2217,7 @@ namespace Ninjacrab.PersistentWindows.Common } - private void RestoreFullScreenWindow(IntPtr hwnd) + private void RestoreFullScreenWindow(IntPtr hwnd, RECT2 rect) { long style = User32.GetWindowLong(hwnd, User32.GWL_STYLE); if ((style & (long)WindowStyleFlags.CAPTION) == 0L) @@ -2224,6 +2225,20 @@ namespace Ninjacrab.PersistentWindows.Common return; } + RECT2 intersect = new RECT2(); + + bool wrong_screen = false; + RECT2 cur_rect = new RECT2(); + User32.GetWindowRect(hwnd, ref cur_rect); + if (!User32.IntersectRect(out intersect, ref cur_rect, ref rect)) + wrong_screen = true; + + if (wrong_screen) + { + User32.MoveWindow(hwnd, rect.Left, rect.Top, rect.Width, rect.Height, true); + Log.Error("fix wrong screen for {0}", GetWindowTitle(hwnd)); + } + RECT2 screenPosition = new RECT2(); User32.GetWindowRect(hwnd, ref screenPosition); @@ -2726,7 +2741,7 @@ namespace Ninjacrab.PersistentWindows.Common success &= User32.MoveWindow(hWnd, rect.Left, rect.Top, rect.Width, rect.Height, true); if (prevDisplayMetrics.IsFullScreen && windowPlacement.ShowCmd == ShowWindowCommands.Normal && !prevDisplayMetrics.IsMinimized) { - RestoreFullScreenWindow(hWnd); + RestoreFullScreenWindow(hWnd, prevDisplayMetrics.ScreenPosition); } else if (restoreTimes >= MinRestoreTimes - 1) {