Files
1258/JinYuan.ControlLib/CheckBoxExt.cs
T
2026-08-04 18:36:40 +08:00

133 lines
4.2 KiB
C#

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace JinYuan.ControlLib
{
public partial class CheckBoxExt : CheckBox
{
public CheckBoxExt()
{
InitializeComponent();
_format.LineAlignment = StringAlignment.Center;
_format.Alignment = StringAlignment.Center;
//设置控件样式,去除缓冲
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}
private StringFormat _format = new StringFormat();
private int defultCheckButtonWidth = 20;
[Browsable(true)]//是否可见
[Category("自定义属性")] //类别
[Description("设置或显示标题名称")]
public int DefultCheckButtonWidth
{
get { return defultCheckButtonWidth; }
set { defultCheckButtonWidth = value; }
}
private Color checkColor = Color.FromArgb(3, 25, 66);
[Browsable(true)]//是否可见
[Category("自定义属性")] //类别
[Description("设置或显示选中颜色")]
public Color CheckColor
{
get { return checkColor; }
set
{
checkColor = value;
this.Invalidate();
}
}
private Color checkBackColor = Color.White;
[Browsable(true)]//是否可见
[Category("自定义属性")] //类别
[Description("设置或显示选中框背景颜色")]
public Color CheckBackColor
{
get { return checkBackColor; }
set
{
checkBackColor = value;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
base.OnPaintBackground(e);
Graphics g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
Rectangle checkRec;
Rectangle textRec;
CalculatorRec(out checkRec, out textRec);
SolidBrush solidBrush = new SolidBrush(CheckBackColor);
g.FillRectangle(solidBrush, checkRec);
Pen pen = new Pen(Color.LightGray);
g.DrawRectangle(pen, checkRec);
if (this.CheckState == CheckState.Checked)
{
//画勾选
DrawCheckFlag(g, checkRec, checkColor);
}
//绘制文本
g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRec, this._format);
}
/// <summary>
/// 计算矩形
/// </summary>
/// <param name="checkRec"></param>
/// <param name="textRec"></param>
private void CalculatorRec(out Rectangle checkRec, out Rectangle textRec)
{
checkRec = new Rectangle(3, (this.Height - defultCheckButtonWidth) / 2, defultCheckButtonWidth,
defultCheckButtonWidth);
textRec = new Rectangle(checkRec.Right + 3, 0, Width - checkRec.Right - 6, this.Height);
}
/// <summary>
/// 画勾选
/// </summary>
/// <param name="g"></param>
/// <param name="rectangle"></param>
/// <param name="color"></param>
private void DrawCheckFlag(Graphics g, Rectangle rectangle, Color color)
{
PointF[] pointFs = new PointF[3];
pointFs[0] = new PointF(rectangle.X + rectangle.Width / 4.5f, rectangle.Y + rectangle.Height / 2.5f);
pointFs[1] = new PointF(rectangle.X + rectangle.Width / 2.5F, rectangle.Bottom - rectangle.Height / 3.0f);
pointFs[2] = new PointF(rectangle.Right - rectangle.Width / 4.0f, rectangle.Y + rectangle.Height / 4.5f);
Pen pen = new Pen(color, 2.0f);
g.DrawLines(pen, pointFs);
}
}
}