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

132 lines
3.2 KiB
C#
Raw Normal View History

2020-04-27 12:17:13 +00:00
namespace RyzStudio.Windows.ThemedForms
{
2020-05-07 22:31:03 +00:00
using System;
2020-05-10 10:03:55 +00:00
using System.ComponentModel;
using System.Drawing;
2020-04-27 12:17:13 +00:00
2020-05-10 10:45:54 +00:00
public partial class TButton : RyzStudio.Windows.ThemedForms.TUserControl
2020-04-27 12:17:13 +00:00
{
2020-05-10 10:03:55 +00:00
public class Style
{
public Color BackColour { get; set; } = Color.Transparent;
public Color PenColour { get; set; } = Color.Transparent;
public Image ForeImage { get; set; } = null;
}
public enum State
{
Idle = 0,
Hover,
Down
}
protected State controlState = State.Idle;
2020-04-27 12:17:13 +00:00
2020-05-10 10:45:54 +00:00
public TButton() : base()
2020-04-27 12:17:13 +00:00
{
InitializeComponent();
label1.ImageAlign = ContentAlignment.MiddleCenter;
label1.Click += delegate { this.OnClick(null); };
2020-05-10 10:03:55 +00:00
label1.MouseEnter += delegate { this.VisualState = State.Hover; };
label1.MouseLeave += delegate { this.VisualState = State.Idle; };
label1.MouseDown += delegate { this.VisualState = State.Down; };
label1.MouseUp += delegate { this.VisualState = State.Idle; };
2020-04-27 12:17:13 +00:00
}
2020-05-10 10:03:55 +00:00
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
2020-04-27 12:17:13 +00:00
// customise
2020-05-10 10:03:55 +00:00
this.StyleOver = new Style()
{
BackColour = Color.FromArgb(51, 51, 51),
PenColour = Color.White,
ForeImage = this.OverImage
};
this.StyleDown = new Style()
{
BackColour = Color.FromArgb(179, 179, 179),
PenColour = Color.Black,
ForeImage = this.DownImage
};
this.StyleDefault = new Style()
{
BackColour = Color.White,
PenColour = Color.Black,
ForeImage = this.DefaultImage
};
this.VisualState = State.Idle;
2020-05-07 22:31:03 +00:00
}
2020-04-27 12:17:13 +00:00
2020-05-10 10:03:55 +00:00
protected State VisualState
2020-04-27 12:17:13 +00:00
{
2020-05-10 10:03:55 +00:00
get { return controlState; }
2020-04-27 12:17:13 +00:00
set
{
switch (value)
{
2020-05-10 10:03:55 +00:00
case State.Idle:
if (this.VisualState == State.Down)
2020-04-27 12:17:13 +00:00
{
updateButton(StyleOver);
}
else
{
updateButton(StyleDefault);
}
break;
2020-05-10 10:03:55 +00:00
case State.Hover:
2020-04-27 12:17:13 +00:00
updateButton(StyleOver);
break;
2020-05-10 10:03:55 +00:00
case State.Down:
2020-04-27 12:17:13 +00:00
updateButton(StyleDown);
break;
default:
updateButton(StyleDefault);
break;
}
2020-05-10 10:03:55 +00:00
controlState = value;
2020-04-27 12:17:13 +00:00
}
}
2020-05-10 10:03:55 +00:00
protected void updateButton(Style style)
2020-04-27 12:17:13 +00:00
{
label1.ForeColor = style.PenColour;
label1.BackColor = style.BackColour;
label1.Image = style.ForeImage;
2020-05-07 22:31:03 +00:00
}
[Browsable(true)]
2020-04-27 12:17:13 +00:00
[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;
2020-05-10 10:03:55 +00:00
[Browsable(false)]
public Style StyleOver { get; set; } = new Style();
2020-04-27 12:17:13 +00:00
[Browsable(false)]
2020-05-10 10:03:55 +00:00
public Style StyleDown { get; set; } = new Style();
2020-04-27 12:17:13 +00:00
[Browsable(false)]
2020-05-10 10:03:55 +00:00
public Style StyleDefault { get; set; } = new Style();
2020-04-27 12:17:13 +00:00
public void PerformClick() => this.OnClick(null);
}
}