namespace RyzStudio.Windows.ThemedForms { using System; using System.ComponentModel; using System.Drawing; public partial class TButton : RyzStudio.Windows.ThemedForms.TUserControl { public class ButtonStyle { public Color BackColour { get; set; } = Color.Transparent; public Color PenColour { get; set; } = Color.Transparent; public Image ForeImage { get; set; } = null; } public enum ButtonState { Idle = 0, Hover, Down } protected ButtonState controlState = ButtonState.Idle; protected bool isSelected = false; public TButton() : base() { InitializeComponent(); label1.ImageAlign = ContentAlignment.MiddleCenter; this.StyleDefault = new ButtonStyle() { BackColour = Color.White, PenColour = Color.Black, ForeImage = this.DefaultImage }; this.StyleDown = new ButtonStyle() { BackColour = Color.FromArgb(179, 179, 179), PenColour = Color.Black, ForeImage = this.DownImage }; this.StyleOver = new ButtonStyle() { BackColour = Color.FromArgb(51, 51, 51), PenColour = Color.White, ForeImage = this.OverImage }; this.StyleSelected = new ButtonStyle() { BackColour = Color.FromArgb(51, 51, 51), PenColour = Color.White, ForeImage = this.OverImage }; label1.Click += delegate { this.OnClick(null); }; label1.MouseEnter += delegate { this.VisualState = ButtonState.Hover; }; label1.MouseLeave += delegate { this.VisualState = ButtonState.Idle; }; label1.MouseDown += delegate { this.VisualState = ButtonState.Down; }; label1.MouseUp += delegate { this.VisualState = ButtonState.Idle; }; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.VisualState = ButtonState.Idle; } protected ButtonState VisualState { get { return controlState; } set { switch (value) { case ButtonState.Idle: if (this.VisualState == ButtonState.Down) { updateButton(StyleOver); } else { if (this.IsSelected) { updateButton(StyleSelected); } else { updateButton(StyleDefault); } } break; case ButtonState.Hover: updateButton(StyleOver); break; case ButtonState.Down: updateButton(StyleDown); break; default: if (this.IsSelected) { updateButton(StyleSelected); } else { updateButton(StyleDefault); } break; } controlState = value; } } protected void updateButton(ButtonStyle style) { label1.ForeColor = style.PenColour; label1.BackColor = style.BackColour; label1.Image = style.ForeImage; } [Browsable(true)] [Category("Appearance")] public string LabelText { get => label1.Text; set => label1.Text = value; } [Browsable(true)] [Category("Appearance")] public Image OverImage { get; set; } = null; [Browsable(true)] [Category("Appearance")] public Image DownImage { get; set; } = null; [Browsable(true)] [Category("Appearance")] public Image DefaultImage { get; set; } = null; public bool IsSelected { get => isSelected; set { isSelected = value; this.VisualState = this.VisualState; } } protected ButtonStyle StyleOver { get; set; } = new ButtonStyle(); protected ButtonStyle StyleDown { get; set; } = new ButtonStyle(); protected ButtonStyle StyleDefault { get; set; } = new ButtonStyle(); protected ButtonStyle StyleSelected { get; set; } = new ButtonStyle(); public void PerformClick() => this.OnClick(null); } }