ButtonBase.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. namespace System.Windows.Forms
  2. {
  3. public class ButtonBase : Control
  4. {
  5. protected new System.Windows.Controls.Primitives.ButtonBase control { get; private set; }
  6. public FlatButtonAppearance FlatAppearance { get; }
  7. private FlatStyle flatStyle;
  8. public FlatStyle FlatStyle
  9. {
  10. get => flatStyle;
  11. set
  12. {
  13. flatStyle = value;
  14. if (value != FlatStyle.Standard)
  15. {
  16. Padding = Padding.Empty;
  17. BackColor = Drawing.Color.Transparent;
  18. if (control is CustomControls.ButtonBase btn)
  19. btn.IsFlat = true;
  20. }
  21. }
  22. }
  23. private System.Drawing.Image image;
  24. public System.Drawing.Image Image
  25. {
  26. get => image;
  27. set
  28. {
  29. if (image != value)
  30. {
  31. image = value;
  32. UpdateControlImage(Helper.GetImage(Image));
  33. }
  34. }
  35. }
  36. private System.Drawing.ContentAlignment imageAlign;
  37. public System.Drawing.ContentAlignment ImageAlign
  38. {
  39. get => imageAlign;
  40. set
  41. {
  42. imageAlign = value;
  43. UpdateContentAlignment();
  44. }
  45. }
  46. private System.Drawing.ContentAlignment textAlign;
  47. public System.Drawing.ContentAlignment TextAlign
  48. {
  49. get => textAlign;
  50. set
  51. {
  52. textAlign = value;
  53. UpdateContentAlignment();
  54. }
  55. }
  56. public TextImageRelation TextImageRelation { get; set; } // TODO?
  57. public override string Text
  58. {
  59. get
  60. {
  61. if (control.Content is System.Windows.Controls.TextBlock tb)
  62. return tb.Text;
  63. return control.Content?.ToString();
  64. }
  65. set
  66. {
  67. value = value?.Replace("\t", "");
  68. if (control.Content is System.Windows.Controls.TextBlock tb)
  69. tb.Text = value;
  70. else
  71. control.Content = value;
  72. base.Text = value;
  73. }
  74. }
  75. public bool UseVisualStyleBackColor { get; set; }
  76. public bool UseCompatibleTextRendering { get; set; }
  77. protected virtual void UpdateContentAlignment()
  78. {
  79. if (control == null)
  80. return;
  81. // we use TextAlign only in FR
  82. Helper.GetContentAlignment(TextAlign, out Windows.HorizontalAlignment h, out Windows.VerticalAlignment v);
  83. control.HorizontalContentAlignment = h;
  84. control.VerticalContentAlignment = v;
  85. }
  86. protected virtual void UpdateControlImage(System.Windows.Media.ImageSource image) { }
  87. protected void SetControl(System.Windows.Controls.Primitives.ButtonBase control)
  88. {
  89. base.SetControl(control);
  90. this.control = control;
  91. control.Click += (sender, e) => OnClick(e);
  92. UpdateContentAlignment();
  93. }
  94. public ButtonBase()
  95. {
  96. FlatAppearance = new FlatButtonAppearance(this);
  97. FlatStyle = FlatStyle.Standard;
  98. ImageAlign = System.Drawing.ContentAlignment.MiddleCenter;
  99. TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  100. }
  101. }
  102. }