FlatCheck.cs
7.0 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace Asa.Theme
{
/// <summary>
/// 复选框控件
/// </summary>
[DefaultProperty("Checked")]
[DefaultEvent("CheckedChanged")]
public partial class FlatCheck : FlatBase
{
private bool _check;
private GraphicsPath _switchPath = new GraphicsPath();
private PointF _switchPoint = new PointF();
private RectangleF _textRect = new RectangleF();
private TextSwitchRelation _relation;
private int _radius;
private int _between;
/// <summary>
/// 复选框控件
/// </summary>
public FlatCheck()
{
InitializeComponent();
_relation = TextSwitchRelation.SwitchBeforeText;
_radius = 9;
_between = 20;
Text = "";
}
/// <summary>
/// 显示的文本
/// </summary>
[Browsable(true), Category(""), Description("显示的文本,用英文逗号分隔可以切换开/关的文本"), DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get => base.Text;
set
{
base.Text = value;
CalcSize();
Refresh();
}
}
/// <summary>
/// 显示的文本的字体
/// </summary>
[Browsable(true), Category(""), Description("显示的文本的字体")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Font Font
{
set
{
base.Font = value;
CalcSize();
Refresh();
}
get
{
return base.Font;
}
}
/// <summary>
/// 组件是否处于选中状态
/// </summary>
[Browsable(true), Category(""), Description("组件是否处于选中状态"), DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool Checked
{
set
{
_check = value;
CalcSize();
Refresh();
CheckedChanged?.Invoke(this);
}
get
{
return _check;
}
}
/// <summary>
/// 左右半圆的半径
/// </summary>
[Browsable(true), Category(""), Description("左右半圆的半径"), DefaultValue(9)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int Radius
{
get
{
return _radius;
}
set
{
_radius = value;
CalcSize();
Refresh();
}
}
/// <summary>
/// 左右半圆的圆心距离
/// </summary>
[Browsable(true), Category(""), Description("左右半圆的圆心距离"), DefaultValue(20)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int CircleSpace
{
set
{
_between = value;
CalcSize();
Refresh();
}
get
{
return _between;
}
}
/// <summary>
/// 文本开关位置
/// </summary>
[Browsable(true), Category(""), Description("指定文本与开关的相对位置"), DefaultValue(TextSwitchRelation.SwitchBeforeText)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public TextSwitchRelation TextSwitchRelation
{
set
{
_relation = value;
CalcSize();
Refresh();
}
get
{
return _relation;
}
}
/// <summary>
/// Check属性更改时发生
/// </summary>
[Browsable(true), Category(""), Description("Check属性更改时发生")]
public event Event.ValueChanged CheckedChanged;
/// <summary>
/// 计算大小
/// </summary>
protected override void CalcSize()
{
int diameter = _radius * 2; //直径
RectangleF rect1 = new RectangleF(6, (Height - diameter) / 2f, diameter, diameter);
RectangleF rect2 = new RectangleF(6 + _between, (Height - diameter) / 2f, diameter, diameter);
SizeF sf = CreateGraphics().MeasureString(Text, Font);
_textRect = new RectangleF(rect2.X + rect2.Width + 6, (Height - sf.Height) / 2f, sf.Width, sf.Height);
_switchPath = new GraphicsPath();
_switchPath.AddArc(rect1, 90, 180);
_switchPath.AddLine(rect1.X + _radius, rect1.Y, rect2.X + _radius, rect2.Y);
_switchPath.AddArc(rect2, -90, 180);
_switchPath.AddLine(rect2.X + _radius, rect2.Y + diameter, rect1.X + _radius, rect1.Y + diameter);
if (Checked)
{
_switchPoint.X = rect2.X + _radius;
_switchPoint.Y = rect2.Y + _radius;
}
else
{
_switchPoint.X = rect1.X + _radius;
_switchPoint.Y = rect1.Y + _radius;
}
}
private void FlatCheck_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.Clear(Common.BACK_COLOR);
if (Checked)
{
e.Graphics.FillPath(Common.BLUE_BRUSH, _switchPath);
e.Graphics.DrawPath(Common.BLUE_PEN, _switchPath);
}
else if (Inside)
e.Graphics.DrawPath(Common.BLUE_PEN, _switchPath);
else
e.Graphics.DrawPath(Common.BORDER_PEN, _switchPath);
int r = _radius * 2 - 6;
e.Graphics.FillEllipse(Common.FORE_BRUSH, _switchPoint.X - r / 2f, _switchPoint.Y - r / 2f, r, r);
string s;
string[] arr = Text.Split(',');
if (arr.Length == 2)
{
if (Checked)
s = arr[1];
else
s = arr[0];
}
else
{
s = Text;
}
e.Graphics.DrawString(s, Font, new SolidBrush(Common.FORE_COLOR), _textRect);
}
private void FlatCheck_MouseDown(object sender, MouseEventArgs e)
{
Checked = !Checked;
}
}
}