random-file-runner/RyzStudio/Windows/ThemedForms/Button/TButton.cs

186 lines
5.2 KiB
C#
Raw Permalink Normal View History

2021-10-24 15:49:48 +00:00
namespace RyzStudio.Windows.ThemedForms
{
using RyzStudio.Windows.Forms;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
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()
{
InitializeComponent();
this.Margin = new Padding(10, 0, 0, 10);
this.Name = "Button";
label1.ImageAlign = ContentAlignment.MiddleCenter;
label1.Click += label1_Click;
label1.MouseClick += label1_MouseClick;
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.Margin = new Padding(10, 0, 10, 10);
this.StyleDefault = new ButtonStyle()
{
BackColour = Color.White,
PenColour = Color.Black,
ForeImage = this.DefaultImage
};
this.StyleDown = new ButtonStyle()
{
BackColour = Color.FromArgb(60, 53, 148),
PenColour = Color.White,
ForeImage = this.DownImage
};
this.StyleOver = new ButtonStyle()
{
BackColour = Color.FromArgb(108, 101, 196),
PenColour = Color.White,
ForeImage = this.OverImage
};
this.StyleSelected = new ButtonStyle()
{
BackColour = Color.FromArgb(51, 51, 51),
PenColour = Color.White,
ForeImage = this.OverImage
};
this.VisualState = ButtonState.Idle;
}
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new Padding Margin { get => base.Margin; set => base.Margin = value; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new Padding Padding { get => base.Padding; set => base.Padding = value; }
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 => ThreadControl.GetText(label1); set => ThreadControl.SetText(label1, 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();
private void label1_MouseClick(object sender, MouseEventArgs e) => this.OnMouseClick(e);
private void label1_Click(object sender, EventArgs e) => this.OnClick(e);
public void PerformClick() => this.OnClick(null);
}
}