FlatRadio.cs
4.8 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
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
{
public partial class FlatRadio : FlatBase
{
private bool _check;
private RectangleF _switchRect = new RectangleF();
private PointF _switchPoint = new PointF();
private RectangleF _textRect = new RectangleF();
private const int radius = 9; //半径
private const int diameter = radius * 2; //直径
public FlatRadio()
{
InitializeComponent();
}
[Browsable(true), Category(""), Description("显示的文本"), DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get => base.Text;
set
{
base.Text = value;
CalcSize();
Refresh();
}
}
[Browsable(true), Category(""), Description("显示的文本的字体")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Font Font
{
set
{
base.Font = value;
CalcSize();
Refresh();
}
get
{
return base.Font;
}
}
[Browsable(true), Category(""), Description("单选按钮是否已选中"), DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool Checked
{
set
{
_check = value;
CalcSize();
Refresh();
if (_check) SetGroup();
}
get
{
return _check;
}
}
[Browsable(true), Category(""), Description("相同的内容为一组,同一组的单选按钮只能选中一个。"), DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string Group { set; get; }
protected override void CalcSize()
{
_switchRect = new RectangleF(6, (Height - diameter) / 2f, diameter, diameter);
SizeF sf = CreateGraphics().MeasureString(Text, Font);
_textRect = new RectangleF(_switchRect.X + _switchRect.Width + 6, (Height - sf.Height) / 2f, sf.Width, sf.Height);
if (Checked)
{
_switchPoint.X = _switchRect.X + radius;
_switchPoint.Y = _switchRect.Y + radius;
}
}
private void SetGroup()
{
if (Parent == null) return;
foreach (System.Windows.Forms.Control ctl in Parent.Controls)
{
if (ctl is FlatRadio)
{
FlatRadio rdo = (FlatRadio)ctl;
if (rdo == this) continue;
if (rdo.Group == Group)
{
if (rdo.Checked)
{
rdo.Checked = false;
break;
}
}
}
}
}
private void FlatRadio_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 (Inside)
{
e.Graphics.DrawEllipse(Common.BLUE_PEN, _switchRect);
}
else
{
e.Graphics.DrawEllipse(Common.FORE_PEN, _switchRect);
}
if (Checked)
{
int r = diameter - 8;
e.Graphics.FillEllipse(Common.BLUE_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, Common.FORE_BRUSH, _textRect);
}
private void FlatRadio_MouseDown(object sender, MouseEventArgs e)
{
Checked = true;
CalcSize();
Refresh();
}
private void FlatRadio_MouseUp(object sender, MouseEventArgs e)
{
}
}
}