BaseForm.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using FastReport.Utils;
  5. namespace FastReport.Forms
  6. {
  7. /// <summary>
  8. /// Base class for all forms.
  9. /// </summary>
  10. public partial class BaseForm : Form
  11. {
  12. private int oldDpi;
  13. protected int NewDpi { get; private set; }
  14. /// <summary>
  15. /// The event occurs on form's dpi change.
  16. /// </summary>
  17. public event EventHandler DpiChanged;
  18. #region Dpi convenience methods
  19. /// <summary>
  20. /// Convenience method returns an image with specified index for this form's dpi.
  21. /// </summary>
  22. /// <param name="index">Image index.</param>
  23. /// <returns>The image.</returns>
  24. public Bitmap GetImage(int index)
  25. {
  26. return Res.GetImage(index, this.Dpi());
  27. }
  28. /// <summary>
  29. /// Convenience method returns an image with specified name for this form's dpi.
  30. /// </summary>
  31. /// <param name="resource">Image resource name.</param>
  32. /// <returns>The image.</returns>
  33. public Bitmap GetImage(string resource)
  34. {
  35. return Res.GetImage(resource, this.Dpi());
  36. }
  37. /// <summary>
  38. /// Convenience method returns an imagelist for this form's dpi.
  39. /// </summary>
  40. /// <returns>The imagelist.</returns>
  41. public ImageList GetImages()
  42. {
  43. return Res.GetImages(this.Dpi());
  44. }
  45. #endregion
  46. /// <summary>
  47. /// Localizes the dialog controls.
  48. /// </summary>
  49. /// <remarks>
  50. /// Use this method to set control's captions specific to the current locale.
  51. /// </remarks>
  52. public virtual void Localize()
  53. {
  54. }
  55. #if !MONO
  56. /// <inheritdoc/>
  57. protected override void WndProc(ref Message m)
  58. {
  59. const int WM_DPICHANGED = 0x02E0;
  60. if (m.Msg == WM_DPICHANGED)
  61. {
  62. // these properties if set to non-zero will lead to weird scaling on dpi change.
  63. // if you need them set them in the UpdateDpiDependencies method.
  64. MinimumSize = new Size(0, 0);
  65. MaximumSize = new Size(0, 0);
  66. }
  67. base.WndProc(ref m);
  68. if (m.Msg == WM_DPICHANGED)
  69. {
  70. NewDpi = this.Dpi();
  71. if (NewDpi != oldDpi)
  72. {
  73. UpdateDpiDependencies();
  74. if (DpiChanged != null)
  75. DpiChanged(this, EventArgs.Empty);
  76. oldDpi = NewDpi;
  77. }
  78. }
  79. }
  80. #endif
  81. private void FixCheckBoxesAndRadioButtons(Control parent)
  82. {
  83. foreach (Control c in parent.Controls)
  84. {
  85. ButtonBase b = c as ButtonBase;
  86. if (b != null && b.AutoSize)
  87. {
  88. // it fixes the problem with AutoSize set incorrectly during autoscale
  89. if (b.Text != "" && !b.Text.EndsWith(" "))
  90. b.Text += " ";
  91. b.Width++;
  92. b.Width--;
  93. }
  94. FixCheckBoxesAndRadioButtons(c);
  95. }
  96. }
  97. /// <summary>
  98. /// Update controls on dpi change.
  99. /// </summary>
  100. /// <remarks>This method is called when the form's dpi is changed. Write custom logic to update
  101. /// some controls (such as ListBox.ItemHeight) here.
  102. /// </remarks>
  103. public virtual void UpdateDpiDependencies()
  104. {
  105. #if !MONO
  106. // Checkboxes and radiobuttons need special care. Sometimes they are not scaled properly
  107. // when a form moved to another monitor.
  108. FixCheckBoxesAndRadioButtons(this);
  109. #endif
  110. }
  111. /// <summary>
  112. /// Initializes a new instance of the <see cref="BaseForm"/> class.
  113. /// </summary>
  114. public BaseForm()
  115. {
  116. InitializeComponent();
  117. this.Font = DrawUtils.DefaultFont;
  118. }
  119. }
  120. }