-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlatCheckBox.cs
More file actions
122 lines (107 loc) · 4.44 KB
/
Copy pathFlatCheckBox.cs
File metadata and controls
122 lines (107 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace GigaPrime3D
{
/// <summary>
/// Owner-drawn dark check box used in place of the stock WinForms CheckBox so
/// the sidebar matches the WPF demo's dark scientific look.
///
/// Replaces the WPF CheckBox whose Checked / Unchecked event pair becomes a
/// single CheckedChanged event here (reading .Checked). Setting Checked to its
/// current value is a no-op, so the _updatingUi guard pattern works.
/// </summary>
public class FlatCheckBox : Control
{
// ---- Theme -------------------------------------------------------------
public static Color BoxColor = Color.FromArgb(255, 0x3C, 0x42, 0x4A); // box fill (unchecked)
public static Color BoxBorder = Color.FromArgb(255, 0x6A, 0x72, 0x7C);
public static Color AccentColor = Color.FromArgb(255, 0x5A, 0xAA, 0xEB); // box fill (checked) + check
public static Color CheckColor = Color.FromArgb(255, 0xFF, 0xFF, 0xFF);
public static Color LabelColor = Color.FromArgb(255, 0xFF, 0xFF, 0xFF);
// ------------------------------------------------------------------------
private bool _checked = false;
private const int BoxSize = 18;
private const int Gap = 8;
public event EventHandler CheckedChanged;
public FlatCheckBox()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor | ControlStyles.Selectable, true);
BackColor = Color.Transparent;
ForeColor = LabelColor;
TabStop = true;
Height = 26;
Width = 160;
Cursor = Cursors.Hand;
Font = new Font("Segoe UI", 11f);
}
public bool Checked
{
get => _checked;
set
{
if (value == _checked) return;
_checked = value;
Invalidate();
CheckedChanged?.Invoke(this, EventArgs.Empty);
}
}
protected override void OnPaint(PaintEventArgs e)
{
var g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
int boxY = (Height - BoxSize) / 2;
var box = new Rectangle(1, boxY, BoxSize, BoxSize);
using (var b = new SolidBrush(_checked ? AccentColor : BoxColor))
g.FillRectangle(b, box);
using (var p = new Pen(_checked ? AccentColor : BoxBorder))
g.DrawRectangle(p, box);
if (_checked)
{
using (var p = new Pen(CheckColor, 2f))
{
g.DrawLines(p, new[]
{
new Point(box.Left + 4, box.Top + 9),
new Point(box.Left + 8, box.Top + 13),
new Point(box.Left + 14, box.Top + 5),
});
}
}
var textRect = new Rectangle(BoxSize + Gap, 0, Width - BoxSize - Gap, Height);
TextRenderer.DrawText(g, Text, Font, textRect, LabelColor,
TextFormatFlags.Left | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
if (Focused)
{
using (var p = new Pen(AccentColor) { DashStyle = DashStyle.Dot })
g.DrawRectangle(p, Rectangle.Inflate(box, 2, 2));
}
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
Focus();
Checked = !_checked;
}
protected override bool IsInputKey(Keys keyData)
{
if (keyData == Keys.Space) return true;
return base.IsInputKey(keyData);
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Space)
{
Checked = !_checked;
e.Handled = true;
}
}
protected override void OnGotFocus(EventArgs e) { base.OnGotFocus(e); Invalidate(); }
protected override void OnLostFocus(EventArgs e) { base.OnLostFocus(e); Invalidate(); }
protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); Invalidate(); }
}
}