123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- namespace System.Windows.Forms
- {
- public class ButtonBase : Control
- {
- protected new System.Windows.Controls.Primitives.ButtonBase control { get; private set; }
-
- public FlatButtonAppearance FlatAppearance { get; }
- private FlatStyle flatStyle;
- public FlatStyle FlatStyle
- {
- get => flatStyle;
- set
- {
- flatStyle = value;
- if (value != FlatStyle.Standard)
- {
- Padding = Padding.Empty;
- BackColor = Drawing.Color.Transparent;
- if (control is CustomControls.ButtonBase btn)
- btn.IsFlat = true;
- }
- }
- }
- private System.Drawing.Image image;
- public System.Drawing.Image Image
- {
- get => image;
- set
- {
- if (image != value)
- {
- image = value;
- UpdateControlImage(Helper.GetImage(Image));
- }
- }
- }
- private System.Drawing.ContentAlignment imageAlign;
- public System.Drawing.ContentAlignment ImageAlign
- {
- get => imageAlign;
- set
- {
- imageAlign = value;
- UpdateContentAlignment();
- }
- }
- private System.Drawing.ContentAlignment textAlign;
- public System.Drawing.ContentAlignment TextAlign
- {
- get => textAlign;
- set
- {
- textAlign = value;
- UpdateContentAlignment();
- }
- }
- public TextImageRelation TextImageRelation { get; set; } // TODO?
- public override string Text
- {
- get
- {
- if (control.Content is System.Windows.Controls.TextBlock tb)
- return tb.Text;
- return control.Content?.ToString();
- }
- set
- {
- value = value?.Replace("\t", "");
- if (control.Content is System.Windows.Controls.TextBlock tb)
- tb.Text = value;
- else
- control.Content = value;
- base.Text = value;
- }
- }
- public bool UseVisualStyleBackColor { get; set; }
- public bool UseCompatibleTextRendering { get; set; }
- protected virtual void UpdateContentAlignment()
- {
- if (control == null)
- return;
- // we use TextAlign only in FR
- Helper.GetContentAlignment(TextAlign, out Windows.HorizontalAlignment h, out Windows.VerticalAlignment v);
- control.HorizontalContentAlignment = h;
- control.VerticalContentAlignment = v;
- }
- protected virtual void UpdateControlImage(System.Windows.Media.ImageSource image) { }
- protected void SetControl(System.Windows.Controls.Primitives.ButtonBase control)
- {
- base.SetControl(control);
- this.control = control;
- control.Click += (sender, e) => OnClick(e);
- UpdateContentAlignment();
- }
- public ButtonBase()
- {
- FlatAppearance = new FlatButtonAppearance(this);
- FlatStyle = FlatStyle.Standard;
- ImageAlign = System.Drawing.ContentAlignment.MiddleCenter;
- TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
- }
- }
- }
|