C#自定義checkBox開關按鈕控件,設計漂亮美觀的UI按鈕

第一步:

先準備開關按鈕要使用到的背景圖片,一張是開啓的,一張是關閉的,如下圖:

     

一共有6種款式,大家也可以全部加進去

圖標素材下載地址: https://download.csdn.net/download/qq15577969/12344587

然後將這些圖片作爲資源文件添加到項目中,如下圖:

第二步、新建用戶控件,命名爲:ButtonCheck.cs

ButtonCheck.cs 代碼如下:

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

namespace OA系統採集器
{
    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(87, 27);
        }
        /// <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;

            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;
            }
            else if (checkStyle == CheckStyle.style4)
            {
                bitMapOn = Properties.Resources.on4;
                bitMapOff = Properties.Resources.off4;
            }
            else if (checkStyle == CheckStyle.style5)
            {
                bitMapOn = Properties.Resources.on5;
                bitMapOff = Properties.Resources.off5;
            }
            else if (checkStyle == CheckStyle.style6)
            {
                bitMapOn = Properties.Resources.on6;
                bitMapOff = Properties.Resources.off6;
            }

            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();
        }
    }
}

在自定義用戶控件右邊的方法屬性裏,找到click,雙擊進入,添加以下代碼:

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

第三步、調用自定義的開關按鈕控件

1.項目上右擊,點擊重新生成

2.回到UI設計界面,刷新“工具箱”,會發現多出一個可使用的ButtonCheck控件

3.直接將控件拖入窗口,就可以使用了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章