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.4 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-12 23:37:01 +00:00
public class ButtonStyle
2020-05-10 10:03:55 +00:00
{
public Color BackColour { get; set; } = Color.Transparent;
public Color PenColour { get; set; } = Color.Transparent;
public Image ForeImage { get; set; } = null;
}
2020-05-12 23:37:01 +00:00
public enum ButtonState
2020-05-10 10:03:55 +00:00
{
Idle = 0,
Hover,
Down
}
2020-05-12 23:37:01 +00:00
protected ButtonState controlState = ButtonState.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-12 23:37:01 +00:00
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; };
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-12 23:37:01 +00:00
this.StyleOver = new ButtonStyle()
2020-05-10 10:03:55 +00:00
{
BackColour = Color.FromArgb(51, 51, 51),
PenColour = Color.White,
ForeImage = this.OverImage
};
2020-05-12 23:37:01 +00:00
this.StyleDown = new ButtonStyle()
2020-05-10 10:03:55 +00:00
{
BackColour = Color.FromArgb(179, 179, 179),
PenColour = Color.Black,
ForeImage = this.DownImage
};
2020-05-12 23:37:01 +00:00
this.StyleDefault = new ButtonStyle()
2020-05-10 10:03:55 +00:00
{
BackColour = Color.White,
PenColour = Color.Black,
ForeImage = this.DefaultImage
};
2020-05-12 23:37:01 +00:00
this.VisualState = ButtonState.Idle;
2020-05-07 22:31:03 +00:00
}
2020-04-27 12:17:13 +00:00
2020-05-12 23:37:01 +00:00
protected ButtonState 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-12 23:37:01 +00:00
case ButtonState.Idle:
if (this.VisualState == ButtonState.Down)
2020-04-27 12:17:13 +00:00
{
updateButton(StyleOver);
}
else
{
updateButton(StyleDefault);
}
break;
2020-05-12 23:37:01 +00:00
case ButtonState.Hover:
2020-04-27 12:17:13 +00:00
updateButton(StyleOver);
break;
2020-05-12 23:37:01 +00:00
case ButtonState.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-12 23:37:01 +00:00
protected void updateButton(ButtonStyle 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)]
2020-05-12 23:37:01 +00:00
public ButtonStyle StyleOver { get; set; } = new ButtonStyle();
2020-04-27 12:17:13 +00:00
[Browsable(false)]
2020-05-12 23:37:01 +00:00
public ButtonStyle StyleDown { get; set; } = new ButtonStyle();
2020-04-27 12:17:13 +00:00
[Browsable(false)]
2020-05-12 23:37:01 +00:00
public ButtonStyle StyleDefault { get; set; } = new ButtonStyle();
2020-04-27 12:17:13 +00:00
public void PerformClick() => this.OnClick(null);
}
}