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.ComponentModel; namespace ColorCheckControls { public class CustomCheckBox: CheckBox { private Color _CheckColor; private void PaintHandler(object sender, PaintEventArgs args) { if (this.Checked) { Point pt = new Point(); if (this.CheckAlign == ContentAlignment.BottomCenter) { pt.X = (this.Width / 2) - 4; pt.Y = this.Height - 11; } if (this.CheckAlign == ContentAlignment.BottomLeft) { pt.X = 3; pt.Y = this.Height - 11; } if (this.CheckAlign == ContentAlignment.BottomRight) { pt.X = this.Width - 11; pt.Y = this.Height - 11; } if (this.CheckAlign == ContentAlignment.MiddleCenter) { pt.X = (this.Width / 2) - 4; pt.Y = (this.Height / 2) - 4; } if (this.CheckAlign == ContentAlignment.MiddleLeft) { pt.X = 3; pt.Y = (this.Height / 2) - 4; } if (this.CheckAlign == ContentAlignment.MiddleRight) { pt.X = this.Width - 11; pt.Y = (this.Height / 2) - 4; } if (this.CheckAlign == ContentAlignment.TopCenter) { pt.X = (this.Width / 2) - 4; pt.Y = 3; } if (this.CheckAlign == ContentAlignment.TopLeft) { pt.X = 3; pt.Y = 3; } if (this.CheckAlign == ContentAlignment.TopRight) { pt.X = this.Width - 11; pt.Y = 3; } DrawCheck(args.Graphics, this.CheckColor, pt); } } private void DrawCheck(Graphics g, Color c, Point pt) { Pen pen = new Pen(c); g.DrawLine(pen, pt.X, pt.Y + 2, pt.X + 2, pt.Y + 5); g.DrawLine(pen, pt.X, pt.Y + 3, pt.X + 2, pt.Y + 6); g.DrawLine(pen, pt.X, pt.Y + 4, pt.X + 2, pt.Y + 7); g.DrawLine(pen, pt.X + 3, pt.Y + 4, pt.X + 6, pt.Y - 1); g.DrawLine(pen, pt.X + 3, pt.Y + 5, pt.X + 6, pt.Y); g.DrawLine(pen, pt.X + 3, pt.Y + 6, pt.X + 6, pt.Y + 2); } public CustomCheckBox() { this._CheckColor = ForeColor; this.Paint += new PaintEventHandler(this.PaintHandler); } [Description("CheckBox复选框颜色")] public Color CheckColor { get { return _CheckColor; } set { _CheckColor = value; Invalidate(); } } } }
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.ComponentModel; namespace ColorCheckControls { public class CustomColorRadioButton: RadioButton { private Color _CheckColor; private void PaintHandler(object sender, PaintEventArgs args) { if (this.Checked) { Point pt = new Point(); if (CheckAlign == ContentAlignment.BottomCenter) { pt.X = (this.Width / 2) - 3; pt.Y = this.Height - 9; } if (CheckAlign == ContentAlignment.BottomLeft) { pt.X = 4; pt.Y = this.Height - 9; } if (CheckAlign == ContentAlignment.BottomRight) { pt.X = this.Width - 9; pt.Y = this.Height - 9; } if (CheckAlign == ContentAlignment.MiddleCenter) { pt.X = (this.Width / 2) - 3; pt.Y = (this.Height / 2) - 3; } if (CheckAlign == ContentAlignment.MiddleLeft) { pt.X = 4; pt.Y = (this.Height / 2) - 3; } if (CheckAlign == ContentAlignment.MiddleRight) { pt.X = this.Width - 9; pt.Y = (this.Height / 2) - 3; } if (CheckAlign == ContentAlignment.TopCenter) { pt.X = (this.Width / 2) - 3; pt.Y = 4; } if (CheckAlign == ContentAlignment.TopLeft) { pt.X = 4; pt.Y = 4; } if (CheckAlign == ContentAlignment.TopRight) { pt.X = this.Width - 9; pt.Y = 4; } DrawCheck(args.Graphics, this.CheckColor, pt); } } private void DrawCheck(Graphics g, Color c, Point pt) { /* Pen pen = new Pen(c); g.DrawLine(pen, pt.X, pt.Y + 1, pt.X + 4, pt.Y + 1); g.DrawLine(pen, pt.X-1, pt.Y + 2, pt.X + 5, pt.Y + 2); g.DrawLine(pen, pt.X, pt.Y + 3, pt.X + 4, pt.Y + 3); g.DrawLine(pen, pt.X + 1, pt.Y, pt.X + 1, pt.Y + 4); g.DrawLine(pen, pt.X + 2, pt.Y-1, pt.X + 2, pt.Y + 5); g.DrawLine(pen, pt.X + 3, pt.Y, pt.X + 3, pt.Y + 4); * */ Brush brush = new SolidBrush(c); g.FillEllipse(brush, pt.X-2, pt.Y-1, 7, 7); } public CustomColorRadioButton() { this._CheckColor = this.ForeColor; this.Paint += new PaintEventHandler(this.PaintHandler); } [Description("按钮颜色")] public Color CheckColor { get { return _CheckColor; } set { _CheckColor = value; Invalidate(); } } } }
点我下载