using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace RyzStudio.Windows.Forms { public class FlatButton : Label { public class Style { public Color BackColour { get; set; } = Color.Transparent; public Color PenColour { get; set; } = Color.Transparent; } public enum State { Idle = 0, Hover, Down } protected State controlState = State.Idle; public FlatButton() : base() { this.AutoSize = false; this.ImageAlign = ContentAlignment.MiddleCenter; this.TextAlign = ContentAlignment.MiddleCenter; // customise this.StyleOver = new Style() { BackColour = Color.FromArgb(51, 51, 51), PenColour = Color.White }; this.StyleDown = new Style() { BackColour = Color.FromArgb(179, 179, 179), PenColour = Color.Black }; this.StyleDefault = new Style() { BackColour = Color.White, PenColour = Color.Black }; this.VisualState = State.Idle; this.Click += delegate { this.OnClick(null); }; this.MouseEnter += delegate { this.VisualState = State.Hover; }; this.MouseLeave += delegate { this.VisualState = State.Idle; }; this.MouseDown += delegate { this.VisualState = State.Down; }; this.MouseUp += delegate { this.VisualState = State.Idle; }; } protected State VisualState { get { return controlState; } set { switch (value) { case State.Idle: if (this.VisualState == State.Down) { updateButton(StyleOver); } else { updateButton(StyleDefault); } break; case State.Hover: updateButton(StyleOver); break; case State.Down: updateButton(StyleDown); break; default: updateButton(StyleDefault); break; } controlState = value; } } protected void updateButton(Style style) { this.ForeColor = style.PenColour; this.BackColor = style.BackColour; } [Browsable(false)] public Style StyleOver { get; set; } = new Style(); [Browsable(false)] public Style StyleDown { get; set; } = new Style(); [Browsable(false)] public Style StyleDefault { get; set; } = new Style(); public void PerformClick() => this.OnClick(null); } }