C# Winform自定义CheckBox控件

1、创建用户控件(Window窗体)
在控件库中添加用户控件(Window窗体),并命名为ColorCheckBox;
在属性/布局栏将Size设置为80,18。
2、修改ColorCheckBox代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UcLib
{
    [DefaultEvent("CheckedChanged")]
    public partial class ColorCheckBox : UserControl
    {
        public ColorCheckBox()
        {
            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);
            this.MouseDown += ColorCheckBox_MouseDown;
            CheckBoxColor = Color.Blue;
            Checked=false;
            Texts = "CheckBox";
        }

        private void ColorCheckBox_MouseDown(object sender, MouseEventArgs e)
        {
            Checked = !Checked;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            g.SmoothingMode=SmoothingMode.AntiAlias;
            SolidBrush solidBrush = new SolidBrush(this.checkBoxColor);
            g.FillRectangle(solidBrush, new Rectangle(0, 0, 12, 12));
            if(Checked)
            {
                Pen pen = new Pen(Color.White, 2.0f);
                g.DrawLine(pen, 2, 3, 6, 8);
                g.DrawLine(pen, 6, 8, 10, 2);
            }
            g.DrawString(this.Texts, new Font("Yahei", 10f), new SolidBrush(Color.Black), 18, 0);
        }
        #region Properties
        private Color checkBoxColor;
        [Description("选框颜色"), Category("自定义")]
        public Color CheckBoxColor
        {
            get { return checkBoxColor; }
            set { checkBoxColor = value; Refresh(); }
        }

        private bool mchecked;
        [Description("是否选中"), Category("自定义")]
        public bool Checked
        {
            get { return mchecked; }
            set { 
                mchecked = value;
                Refresh();
                if (CheckedChanged != null)
                {
                    CheckedChanged(this, null);
                }
            }
        }

        private string mText;
        [Description("文本内容"), Category("自定义")]
        public string Texts
        {
            get { return mText; }
            set { mText = value; Refresh(); }
        }


        #endregion
        #region Events
        [Description("选中改变事件"), Category("自定义")]
        public event EventHandler CheckedChanged;
        #endregion
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值