Winform自定义按钮

 右键项目添加 用户控件

//添加用户控件窗口
public partial class ButtonCheck : UserControl
    {
        //是否选中
        bool isCheck = true;
        /// <summary>
        /// 枚举
        /// </summary>
        public enum CheckStyle
        {
            style1 = 0,
            style2 = 1,
            style3 = 2,
            style4 = 3,
            style5 = 4,
            style6 = 5
        };
        public ButtonCheck()
        {
            InitializeComponent();
            //设置Style支持透明背景色并且双缓冲
            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.BackColor = Color.Transparent;

            this.Cursor = Cursors.Hand;
            this.Size = new Size(70, 70);
        }
        /// <summary>
        /// 是否选中
        /// </summary>
        public bool Checked
        {
            set { isCheck = value; this.Invalidate(); }
            get { return isCheck; }
        }

        CheckStyle checkStyle = CheckStyle.style1;

        /// <summary>
        /// 样式
        /// </summary>
        public CheckStyle CheckStyleX
        {
            set { checkStyle = value; this.Invalidate(); }
            get { return checkStyle; }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Bitmap bitMapOn = null;
            Bitmap bitMapOff = null;
            

            //定义选中的状态的 按钮的背景图
            //需要现在 Resources里添加按钮图片
            if (checkStyle == CheckStyle.style1)
            {
                bitMapOn = Properties.Resources.on1;
                bitMapOff = Properties.Resources.off1;
            }
            else if (checkStyle == CheckStyle.style2)
            {
                bitMapOn = Properties.Resources.on2;
                bitMapOff = Properties.Resources.off2;
            }
            else if (checkStyle == CheckStyle.style3)
            {
                bitMapOn = Properties.Resources.on3;
                bitMapOff = Properties.Resources.off3;
            }

            Graphics g = e.Graphics;
            Rectangle rec = new Rectangle(0, 0, this.Size.Width, this.Size.Height);

            if (isCheck)
            {
                g.DrawImage(bitMapOn, rec);
            }
            else
            {
                g.DrawImage(bitMapOff, rec);
            }
        }
        //特别说明:ButtonCheck_Click这个事件要添加控制方法产生,而不是直接在这里写
        private void ButtonCheck_Click(object sender, EventArgs e)
        {
            isCheck = !isCheck;
            this.Invalidate();
        }
    }

特别说明:ButtonCheck_Click这个事件要添加控制方法产生

点击界面,事件,click事件,应用ButtonCheck_Click;

最后点击屏幕上方 重新生成解决方案,工具栏就会生成 ButtonCheck组件

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值