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; } } }