using AllBase.Helper; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Work.UserComponents { public partial class UcCheckBox : CheckBox { public UcCheckBox() { InitializeComponent(); this.CheckBoxCheckColor = GlobalConfig.MainColor; this.CheckBoxBorderColor = GlobalConfig.BorderColor; } public UcCheckBox(IContainer container) { container.Add(this); this.CheckBoxCheckColor = GlobalConfig.MainColor; this.CheckBoxBorderColor = GlobalConfig.BorderColor; InitializeComponent(); } private int checkBoxSize = 12; public Color CheckBoxColor = Color.White; public Color CheckBoxCheckColor = GlobalConfig.DefaultMainColor; public Color CheckBoxBorderColor = GlobalConfig.DefaultBorderColor; protected override void OnPaint(PaintEventArgs e) { //base.OnPaint(e); // 禁用系统绘制全部 base.OnPaintBackground(e); // 使用系统绘制背景 //if (e.ClipRectangle.X != 0 || e.ClipRectangle.Y != 0) // return; Graphics g = e.Graphics; //g.Clear(this.CheckBoxColor); //填充背景色 //g.FillRectangle(new SolidBrush(this.BackColor), e.ClipRectangle); g.FillRectangle(new SolidBrush(this.BackColor), this.ClientRectangle); Size size = TextRenderer.MeasureText(this.Text, Font); e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; // 或者 AntiAliasGridFit // 平滑字体 //e.Graphics.DrawString(this.Text, Font, new SolidBrush(this.ForeColor), // new PointF(e.ClipRectangle.X + checkBoxSize + 2 + Padding.Left, e.ClipRectangle.Y + (Height - size.Height) / 2)); e.Graphics.DrawString(this.Text, Font, new SolidBrush(this.ForeColor), new PointF(this.ClientRectangle.X + checkBoxSize + 2 + Padding.Left, this.ClientRectangle.Y + (Height - size.Height) / 2)); Rectangle rt = new Rectangle(); rt.X = 0; rt.Y = (Height - checkBoxSize) / 2; rt.Width = checkBoxSize; rt.Height = checkBoxSize; if (this.Enabled == false) DrawCheckBoxIcon(g, rt, this.Checked, Color.LightGray, Color.DarkGray, Color.Gray); else DrawCheckBoxIcon(g, rt, this.Checked, this.CheckBoxColor, this.CheckBoxCheckColor, this.CheckBoxBorderColor); } public static void DrawCheckBoxIcon(Graphics g, Rectangle rt, bool isChecked, Color checkBoxColor, Color checkBoxCheckColor, Color checkBoxBorderColor) { //绘制圆形(注释此句,以备后用) //Brush brush = new SolidBrush(this.Checked ? checkBoxCheckColor : checkBoxColor); //g.FillEllipse(brush, 0, (Height - checkBoxSize) / 2, checkBoxSize, checkBoxSize); // 绘制圆形按钮 Color color = isChecked ? checkBoxCheckColor : checkBoxColor; if (isChecked) { DrawRoundedRectangle(g, rt, 2, color, true); DrawRoundedRectangle(g, rt, 2, checkBoxBorderColor); // 绘制勾号 Pen p = new Pen(Color.FromArgb(220, 255, 255, 255), 1); //g.DrawLine(Pens.SkyBlue, rt.X + 3, rt.Y + 6, rt.X + 5, rt.Y + 8); //g.DrawLine(Pens.SkyBlue, rt.X + 5, rt.Y + 8, rt.X + 9, rt.Y + 4); g.DrawLine(p, rt.X + 3, rt.Y + 5, rt.X + 5, rt.Y + 7); g.DrawLine(p, rt.X + 5, rt.Y + 7, rt.X + 9, rt.Y + 3); g.DrawLine(p, rt.X + 3, rt.Y + 6, rt.X + 5, rt.Y + 8); g.DrawLine(p, rt.X + 5, rt.Y + 8, rt.X + 9, rt.Y + 4); g.DrawLine(p, rt.X + 3, rt.Y + 7, rt.X + 5, rt.Y + 9); g.DrawLine(p, rt.X + 5, rt.Y + 9, rt.X + 9, rt.Y + 5); } else { DrawRoundedRectangle(g, rt, 3, color, true); DrawRoundedRectangle(g, rt, 3, checkBoxBorderColor); } } public static void DrawRoundedRectangle(Graphics g, Rectangle rect, int radius, Color color, bool isFill = false) { using (GraphicsPath path = new GraphicsPath()) { path.StartFigure(); path.AddArc(rect.X, rect.Y, radius * 2, radius * 2, 180, 90); // Upper left corner path.AddArc(rect.Right - radius * 2, rect.Y, radius * 2, radius * 2, 270, 90); // Upper right corner path.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90); // Lower right corner path.AddArc(rect.X, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90); // Lower left corner path.CloseFigure(); //g.FillPath(Brushes.LightBlue, path); if (isFill) g.FillPath(new SolidBrush(color), path); // Fill the rounded rectangle else g.DrawPath(new Pen(color), path); } } /* protected override void OnDrawItem(DrawItemEventArgs e) { //int ItemsCount = Items.Count; //Rectangle rect = new Rectangle(0, 0, e.Bounds.Width, e.Bounds.Height * ItemsCount); e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; //// 绘制下拉框背景 //e.Graphics.FillRectangle(new SolidBrush(_BoxCenterColor), rect); //// 绘制下拉框边框 //e.Graphics.DrawRectangle(new Pen(new SolidBrush(_BorderColor)), rect); //// 绘制选中文字背景 //for (int i = 0; i < Items.Count; i++) //{ // if (SelectedItem != null) // { // if (SelectedItem.ToString() == Items[i].ToString()) // { // Rectangle rectSel = new Rectangle(1, e.Bounds.Height * (SelectedIndex), e.Bounds.Width - 2, e.Bounds.Height - 2); // e.Graphics.FillRectangle(new SolidBrush(Color.White), rectSel); // } // } // // 绘制文字 // e.Graphics.DrawString(Items[i].ToString(), Font, new SolidBrush(_FontColor), new PointF(0, i * (e.Bounds.Height == 0 ? 16 : e.Bounds.Height))); //} if (e.Index >= 0) { e.Graphics.FillRectangle(new SolidBrush(_BoxCenterColor), e.Bounds); if ((e.State & DrawItemState.Focus) == DrawItemState.Focus || (e.State & DrawItemState.Selected) == DrawItemState.Selected) { // 设置鼠标悬浮ComboBox的item的背景色 e.Graphics.FillRectangle(Brushes.White, e.Bounds); } else if (((e.State & DrawItemState.ComboBoxEdit) != DrawItemState.ComboBoxEdit)) { // 默认背景 e.Graphics.FillRectangle(new SolidBrush(e.BackColor), e.Bounds); } // 绘制文字 e.Graphics.DrawString(Items[e.Index].ToString(), Font, new SolidBrush(_FontColor), new PointF(e.Bounds.X, e.Bounds.Y)); } }*/ } }