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/ThemedForms/TButton.cs
2020-05-13 00:37:01 +01:00

132 lines
3.4 KiB
C#

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;
public TButton() : base()
{
InitializeComponent();
label1.ImageAlign = ContentAlignment.MiddleCenter;
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);
// customise
this.StyleOver = new ButtonStyle()
{
BackColour = Color.FromArgb(51, 51, 51),
PenColour = Color.White,
ForeImage = this.OverImage
};
this.StyleDown = new ButtonStyle()
{
BackColour = Color.FromArgb(179, 179, 179),
PenColour = Color.Black,
ForeImage = this.DownImage
};
this.StyleDefault = new ButtonStyle()
{
BackColour = Color.White,
PenColour = Color.Black,
ForeImage = this.DefaultImage
};
this.VisualState = ButtonState.Idle;
}
protected ButtonState VisualState
{
get { return controlState; }
set
{
switch (value)
{
case ButtonState.Idle:
if (this.VisualState == ButtonState.Down)
{
updateButton(StyleOver);
}
else
{
updateButton(StyleDefault);
}
break;
case ButtonState.Hover:
updateButton(StyleOver);
break;
case ButtonState.Down:
updateButton(StyleDown);
break;
default:
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;
[Browsable(false)]
public ButtonStyle StyleOver { get; set; } = new ButtonStyle();
[Browsable(false)]
public ButtonStyle StyleDown { get; set; } = new ButtonStyle();
[Browsable(false)]
public ButtonStyle StyleDefault { get; set; } = new ButtonStyle();
public void PerformClick() => this.OnClick(null);
}
}