123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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 int oldDpi;
- protected int NewDpi { get; private set; }
- /// <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()
- {
- }
- #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;
- }
- }
- }
- #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;
- }
- }
- }
|