123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using FastReport.Utils;
- namespace FastReport.Forms
- {
- /// <summary>
- /// Base class for all forms.
- /// </summary>
- public partial class BaseForm : Form
- {
- private FormStorageService storage;
- private bool canSaveRestoreState;
- private int oldDpi;
- protected int NewDpi { get; private set; }
- /// <summary>
- /// Gets the form's storage service.
- /// </summary>
- public FormStorageService Storage
- {
- get
- {
- if (storage == null)
- storage = new FormStorageService(this);
- return storage;
- }
- }
- /// <summary>
- /// Gets or sets value indicating that the form can save/restore the state such as location and size.
- /// </summary>
- public bool CanSaveRestoreState
- {
- get => canSaveRestoreState;
- set => canSaveRestoreState = value;
- }
- /// <summary>
- /// The event occurs on form's dpi change.
- /// </summary>
- public event EventHandler DpiChanged;
- #region Dpi convenience methods
- /// <summary>
- /// Convenience method returns an image with specified index for this form's dpi.
- /// </summary>
- /// <param name="index">Image index.</param>
- /// <returns>The image.</returns>
- public Bitmap GetImage(int index)
- {
- return Res.GetImage(index, this.Dpi());
- }
- /// <summary>
- /// Convenience method returns an image with specified name for this form's dpi.
- /// </summary>
- /// <param name="resource">Image resource name.</param>
- /// <returns>The image.</returns>
- public Bitmap GetImage(string resource)
- {
- return Res.GetImage(resource, this.Dpi());
- }
- /// <summary>
- /// Convenience method returns an imagelist for this form's dpi.
- /// </summary>
- /// <returns>The imagelist.</returns>
- public ImageList GetImages()
- {
- return Res.GetImages(this.Dpi());
- }
- #endregion
- /// <summary>
- /// Localizes the dialog controls.
- /// </summary>
- /// <remarks>
- /// Use this method to set control's captions specific to the current locale.
- /// </remarks>
- public virtual void Localize()
- {
- }
- /// <summary>
- /// Saves the form's state.
- /// </summary>
- protected virtual void SaveState()
- {
- if (CanSaveRestoreState)
- Storage.SaveFormState();
- }
- /// <summary>
- /// Restores the form's state.
- /// </summary>
- protected virtual void RestoreState()
- {
- if (CanSaveRestoreState)
- Storage.RestoreFormState();
- }
- /// <inheritdoc/>
- protected override void OnFormClosed(FormClosedEventArgs e)
- {
- SaveState();
- base.OnFormClosed(e);
- }
- /// <inheritdoc/>
- protected override void OnLoad(EventArgs e)
- {
- RestoreState();
- base.OnLoad(e);
- }
- #if !MONO
- /// <inheritdoc/>
- protected override void WndProc(ref Message m)
- {
- const int WM_DPICHANGED = 0x02E0;
- if (m.Msg == WM_DPICHANGED)
- {
- // these properties if set to non-zero will lead to weird scaling on dpi change.
- // if you need them set them in the UpdateDpiDependencies method.
- MinimumSize = new Size(0, 0);
- MaximumSize = new Size(0, 0);
- }
- base.WndProc(ref m);
- if (m.Msg == WM_DPICHANGED)
- {
- NewDpi = this.Dpi();
- if (NewDpi != oldDpi)
- {
- UpdateDpiDependencies();
- if (DpiChanged != null)
- DpiChanged(this, EventArgs.Empty);
- oldDpi = NewDpi;
- }
- }
- }
- #elif (WPF || AVALONIA)
- /// <inheritdoc/>
- protected override void OnDpiChanged(object sender, EventArgs e)
- {
- base.OnDpiChanged(sender, e);
- NewDpi = this.Dpi();
- UpdateDpiDependencies();
- if (DpiChanged != null)
- DpiChanged(this, EventArgs.Empty);
- }
- #endif
- private void FixCheckBoxesAndRadioButtons(Control parent)
- {
- foreach (Control c in parent.Controls)
- {
- ButtonBase b = c as ButtonBase;
- if (b != null && b.AutoSize)
- {
- // it fixes the problem with AutoSize set incorrectly during autoscale
- if (b.Text != "" && !b.Text.EndsWith(" "))
- b.Text += " ";
- b.Width++;
- b.Width--;
- }
- FixCheckBoxesAndRadioButtons(c);
- }
- }
- /// <summary>
- /// Update controls on dpi change.
- /// </summary>
- /// <remarks>This method is called when the form's dpi is changed. Write custom logic to update
- /// some controls (such as ListBox.ItemHeight) here.
- /// </remarks>
- public virtual void UpdateDpiDependencies()
- {
- #if !MONO
- // Checkboxes and radiobuttons need special care. Sometimes they are not scaled properly
- // when a form moved to another monitor.
- FixCheckBoxesAndRadioButtons(this);
- #endif
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="BaseForm"/> class.
- /// </summary>
- public BaseForm()
- {
- InitializeComponent();
- this.Font = DrawUtils.DefaultFont;
- }
- }
- }
|