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;
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);
}
///
/// 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);
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);
}
///
/// 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;
}
}
}