From e8c7b5c322bfdde5bb60170468e862fe27d2992d Mon Sep 17 00:00:00 2001 From: mega5800 <43865655+mega5800@users.noreply.github.com> Date: Fri, 3 May 2024 06:17:59 +0100 Subject: [PATCH] fix: resolved an error that would occur upon closing the app due to saving window size and location in app folder (#191) fixes #190 Thanks @mega5800 ! --- src/LessMsi.Gui/MainForm.cs | 53 ++++--------------- .../Properties/Settings.Designer.cs | 28 +++++++++- src/LessMsi.Gui/Properties/Settings.settings | 6 +++ src/LessMsi.Gui/app.config | 22 +++++--- 4 files changed, 58 insertions(+), 51 deletions(-) diff --git a/src/LessMsi.Gui/MainForm.cs b/src/LessMsi.Gui/MainForm.cs index 6c7584c..44fb1f3 100644 --- a/src/LessMsi.Gui/MainForm.cs +++ b/src/LessMsi.Gui/MainForm.cs @@ -52,14 +52,6 @@ internal class MainForm : Form, IMainFormView private ToolStripMenuItem searchFileToolStripMenuItem; readonly static string[] AllowedDragDropExtensions = new[] { ".msi", ".msp" }; - #region Form size and location members - private const string c_WindowWidthAttribute = "WindowWidth"; - private const string c_WindowHeightAttribute = "WindowHeight"; - - private const string c_XPositionAttribute = "X_Position"; - private const string c_YPositionAttribute = "Y_Position"; - #endregion - public MainForm(string defaultInputFile) { InitializeComponent(); @@ -1088,46 +1080,23 @@ private void cboStream_SelectedValueChanged(object sender, EventArgs e) private void MainForm_Load(object sender, EventArgs e) { - int windowWidth = getIntValueFromConfiguration(c_WindowWidthAttribute, MinimumSize.Width); - int windowHeight = getIntValueFromConfiguration(c_WindowHeightAttribute, MinimumSize.Height); + Size lastRecordedAppSize = Settings.Default.LastRecordedAppSize; + Point lastRecordedAppLocation = Settings.Default.LastRecordedAppLocation; - int xPosition = getIntValueFromConfiguration(c_XPositionAttribute, 0); - int yPosition = getIntValueFromConfiguration(c_YPositionAttribute, 0); + Size = new Size + ( + Math.Max(MinimumSize.Width, lastRecordedAppSize.Width), + Math.Max(MinimumSize.Height, lastRecordedAppSize.Height) + ); - Size = new Size(windowWidth, windowHeight); - Location = new Point(xPosition, yPosition); + Location = lastRecordedAppLocation; } private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { - updateConfiguration(c_WindowWidthAttribute, Size.Width.ToString()); - updateConfiguration(c_WindowHeightAttribute, Size.Height.ToString()); - - updateConfiguration(c_XPositionAttribute, Location.X.ToString()); - updateConfiguration(c_YPositionAttribute, Location.Y.ToString()); - } - #endregion - - #region Form size and location methods - private int getIntValueFromConfiguration(string i_SettingName, int i_MinValue) - { - int intValue = 0; - string rawValue = ConfigurationManager.AppSettings.Get(i_SettingName); - - int.TryParse(rawValue, out intValue); - intValue = Math.Max(intValue, i_MinValue); - - return intValue; - } - - private void updateConfiguration(string i_SettingName, string i_SettingValue) - { - Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath); - - config.AppSettings.Settings.Remove(i_SettingName); - config.AppSettings.Settings.Add(i_SettingName, i_SettingValue); - - config.Save(ConfigurationSaveMode.Minimal); + Settings.Default.LastRecordedAppSize = Size; + Settings.Default.LastRecordedAppLocation = Location; + Settings.Default.Save(); } #endregion diff --git a/src/LessMsi.Gui/Properties/Settings.Designer.cs b/src/LessMsi.Gui/Properties/Settings.Designer.cs index ce5c8cf..d3dbc19 100644 --- a/src/LessMsi.Gui/Properties/Settings.Designer.cs +++ b/src/LessMsi.Gui/Properties/Settings.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.17929 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -12,7 +12,7 @@ namespace LessMsi.Gui.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.9.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -33,5 +33,29 @@ public static Settings Default { this["RecentFiles"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("0, 0")] + public global::System.Drawing.Size LastRecordedAppSize { + get { + return ((global::System.Drawing.Size)(this["LastRecordedAppSize"])); + } + set { + this["LastRecordedAppSize"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("10, 10")] + public global::System.Drawing.Point LastRecordedAppLocation { + get { + return ((global::System.Drawing.Point)(this["LastRecordedAppLocation"])); + } + set { + this["LastRecordedAppLocation"] = value; + } + } } } diff --git a/src/LessMsi.Gui/Properties/Settings.settings b/src/LessMsi.Gui/Properties/Settings.settings index bed8783..99c4a8a 100644 --- a/src/LessMsi.Gui/Properties/Settings.settings +++ b/src/LessMsi.Gui/Properties/Settings.settings @@ -5,5 +5,11 @@ + + 0, 0 + + + 10, 10 + \ No newline at end of file diff --git a/src/LessMsi.Gui/app.config b/src/LessMsi.Gui/app.config index c843c11..337e7db 100644 --- a/src/LessMsi.Gui/app.config +++ b/src/LessMsi.Gui/app.config @@ -1,14 +1,22 @@ + + +
+ + - - - - - - - + + + + 0, 0 + + + 10, 10 + + + \ No newline at end of file