天天看點

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制作唯美的自定義開關按鈕控件

繼續閱讀