This repository has been archived on 2024-08-06. You can view files and clone it, but cannot push or open issues or pull requests.
linear-app-launcher/RyzStudio/Windows/Forms/FlatButton.cs

125 lines
3.4 KiB
C#
Raw Normal View History

2020-05-10 10:03:55 +00:00
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();
2020-05-10 10:34:45 +00:00
public void PerformClick()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(() => {
this.OnClick(null);
}));
}
else
{
this.OnClick(null);
}
}
2020-05-10 10:03:55 +00:00
}
}