From 3f0e6b9ee5c60d415b34d36c085808e6d7652553 Mon Sep 17 00:00:00 2001 From: MBucari Date: Sat, 11 Mar 2023 21:33:55 -0700 Subject: [PATCH] Fix window restore maximize statate on secondary monitor. --- Source/LibationWinForms/FormSaveExtension.cs | 26 ++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/Source/LibationWinForms/FormSaveExtension.cs b/Source/LibationWinForms/FormSaveExtension.cs index 3ab11630..677ccb1d 100644 --- a/Source/LibationWinForms/FormSaveExtension.cs +++ b/Source/LibationWinForms/FormSaveExtension.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Windows.Forms; using LibationFileManager; +using Microsoft.EntityFrameworkCore.Query.SqlExpressions; namespace LibationWinForms { @@ -44,20 +45,31 @@ namespace LibationWinForms var rect = new Rectangle(x, y, savedState.Width, savedState.Height); - // is proposed rect on a screen? - if (Screen.AllScreens.Any(screen => screen.WorkingArea.Contains(rect))) + if (savedState.IsMaximized) { + //When a window is maximized, the client rectangle is not on a screen (y is negative). form.StartPosition = FormStartPosition.Manual; form.DesktopBounds = rect; + + // FINAL: for Maximized: start normal state, set size and location, THEN set max state + form.WindowState = FormWindowState.Maximized; } else { - form.StartPosition = FormStartPosition.WindowsDefaultLocation; - form.Size = rect.Size; - } + // is proposed rect on a screen? + if (Screen.AllScreens.Any(screen => screen.WorkingArea.Contains(rect))) + { + form.StartPosition = FormStartPosition.Manual; + form.DesktopBounds = rect; + } + else + { + form.StartPosition = FormStartPosition.WindowsDefaultLocation; + form.Size = rect.Size; + } - // FINAL: for Maximized: start normal state, set size and location, THEN set max state - form.WindowState = savedState.IsMaximized ? FormWindowState.Maximized : FormWindowState.Normal; + form.WindowState = FormWindowState.Normal; + } } public static void SaveSizeAndLocation(this Form form, Configuration config)