fix random issue on Window 11 that unminimized full screen window is placed at wrong screen, without afecting multi-screen rdp session (#127)

This commit is contained in:
Kang Yu 2021-09-19 16:54:33 -07:00
parent cc769fe59a
commit f9e244272e

View file

@ -937,7 +937,8 @@ namespace Ninjacrab.PersistentWindows.Common
{ {
if (prevDisplayMetrics.IsFullScreen) 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)) 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); long style = User32.GetWindowLong(hwnd, User32.GWL_STYLE);
if ((style & (long)WindowStyleFlags.CAPTION) == 0L) if ((style & (long)WindowStyleFlags.CAPTION) == 0L)
@ -2224,6 +2225,20 @@ namespace Ninjacrab.PersistentWindows.Common
return; 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(); RECT2 screenPosition = new RECT2();
User32.GetWindowRect(hwnd, ref screenPosition); 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); success &= User32.MoveWindow(hWnd, rect.Left, rect.Top, rect.Width, rect.Height, true);
if (prevDisplayMetrics.IsFullScreen && windowPlacement.ShowCmd == ShowWindowCommands.Normal && !prevDisplayMetrics.IsMinimized) if (prevDisplayMetrics.IsFullScreen && windowPlacement.ShowCmd == ShowWindowCommands.Normal && !prevDisplayMetrics.IsMinimized)
{ {
RestoreFullScreenWindow(hWnd); RestoreFullScreenWindow(hWnd, prevDisplayMetrics.ScreenPosition);
} }
else if (restoreTimes >= MinRestoreTimes - 1) else if (restoreTimes >= MinRestoreTimes - 1)
{ {