// *********************************************************************** // Assembly : JinYuan.ControlLib // Created : 08-08-2019 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // Blog: https://www.cnblogs.com/bfyx // GitHub:https://github.com/kwwwvagaa/NetWinformControl // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git // // If you use this code, please keep this note. // *********************************************************************** using JinYuan.ControlLib.UCHelper; using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace JinYuan.ControlLib { /// /// Class UCCheckBox. /// Implements the /// /// [DefaultEvent("CheckedChangeEvent")] public partial class UCCheckBox : UserControl { /// /// 选中改变事件 /// [Description("选中改变事件"), Category("自定义")] public event EventHandler CheckedChangeEvent; /// /// 字体 /// /// The font. /// /// /// /// /// /// [Description("字体"), Category("自定义")] public override Font Font { get { return base.Font; } set { base.Font = value; label1.Font = value; } } private Color _boxColor = Color.FromArgb(64, 158, 255); [Description("颜色"), Category("自定义")] public Color BoxColor { get { return _boxColor; } set { if (_boxColor == value) return; _boxColor = value; panel1.Invalidate(); } } /// /// The fore color /// private Color _ForeColor = Color.FromArgb(62, 62, 62); /// /// 字体颜色 /// /// The color of the fore. /// /// /// [Description("字体颜色"), Category("自定义")] public new Color ForeColor { get { return _ForeColor; } set { base.ForeColor = value; label1.ForeColor = value; _ForeColor = value; } } /// /// The text /// private string _Text = "复选框"; /// /// 文本 /// /// The text value. [Description("文本"), Category("自定义")] public string TextValue { get { return _Text; } set { label1.Text = value; _Text = value; } } /// /// The checked /// private bool _checked = false; /// /// 是否选中 /// /// true if checked; otherwise, false. [Description("是否选中"), Category("自定义")] public bool Checked { get { return _checked; } set { if (_checked != value) { _checked = value; Invalidate(); if (CheckedChangeEvent != null) { CheckedChangeEvent(this, null); } } } } /// /// 获取或设置一个值,该值指示控件是否可以对用户交互作出响应。 /// /// true if enabled; otherwise, false. /// /// /// /// /// /// public new bool Enabled { get { return base.Enabled; } set { base.Enabled = value; Invalidate(); } } /// /// Initializes a new instance of the class. /// public UCCheckBox() { InitializeComponent(); 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); this.SetStyle(ControlStyles.UserPaint, true); } /// /// Handles the MouseDown event of the CheckBox control. /// /// The source of the event. /// The instance containing the event data. private void CheckBox_MouseDown(object sender, MouseEventArgs e) { Checked = !Checked; } private void panel1_Paint(object sender, PaintEventArgs e) { e.Graphics.Clear(panel1.BackColor); Rectangle rect = new Rectangle(new Point(2, (this.panel1.Height - 14) / 2), new Size(14, 14)); e.Graphics.SetGDIHigh(); if (this.Enabled) { if (_checked) { e.Graphics.DrawRectangle(new Pen(_boxColor, 2), rect); e.Graphics.FillRectangle(new SolidBrush(_boxColor), new Rectangle(rect.Left + 3, rect.Top + 3, 8, 8)); } else { e.Graphics.DrawRectangle(new Pen(_boxColor, 2), rect); } } else { if (_checked) { e.Graphics.DrawRectangle(new Pen(Color.FromArgb(189, 189, 189), 2), rect); e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(189, 189, 189)), new Rectangle(rect.Left + 3, rect.Top + 3, 8, 8)); } else { e.Graphics.DrawRectangle(new Pen(Color.FromArgb(189, 189, 189), 2), rect); } } } private void UCCheckBox_EnabledChanged(object sender, EventArgs e) { panel1.Invalidate(); } } }