123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- using System.Drawing;
- using System.Windows.Media;
- namespace System.Windows.Forms
- {
- public class Button : ButtonBase
- {
- protected new CustomControls.Button control { get; }
- public AutoSizeMode AutoSizeMode { get; set; }
- public DialogResult DialogResult { get; set; }
- private bool autoSize;
- public new bool AutoSize
- {
- get => autoSize;
- set
- {
- autoSize = value;
- UpdateSize();
- }
- }
- public override Image BackgroundImage
- {
- get => base.BackgroundImage;
- set
- {
- base.BackgroundImage = value;
- control.BackgroundImage = Helper.GetImage(value, DeviceDpi, DeviceDpi);
- }
- }
- private void UpdateSize()
- {
- if (autoSize)
- {
- var width = Width;
- var height = Height;
- ResetAutoSizeValue(true);
- var size = AutoSizeValue;
- // restore control size after reset/autosizevalue calls. Setting Width and Height does not update control size always
- SetControlWidth(width);
- SetControlHeight(height);
- if (AutoSizeMode == AutoSizeMode.GrowOnly)
- {
- Width = size.Width > width ? size.Width + 4 : width;
- Height = size.Height > height ? size.Height : height;
- }
- else
- {
- Width = size.Width + 4; // padding
- Height = size.Height;
- }
-
- if ((Anchor & AnchorStyles.Right) != 0)
- {
- Left += width - Width;
- }
- }
- }
- public override string Text
- {
- get => control.Text;
- set
- {
- control.Text = value;
- UpdateSize();
- }
- }
- protected override void UpdateContentAlignment()
- {
- base.UpdateContentAlignment();
- // SWF consistency
- if (control != null && control.HorizontalContentAlignment == Windows.HorizontalAlignment.Left)
- control.Padding = new Thickness(1, 1, 0, 0);
- }
- protected override void UpdateControlImage(ImageSource image)
- {
- control.Image = image;
- UpdateSize();
- }
- protected override void OnClick(EventArgs e)
- {
- base.OnClick(e);
- var form = FindForm();
- if (form != null && DialogResult != DialogResult.None)
- form.DialogResult = DialogResult;
- }
- protected override void ScaleCore(float dx, float dy)
- {
- base.ScaleCore(dx, dy);
- if (autoSize)
- UpdateSize();
- }
- public Button()
- {
- control = new();
- SetControl(control);
- BackColor = System.Drawing.SystemColors.ControlLightLight;
- Width = 75;
- Height = 23;
- }
- }
- }
|