123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using System.Drawing;
- using System.Windows.Forms;
- namespace FastReport.Utils
- {
- /// <summary>
- /// Storage service for forms.
- /// </summary>
- 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;
- }
- /// <summary>
- /// Saves the form state to the configuration file.
- /// </summary>
- public void SaveFormState()
- {
- bool isMaximized = form.WindowState == FormWindowState.Maximized;
- bool isMinimized = form.WindowState == FormWindowState.Minimized;
- SetBool(key_Maximized, isMaximized);
- SetInt(key_Left, isMinimized ? 0 : form.Location.X);
- SetInt(key_Top, isMinimized ? 0 : form.Location.Y);
- SetInt(key_Width, form.Size.Width);
- SetInt(key_Height, form.Size.Height);
- }
- /// <summary>
- /// Restores the form state from the configuration file.
- /// </summary>
- 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);
- form.Location = new Point(windowLeftPosition, windowTopPosition);
- }
- // Get saved width and height
- if (Has(key_Width) && Has(key_Height))
- {
- windowWidth = GetInt(key_Width);
- windowHeight = GetInt(key_Height);
- form.Size = new Size(windowWidth, windowHeight);
- }
- Rectangle formRect = new Rectangle(windowLeftPosition, windowTopPosition,
- windowWidth, windowHeight);
- // Check a visibility of form rectangle on any screen
- if (!IsVisibleOnAnyScreen(formRect))
- {
- form.StartPosition = FormStartPosition.WindowsDefaultLocation;
- form.Location = new Point(screenWorkingArea.Left, screenWorkingArea.Top);
- }
- form.StartPosition = FormStartPosition.Manual;
- // Set the window state
- if (!ignoreWindowState)
- form.WindowState = GetBool(key_Maximized) ?
- FormWindowState.Maximized : FormWindowState.Normal;
- return GetBool(key_Maximized);
- }
- /// <summary>
- /// Gets font from font storage.
- /// </summary>
- /// <param name="formElement">Element name from font storage.</param>
- /// <param name="defaultFont">Default value.</param>
- /// <returns>The font object.</returns>
- public new Font GetFont(string formElement, Font defaultFont)
- {
- return base.GetFont(form.Name + "," + formElement, defaultFont);
- }
- /// <summary>
- /// Initializes a new instance of storage class.
- /// </summary>
- /// <param name="form">The form which state will be saved/restored.</param>
- public FormStorageService(Form form) : base(form, "Forms," + form.Name)
- {
- this.form = form;
- }
- }
- }
|