C# Winform 自定义彩色CheckBox控件

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

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;

            
        }
        

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据