C# Winform 自定义彩色CheckBox控件

CheckBox默认有一个黑色框,黑色对号,白色背景。 使用自定义控件,继承Checkbox类,重写OnPaint,可以改变这几个颜色,和对号的样式。

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
class ColoredCheckbox: System.Windows.Forms.CheckBox
{
public ColoredCheckbox()
{

}
protected override void OnPaint(PaintEventArgs pevent)
{
//base.OnPaint(pevent);

pevent.Graphics.Clear(Color.White);

using (SolidBrush brush = new SolidBrush(ForeColor))
pevent.Graphics.DrawString(Text, Font, brush, 27, 4);

Point pt = new Point(4, 4);
Rectangle rect = new Rectangle(pt, new Size(16, 16));


pevent.Graphics.FillRectangle(Brushes.White, rect);

if (Checked)
{
using (SolidBrush brush = new SolidBrush(Color.FromArgb(0, 163, 235)))
using (Font wing = new Font("Wingdings", 12f))
pevent.Graphics.DrawString("ü", wing, brush, 1, 2);
}

pevent.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.FromArgb(0, 163, 235))), rect);

Rectangle fRect = ClientRectangle;


}


}