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 FlatButtonStyle { public Color BackColour { get; set; } = Color.Transparent; public Color PenColour { get; set; } = Color.Transparent; } public enum FlatButtonState { Idle = 0, Hover, Down } protected FlatButtonState controlState = FlatButtonState.Idle; public FlatButton() : base() { this.AutoSize = false; this.ImageAlign = ContentAlignment.MiddleCenter; this.TextAlign = ContentAlignment.MiddleCenter; // customise this.StyleOver = new FlatButtonStyle() { BackColour = Color.FromArgb(51, 51, 51), PenColour = Color.White }; this.StyleDown = new FlatButtonStyle() { BackColour = Color.FromArgb(179, 179, 179), PenColour = Color.Black }; this.StyleDefault = new FlatButtonStyle() { BackColour = Color.White, PenColour = Color.Black }; this.VisualState = FlatButtonState.Idle; this.Click += delegate { this.OnClick(null); }; this.MouseEnter += delegate { this.VisualState = FlatButtonState.Hover; }; this.MouseLeave += delegate { this.VisualState = FlatButtonState.Idle; }; this.MouseDown += delegate { this.VisualState = FlatButtonState.Down; }; this.MouseUp += delegate { this.VisualState = FlatButtonState.Idle; }; } protected FlatButtonState VisualState { get { return controlState; } set { switch (value) { case FlatButtonState.Idle: if (this.VisualState == FlatButtonState.Down) { updateButton(StyleOver); } else { updateButton(StyleDefault); } break; case FlatButtonState.Hover: updateButton(StyleOver); break; case FlatButtonState.Down: updateButton(StyleDown); break; default: updateButton(StyleDefault); break; } controlState = value; } } protected void updateButton(FlatButtonStyle style) { this.ForeColor = style.PenColour; this.BackColor = style.BackColour; } [Browsable(false)] public FlatButtonStyle StyleOver { get; set; } = new FlatButtonStyle(); [Browsable(false)] public FlatButtonStyle StyleDown { get; set; } = new FlatButtonStyle(); [Browsable(false)] public FlatButtonStyle StyleDefault { get; set; } = new FlatButtonStyle(); public void PerformClick() { if (this.InvokeRequired) { this.Invoke(new MethodInvoker(() => { this.OnClick(null); })); } else { this.OnClick(null); } } } }