Histacom2/Histacom2.Engine/UI/ClassicButton.cs

101 lines
3.8 KiB
C#
Raw Normal View History

2017-08-22 20:44:03 -04:00
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
2017-08-23 13:38:40 -04:00
namespace Histacom2.Engine.UI
2017-08-22 20:44:03 -04:00
{
2017-09-12 09:51:26 -04:00
public class ClassicButton : Control, IButtonControl
2017-08-22 20:44:03 -04:00
{
2017-09-05 20:05:28 -04:00
private Color _lightBack;
private Color _darkBack;
2017-08-22 20:44:03 -04:00
2017-09-04 20:14:19 -04:00
private bool _pressing = false;
2017-08-22 20:44:03 -04:00
2017-09-20 11:02:43 -04:00
public DialogResult DialogResult { get; set; }
2017-09-12 09:51:26 -04:00
2017-09-20 11:02:43 -04:00
public bool AdaptBackColorWithTheme { get; set; }
public bool AdaptForeColorWithTheme { get; set; }
public bool AdaptFontWithTheme { get; set; }
2017-09-04 20:14:19 -04:00
public ClassicButton() : base()
2017-08-22 20:44:03 -04:00
{
2017-09-20 11:02:43 -04:00
AdaptBackColorWithTheme = true;
AdaptForeColorWithTheme = true;
AdaptFontWithTheme = true;
2017-09-04 20:14:19 -04:00
if (SaveSystem.currentTheme != null) BackColor = SaveSystem.currentTheme.threeDObjectsColor;
else BackColor = Color.Silver;
_lightBack = ControlPaint.Light(BackColor, 50);
_darkBack = ControlPaint.Dark(BackColor, 50);
2017-08-22 20:44:03 -04:00
2017-09-04 20:14:19 -04:00
MouseDown += (s, e) => { _pressing = true; Invalidate(); };
MouseUp += (s, e) => { _pressing = false; Invalidate(); };
2017-09-05 20:05:28 -04:00
Invalidate();
2017-08-22 20:44:03 -04:00
}
2017-09-04 20:14:19 -04:00
protected override void OnPaint(PaintEventArgs e)
2017-08-22 20:44:03 -04:00
{
2017-09-04 20:14:19 -04:00
base.OnPaint(e);
2017-09-05 20:05:28 -04:00
2017-09-20 00:37:10 -04:00
if (SaveSystem.currentTheme != null && AdaptBackColorWithTheme) BackColor = SaveSystem.currentTheme.threeDObjectsColor;
2017-09-05 20:05:28 -04:00
if (AdaptForeColorWithTheme)
{
if (SaveSystem.currentTheme != null) ForeColor = SaveSystem.currentTheme.threeDObjectsTextColor;
else ForeColor = Color.Black;
}
if (AdaptFontWithTheme)
{
if (SaveSystem.currentTheme != null) Font = SaveSystem.currentTheme.buttonFont;
else Font = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Regular);
}
2017-09-05 20:05:28 -04:00
_lightBack = Paintbrush.GetLightFromColor(BackColor);
_darkBack = Paintbrush.GetDarkFromColor(BackColor);
2017-09-04 20:14:19 -04:00
var g = e.Graphics;
g.Clear(BackColor);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
2017-09-05 20:05:28 -04:00
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
2017-09-23 12:23:10 -04:00
sf.LineAlignment = StringAlignment.Center;
2017-09-05 20:05:28 -04:00
sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
2017-09-04 20:14:19 -04:00
2017-09-23 12:23:10 -04:00
if (_pressing && Enabled)
2017-09-04 20:14:19 -04:00
{
g.FillRectangle(new SolidBrush(_lightBack), new Rectangle(0, 0, Width, Height));
g.FillRectangle(Brushes.Black, new Rectangle(0, 0, Width - 1, Height - 1));
g.FillRectangle(new SolidBrush(_darkBack), new Rectangle(1, 1, Width - 2, Height - 2));
g.FillRectangle(new SolidBrush(BackColor), new Rectangle(2, 2, Width - 3, Height - 3));
2017-09-23 12:23:10 -04:00
g.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(2, 2, Width - 3, Height - 3), sf);
2017-09-04 20:14:19 -04:00
}
else
{
g.FillRectangle(Brushes.Black, new Rectangle(0, 0, Width, Height));
g.FillRectangle(new SolidBrush(_lightBack), new Rectangle(0, 0, Width - 1, Height - 1));
g.FillRectangle(new SolidBrush(_darkBack), new Rectangle(1, 1, Width - 2, Height - 2));
g.FillRectangle(new SolidBrush(BackColor), new Rectangle(1, 1, Width - 3, Height - 3));
2017-09-23 12:23:10 -04:00
if (Enabled) g.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(1, 1, Width - 3, Height - 3), sf);
else g.DrawString(Text, Font, new SolidBrush(_darkBack), new Rectangle(1, 1, Width - 3, Height - 3), sf);
2017-09-04 20:14:19 -04:00
}
2017-08-22 20:44:03 -04:00
}
2017-09-12 09:51:26 -04:00
public void NotifyDefault(bool value)
{
}
public void PerformClick()
{
this.OnClick(new EventArgs());
}
2017-08-22 20:44:03 -04:00
}
2017-09-04 20:14:19 -04:00
}