forked from RJCodeAdvance/Custom-RadioButton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RJRadioButton.cs
111 lines (101 loc) · 3.53 KB
/
RJRadioButton.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace CustomControls.RJControls
{
class RJRadioButton : RadioButton
{
//Fields
private Color checkedColor = Color.MediumSlateBlue;
private Color unCheckedColor = Color.Gray;
//Properties
public Color CheckedColor
{
get
{
return checkedColor;
}
set
{
checkedColor = value;
this.Invalidate();
}
}
public Color UnCheckedColor
{
get
{
return unCheckedColor;
}
set
{
unCheckedColor = value;
this.Invalidate();
}
}
//Constructor
public RJRadioButton()
{
this.MinimumSize = new Size(0, 21);
//Add a padding of 10 to the left to have a considerable distance between the text and the RadioButton.
this.Padding = new Padding(10,0,0,0);
}
//Overridden methods
protected override void OnPaint(PaintEventArgs pevent)
{
//Fields
Graphics graphics = pevent.Graphics;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
float rbBorderSize = 18F;
float rbCheckSize = 12F;
RectangleF rectRbBorder = new RectangleF()
{
X = 0.5F,
Y = (this.Height - rbBorderSize) / 2, //Center
Width = rbBorderSize,
Height = rbBorderSize
};
RectangleF rectRbCheck = new RectangleF()
{
X = rectRbBorder.X + ((rectRbBorder.Width - rbCheckSize) / 2), //Center
Y = (this.Height - rbCheckSize) / 2, //Center
Width = rbCheckSize,
Height = rbCheckSize
};
//Drawing
using (Pen penBorder = new Pen(checkedColor, 1.6F))
using (SolidBrush brushRbCheck = new SolidBrush(checkedColor))
using (SolidBrush brushText = new SolidBrush(this.ForeColor))
{
//Draw surface
graphics.Clear(this.BackColor);
//Draw Radio Button
if (this.Checked)
{
graphics.DrawEllipse(penBorder, rectRbBorder);//Circle border
graphics.FillEllipse(brushRbCheck, rectRbCheck); //Circle Radio Check
}
else
{
penBorder.Color = unCheckedColor;
graphics.DrawEllipse(penBorder, rectRbBorder); //Circle border
}
//Draw text
graphics.DrawString(this.Text, this.Font, brushText,
rbBorderSize + 8, (this.Height - TextRenderer.MeasureText(this.Text, this.Font).Height) / 2);//Y=Center
}
}
//X-> Obsolete code, this was replaced by the Padding property in the constructor
//(this.Padding = new Padding(10,0,0,0);)
//protected override void OnResize(EventArgs e)
//{
// base.OnResize(e);
// this.Width = TextRenderer.MeasureText(this.Text, this.Font).Width + 30;
//}
}
}