/// <summary> /// Save the position of a form to the user settings. Hides the window /// as a side-effect. /// </summary> /// <param name = "name">The name to use when writing the position to the /// settings</param> private void SavePosition(string name) { if (!_needsPositionSave) { return; } _needsPositionSave = false; try { var rectangle = WindowState == FormWindowState.Normal ? DesktopBounds : RestoreBounds; var formWindowState = WindowState == FormWindowState.Maximized ? FormWindowState.Maximized : FormWindowState.Normal; if (_windowPositionList == null) { _windowPositionList = WindowPositionList.Load(); if (_windowPositionList == null) { return; } } WindowPosition windowPosition = _windowPositionList.Get(name); // Don't save location when we center modal form if (windowPosition != null && Owner != null && _windowCentred) { if (rectangle.Width <= windowPosition.Rect.Width && rectangle.Height <= windowPosition.Rect.Height) { rectangle.Location = windowPosition.Rect.Location; } } var position = new WindowPosition(rectangle, DpiUtil.DpiX, formWindowState, name); _windowPositionList.AddOrUpdate(position); _windowPositionList.Save(); } catch { // TODO: how to restore a corrupted config? } }
/// <summary> /// Save the position of a form to the user settings. Hides the window /// as a side-effect. /// </summary> /// <param name="form">The form to save the position for.</param> public void SavePosition(Form form) { try { var rectangle = form.WindowState == FormWindowState.Normal ? form.DesktopBounds : form.RestoreBounds; var formWindowState = form.WindowState == FormWindowState.Maximized ? FormWindowState.Maximized : FormWindowState.Normal; if (_windowPositionList == null) { _windowPositionList = WindowPositionList.Load(); if (_windowPositionList == null) { return; } } var name = form.GetType().Name; WindowPosition windowPosition = _windowPositionList.Get(name); var windowCentred = form.StartPosition == FormStartPosition.CenterParent; // Don't save location when we center modal form if (windowPosition != null && form.Owner != null && windowCentred) { if (rectangle.Width <= windowPosition.Rect.Width && rectangle.Height <= windowPosition.Rect.Height) { rectangle.Location = windowPosition.Rect.Location; } } var position = new WindowPosition(rectangle, DpiUtil.DpiX, formWindowState, name); _windowPositionList.AddOrUpdate(position); _windowPositionList.Save(); } catch { // TODO: how to restore a corrupted config? } }
/// <summary> /// Save the position of a form to the user settings. Hides the window /// as a side-effect. /// </summary> /// <param name = "name">The name to use when writing the position to the /// settings</param> private void SavePosition(String name) { try { var rectangle = WindowState == FormWindowState.Normal ? DesktopBounds : RestoreBounds; var formWindowState = WindowState == FormWindowState.Maximized ? FormWindowState.Maximized : FormWindowState.Normal; // Write to the user settings: if (_windowPositionList == null) { _windowPositionList = WindowPositionList.Load(); } WindowPosition windowPosition = _windowPositionList.Get(name); // Don't save location when we center modal form if (windowPosition != null && Owner != null && _windowCentred) { if (rectangle.Width <= windowPosition.Rect.Width && rectangle.Height <= windowPosition.Rect.Height) { rectangle.Location = windowPosition.Rect.Location; } } int deviceDpi = GetCurrentDeviceDpi(); var position = new WindowPosition(rectangle, deviceDpi, formWindowState, name); _windowPositionList.AddOrUpdate(position); _windowPositionList.Save(); } catch (Exception) { // TODO: howto restore a corrupted config? } }