using System.Drawing; using System.Windows.Forms; namespace FastReport.Utils { /// /// Storage service for forms. /// public class FormStorageService : ControlStorageService { private Form form; private const string key_Left = "Left"; private const string key_Top = "Top"; private const string key_Width = "Width"; private const string key_Height = "Height"; private const string key_Maximized = "Maximized"; private bool IsVisibleOnAnyScreen(Rectangle rect) { Rectangle formRect = new Rectangle(rect.Left + 20, rect.Top + 20, rect.Width - 40, rect.Height - 40); foreach (Screen screen in Screen.AllScreens) { if (screen.WorkingArea.IntersectsWith(formRect)) { return true; } } return false; } /// /// Saves the form state to the configuration file. /// public void SaveFormState() { bool isMaximized = form.WindowState == FormWindowState.Maximized; bool isMinimized = form.WindowState == FormWindowState.Minimized; var restoreBounds = form.window.RestoreBounds; restoreBounds.Scale(form.DpiScale, form.DpiScale); SetBool(key_Maximized, isMaximized); SetInt(key_Left, isMinimized ? 0 : (isMaximized ? (int)restoreBounds.Left : form.Left)); SetInt(key_Top, isMinimized ? 0 : (isMaximized ? (int)restoreBounds.Top : form.Top)); SetInt(key_Width, isMaximized ? (int)restoreBounds.Width : form.Width); SetInt(key_Height, isMaximized ? (int)restoreBounds.Height : form.Height); } /// /// Restores the form state from the configuration file. /// public bool RestoreFormState(bool ignoreWindowState = false) { if (!Has(key_Left)) { // there is no state information yet, display the form as it was designed (usually centered on screen). return true; } // Get current screen working area Rectangle screenWorkingArea = Screen.GetWorkingArea(form); int windowLeftPosition = screenWorkingArea.Left; int windowTopPosition = screenWorkingArea.Top; int windowWidth = screenWorkingArea.Width; int windowHeight = screenWorkingArea.Height; // Get saved left and top positions if (Has(key_Left) && Has(key_Top)) { windowLeftPosition = GetInt(key_Left); windowTopPosition = GetInt(key_Top); } // Get saved width and height if (Has(key_Width) && Has(key_Height)) { windowWidth = GetInt(key_Width); windowHeight = GetInt(key_Height); } Rectangle formRect = new Rectangle(windowLeftPosition, windowTopPosition, windowWidth, windowHeight); // Check a visibility of form rectangle on any screen if (!IsVisibleOnAnyScreen(formRect)) { form.StartPosition = FormStartPosition.WindowsDefaultLocation; } else { form.StartPosition = FormStartPosition.Manual; // setting a WPF window position is tricky. This is a helper method form.MoveWindow(formRect); } // Set the window state if (!ignoreWindowState) form.WindowState = GetBool(key_Maximized) ? FormWindowState.Maximized : FormWindowState.Normal; return GetBool(key_Maximized); } /// /// Gets font from font storage. /// /// Element name from font storage. /// Default value. /// The font object. public new Font GetFont(string formElement, Font defaultFont) { return base.GetFont(form.Name + "," + formElement, defaultFont); } /// /// Initializes a new instance of storage class. /// /// The form which state will be saved/restored. public FormStorageService(Form form) : base(form, "Forms," + form.Name) { this.form = form; } } }