天天看點

WinForm 如何自定義開關控件

先上結果:

WinForm 如何自定義開關控件

由于WinForm中沒有開關控件,需要自定義。下面就寫我制作開關控件的步驟。

1、建立個WinForm程式,右鍵選擇屬性。

WinForm 如何自定義開關控件
2、點選資源,将資源的類型修改為圖像,然後将Button的圖檔複制進去(直接從檔案夾裡複制就行了)
WinForm 如何自定義開關控件
3、建立使用者控件,命名為OnOffButton。
WinForm 如何自定義開關控件
在OnOffButton.cs中編寫代碼。

using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace WindowsFormsApp1
{
    public enum CheckStyle
    {
        style1 = 0,
        style2 = 1,
        style3 = 2,
        style4 = 3,
        style5 = 4,
        style6 = 5
    };
    public partial class OnOffButton : UserControl
    {
        public OnOffButton()
        {
            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);
        }
        bool isCheck = false;
 
        /// <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 = global::WindowsFormsApp1.Properties.Resources.btncheckon1;
                bitMapOff = global::WindowsFormsApp1.Properties.Resources.btncheckoff1;
            }
            else if (checkStyle == CheckStyle.style2)
            {
                bitMapOn = global::WindowsFormsApp1.Properties.Resources.btncheckon2;
                bitMapOff = global::WindowsFormsApp1.Properties.Resources.btncheckoff2;
            }
            else if (checkStyle == CheckStyle.style3)
            {
                bitMapOn = global::WindowsFormsApp1.Properties.Resources.btncheckon3;
                bitMapOff = global::WindowsFormsApp1.Properties.Resources.btncheckoff3;
            }
            else if (checkStyle == CheckStyle.style4)
            {
                bitMapOn = global::WindowsFormsApp1.Properties.Resources.btncheckon4;
                bitMapOff = global::WindowsFormsApp1.Properties.Resources.btncheckoff4;
            }
            else if (checkStyle == CheckStyle.style5)
            {
                bitMapOn = global::WindowsFormsApp1.Properties.Resources.btncheckon5;
                bitMapOff = global::WindowsFormsApp1.Properties.Resources.btncheckoff5;
            }
            else if (checkStyle == CheckStyle.style6)
            {
                bitMapOn = global::WindowsFormsApp1.Properties.Resources.btncheckon6;
                bitMapOff = global::WindowsFormsApp1.Properties.Resources.btncheckoff6;
            }
 
            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);
            }
        }
 
        private void OnOffButton_Click(object sender, EventArgs e)
        {
            isCheck = !isCheck;
            this.Invalidate();
        }
    }
}
       

然後編寫OnOffButton.Designer.cs代碼。主要修改元件設計器生成的代碼

#region 元件設計器生成的代碼
 
        /// <summary> 
        /// 設計器支援所需的方法 - 不要修改
        /// 使用代碼編輯器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();        
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "myButtonCheck";
            this.Click += new System.EventHandler(this.OnOffButton_Click);
            this.ResumeLayout(false);
        }      

然後生成解決方案。就可以在工具箱裡看到元件了。

WinForm 如何自定義開關控件

将元件拖入Form中就可以使用。

繼續閱讀