天天看点

C# WinForm制作唯美的自定义开关按钮控件

作者:快乐鲤渔
C# WinForm制作唯美的自定义开关按钮控件

首先新建用户控件窗体:

public partial class myButton : UserControl
    {
        public enum Check
        {
            st1 = 0,
            st2 = 1,
            st3 = 2,
            st4 = 3,
            st5 = 4,
            st6 = 5
        };           
public myButton()
        {
            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(80, 20);
        }
        bool isChe = false;

        /// <summary>
        /// 是否选中
        /// </summary>
        public bool Checked
        {
            set { isChe = value; this.Invalidate(); }
            get { return isChe; }
        }
        Check che = Check.st1;           

样式

/// 样式
        /// </summary>
        public Check CheckStyleX
        {
            set { che = value; this.Invalidate(); }
            get { return che; }
        }           
protected override void OnPaint(PaintEventArgs e)
        {
            Bitmap biOn = null;
            Bitmap biOff = null;

            if (che == Check.st1)
            {
                biOn = Properties.Resources.btncheckon1;
                biOff = Properties.Resources.btncheckoff1;

            }
            else if (che == Check.st2)
            {
                biOn = Properties.Resources.btncheckon2;
                biOff = Properties.Resources.btncheckoff2;
            }
            else if (che == Check.st3)
            {
                biOn = Properties.Resources.btncheckon3;
                biOff = Properties.Resources.btncheckoff3;
            }
            else if (che == Check.st4)
            {
                biOn = Properties.Resources.btncheckon4;
                biOff = Properties.Resources.btncheckoff4;
            }
            else if (che == Check.st5)
            {
                biOn = Properties.Resources.btncheckon5;
                biOff = Properties.Resources.btncheckoff5;
            }
            else if (che == Check.st6)
            {
                biOn = Properties.Resources.btncheckon6;
                biOff = Properties.Resources.btncheckoff6;
            }
            Graphics s = e.Graphics;
            Rectangle rec = new Rectangle(0, 0, this.Size.Width, this.Size.Height);

            if (isChe)
            {
                s.DrawImage(biOn, rec);
            }
            else
            {
                s.DrawImage(biOff, rec);
            }
        }           

添加用户按钮控件:

private void myButton_Click(object sender, EventArgs e)
        {
            isChe = !isChe;
            this.Invalidate();
        }           

#大湾区寻宝##头条家时光##风船计划3.0#

C# WinForm制作唯美的自定义开关按钮控件

继续阅读