Button.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Drawing;
  2. using System.Windows.Media;
  3. namespace System.Windows.Forms
  4. {
  5. public class Button : ButtonBase
  6. {
  7. protected new CustomControls.Button control { get; }
  8. public AutoSizeMode AutoSizeMode { get; set; }
  9. public DialogResult DialogResult { get; set; }
  10. private bool autoSize;
  11. public new bool AutoSize
  12. {
  13. get => autoSize;
  14. set
  15. {
  16. autoSize = value;
  17. UpdateSize();
  18. }
  19. }
  20. public override Image BackgroundImage
  21. {
  22. get => base.BackgroundImage;
  23. set
  24. {
  25. base.BackgroundImage = value;
  26. control.BackgroundImage = Helper.GetImage(value, DeviceDpi, DeviceDpi);
  27. }
  28. }
  29. private void UpdateSize()
  30. {
  31. if (autoSize)
  32. {
  33. var width = Width;
  34. var height = Height;
  35. ResetAutoSizeValue(true);
  36. var size = AutoSizeValue;
  37. // restore control size after reset/autosizevalue calls. Setting Width and Height does not update control size always
  38. SetControlWidth(width);
  39. SetControlHeight(height);
  40. if (AutoSizeMode == AutoSizeMode.GrowOnly)
  41. {
  42. Width = size.Width > width ? size.Width + 4 : width;
  43. Height = size.Height > height ? size.Height : height;
  44. }
  45. else
  46. {
  47. Width = size.Width + 4; // padding
  48. Height = size.Height;
  49. }
  50. if ((Anchor & AnchorStyles.Right) != 0)
  51. {
  52. Left += width - Width;
  53. }
  54. }
  55. }
  56. public override string Text
  57. {
  58. get => control.Text;
  59. set
  60. {
  61. control.Text = value;
  62. UpdateSize();
  63. }
  64. }
  65. protected override void UpdateContentAlignment()
  66. {
  67. base.UpdateContentAlignment();
  68. // SWF consistency
  69. if (control != null && control.HorizontalContentAlignment == Windows.HorizontalAlignment.Left)
  70. control.Padding = new Thickness(1, 1, 0, 0);
  71. }
  72. protected override void UpdateControlImage(ImageSource image)
  73. {
  74. control.Image = image;
  75. UpdateSize();
  76. }
  77. protected override void OnClick(EventArgs e)
  78. {
  79. base.OnClick(e);
  80. var form = FindForm();
  81. if (form != null && DialogResult != DialogResult.None)
  82. form.DialogResult = DialogResult;
  83. }
  84. protected override void ScaleCore(float dx, float dy)
  85. {
  86. base.ScaleCore(dx, dy);
  87. if (autoSize)
  88. UpdateSize();
  89. }
  90. public Button()
  91. {
  92. control = new();
  93. SetControl(control);
  94. BackColor = System.Drawing.SystemColors.ControlLightLight;
  95. Width = 75;
  96. Height = 23;
  97. }
  98. }
  99. }