/// <summary> /// Restore the size, state and location that were saved for a form /// </summary> /// <param name="form">The form whose properties are to be restored</param> public static void RestoreWindowLocation(Form form) { try { int currentScreenSizeCode = GetScreenSizeSummaryCode(); FormLocationsCollection flc = Properties.Settings.Default.FormLocations; if (flc != null) { string name = GetWindowName(form); FormLocation fl = (from f in flc where f.Name == name && f.ScreenSizeCode == currentScreenSizeCode select f).FirstOrDefault(); if (fl != null) { if (fl.IsMaximized) { form.WindowState = FormWindowState.Maximized; } else { form.Size = fl.Size; form.Location = fl.Location; } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("RestoreWindowLocation: " + ex.Message); } }
/// <summary> /// Save the current size, state and location of a form /// </summary> /// <param name="form">The form whose properties are to be saved</param> public static void SaveWindowLocation(Form form) { try { int currentScreenSizeCode = GetScreenSizeSummaryCode(); FormLocationsCollection flc = Properties.Settings.Default.FormLocations ?? new FormLocationsCollection(); FormLocation fl = new FormLocation(); fl.ScreenSizeCode = currentScreenSizeCode; fl.IsMaximized = form.WindowState == FormWindowState.Maximized; if (!fl.IsMaximized) { fl.Size = form.Size; fl.Location = form.Location; } string name = GetWindowName(form); fl.Name = name; // Get all locations which are *not* the current one FormLocationsCollection flcNew = new FormLocationsCollection(from f in flc where f.Name != name || f.ScreenSizeCode != currentScreenSizeCode select f); // Add current location to the collections flcNew.Add(fl); // Save back the newly formed collection Properties.Settings.Default.FormLocations = flcNew; Properties.Settings.Default.Save(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("SaveWindowLocation: " + ex.Message); } }