FormStorageService.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Drawing;
  2. using System.Windows.Forms;
  3. namespace FastReport.Utils
  4. {
  5. /// <summary>
  6. /// Storage service for forms.
  7. /// </summary>
  8. public class FormStorageService : ControlStorageService
  9. {
  10. private Form form;
  11. private const string key_Left = "Left";
  12. private const string key_Top = "Top";
  13. private const string key_Width = "Width";
  14. private const string key_Height = "Height";
  15. private const string key_Maximized = "Maximized";
  16. private bool IsVisibleOnAnyScreen(Rectangle rect)
  17. {
  18. Rectangle formRect = new Rectangle(rect.Left + 20, rect.Top + 20, rect.Width - 40, rect.Height - 40);
  19. foreach (Screen screen in Screen.AllScreens)
  20. {
  21. if (screen.WorkingArea.IntersectsWith(formRect))
  22. {
  23. return true;
  24. }
  25. }
  26. return false;
  27. }
  28. /// <summary>
  29. /// Saves the form state to the configuration file.
  30. /// </summary>
  31. public void SaveFormState()
  32. {
  33. bool isMaximized = form.WindowState == FormWindowState.Maximized;
  34. bool isMinimized = form.WindowState == FormWindowState.Minimized;
  35. SetBool(key_Maximized, isMaximized);
  36. SetInt(key_Left, isMinimized ? 0 : form.Location.X);
  37. SetInt(key_Top, isMinimized ? 0 : form.Location.Y);
  38. SetInt(key_Width, form.Size.Width);
  39. SetInt(key_Height, form.Size.Height);
  40. }
  41. /// <summary>
  42. /// Restores the form state from the configuration file.
  43. /// </summary>
  44. public bool RestoreFormState(bool ignoreWindowState = false)
  45. {
  46. if (!Has(key_Left))
  47. {
  48. // there is no state information yet, display the form as it was designed (usually centered on screen).
  49. return true;
  50. }
  51. // Get current screen working area
  52. Rectangle screenWorkingArea = Screen.GetWorkingArea(form);
  53. int windowLeftPosition = screenWorkingArea.Left;
  54. int windowTopPosition = screenWorkingArea.Top;
  55. int windowWidth = screenWorkingArea.Width;
  56. int windowHeight = screenWorkingArea.Height;
  57. // Get saved left and top positions
  58. if (Has(key_Left) && Has(key_Top))
  59. {
  60. windowLeftPosition = GetInt(key_Left);
  61. windowTopPosition = GetInt(key_Top);
  62. form.Location = new Point(windowLeftPosition, windowTopPosition);
  63. }
  64. // Get saved width and height
  65. if (Has(key_Width) && Has(key_Height))
  66. {
  67. windowWidth = GetInt(key_Width);
  68. windowHeight = GetInt(key_Height);
  69. form.Size = new Size(windowWidth, windowHeight);
  70. }
  71. Rectangle formRect = new Rectangle(windowLeftPosition, windowTopPosition,
  72. windowWidth, windowHeight);
  73. // Check a visibility of form rectangle on any screen
  74. if (!IsVisibleOnAnyScreen(formRect))
  75. {
  76. form.StartPosition = FormStartPosition.WindowsDefaultLocation;
  77. form.Location = new Point(screenWorkingArea.Left, screenWorkingArea.Top);
  78. }
  79. form.StartPosition = FormStartPosition.Manual;
  80. // Set the window state
  81. if (!ignoreWindowState)
  82. form.WindowState = GetBool(key_Maximized) ?
  83. FormWindowState.Maximized : FormWindowState.Normal;
  84. return GetBool(key_Maximized);
  85. }
  86. /// <summary>
  87. /// Gets font from font storage.
  88. /// </summary>
  89. /// <param name="formElement">Element name from font storage.</param>
  90. /// <param name="defaultFont">Default value.</param>
  91. /// <returns>The font object.</returns>
  92. public new Font GetFont(string formElement, Font defaultFont)
  93. {
  94. return base.GetFont(form.Name + "," + formElement, defaultFont);
  95. }
  96. /// <summary>
  97. /// Initializes a new instance of storage class.
  98. /// </summary>
  99. /// <param name="form">The form which state will be saved/restored.</param>
  100. public FormStorageService(Form form) : base(form, "Forms," + form.Name)
  101. {
  102. this.form = form;
  103. }
  104. }
  105. }