FormStorageService.WPF.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. var restoreBounds = form.window.RestoreBounds;
  36. restoreBounds.Scale(form.DpiScale, form.DpiScale);
  37. SetBool(key_Maximized, isMaximized);
  38. SetInt(key_Left, isMinimized ? 0 : (isMaximized ? (int)restoreBounds.Left : form.Left));
  39. SetInt(key_Top, isMinimized ? 0 : (isMaximized ? (int)restoreBounds.Top : form.Top));
  40. SetInt(key_Width, isMaximized ? (int)restoreBounds.Width : form.Width);
  41. SetInt(key_Height, isMaximized ? (int)restoreBounds.Height : form.Height);
  42. }
  43. /// <summary>
  44. /// Restores the form state from the configuration file.
  45. /// </summary>
  46. public bool RestoreFormState(bool ignoreWindowState = false)
  47. {
  48. if (!Has(key_Left))
  49. {
  50. // there is no state information yet, display the form as it was designed (usually centered on screen).
  51. return true;
  52. }
  53. // Get current screen working area
  54. Rectangle screenWorkingArea = Screen.GetWorkingArea(form);
  55. int windowLeftPosition = screenWorkingArea.Left;
  56. int windowTopPosition = screenWorkingArea.Top;
  57. int windowWidth = screenWorkingArea.Width;
  58. int windowHeight = screenWorkingArea.Height;
  59. // Get saved left and top positions
  60. if (Has(key_Left) && Has(key_Top))
  61. {
  62. windowLeftPosition = GetInt(key_Left);
  63. windowTopPosition = GetInt(key_Top);
  64. }
  65. // Get saved width and height
  66. if (Has(key_Width) && Has(key_Height))
  67. {
  68. windowWidth = GetInt(key_Width);
  69. windowHeight = GetInt(key_Height);
  70. }
  71. Rectangle formRect = new Rectangle(windowLeftPosition, windowTopPosition, windowWidth, windowHeight);
  72. // Check a visibility of form rectangle on any screen
  73. if (!IsVisibleOnAnyScreen(formRect))
  74. {
  75. form.StartPosition = FormStartPosition.WindowsDefaultLocation;
  76. }
  77. else
  78. {
  79. form.StartPosition = FormStartPosition.Manual;
  80. // setting a WPF window position is tricky. This is a helper method
  81. form.MoveWindow(formRect);
  82. }
  83. // Set the window state
  84. if (!ignoreWindowState)
  85. form.WindowState = GetBool(key_Maximized) ? FormWindowState.Maximized : FormWindowState.Normal;
  86. return GetBool(key_Maximized);
  87. }
  88. /// <summary>
  89. /// Gets font from font storage.
  90. /// </summary>
  91. /// <param name="formElement">Element name from font storage.</param>
  92. /// <param name="defaultFont">Default value.</param>
  93. /// <returns>The font object.</returns>
  94. public new Font GetFont(string formElement, Font defaultFont)
  95. {
  96. return base.GetFont(form.Name + "," + formElement, defaultFont);
  97. }
  98. /// <summary>
  99. /// Initializes a new instance of storage class.
  100. /// </summary>
  101. /// <param name="form">The form which state will be saved/restored.</param>
  102. public FormStorageService(Form form) : base(form, "Forms," + form.Name)
  103. {
  104. this.form = form;
  105. }
  106. }
  107. }