WinForm 如何自定义开关控件

简介: WinForm 如何自定义开关控件

先上结果:

tt.png

由于WinForm中没有开关控件,需要自定义。下面就写我制作开关控件的步骤。

1、新建个WinForm程序,右键选择属性。

tt.png

2、点击资源,将资源的类型修改为图像,然后将Button的图片复制进去(直接从文件夹里复制就行了)

tt.png

3、新建用户控件,命名为OnOffButton。

tt.png

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

然后生成解决方案。就可以在工具箱里看到组件了。

tt.png

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

目录
相关文章
|
JavaScript Java Android开发
uniapp通过蓝牙传输数据 (安卓)
uniapp通过蓝牙传输数据 (安卓)
1247 1
|
机器学习/深度学习 文字识别 数据挖掘
Visionpro从小白到大佬,第一章了解工具名称和用途
Visionpro从小白到大佬,第一章了解工具名称和用途
4205 0
Visionpro从小白到大佬,第一章了解工具名称和用途
|
C# 图形学 Windows
Winform控件优化之背景透明那些事2:窗体背景透明、镂空穿透、SetStyle、GDI透明效果等
两行代码就能实现Form窗体的(背景)透明效果,它不是Opacity属性的整个窗体透明,`TransparencyKey`实现窗体的透明、窗体中间部分镂空效果...
5199 0
Winform控件优化之背景透明那些事2:窗体背景透明、镂空穿透、SetStyle、GDI透明效果等
|
8月前
|
人工智能 JavaScript 安全
零基础搞定OpenClaw(小龙虾)Windows安装与使用,附最新部署包
OpenClaw(小龙虾)是2026年热门开源AI智能体,本地运行、零代码、可视化,支持办公/开发自动化。隐私安全、5–10分钟一键部署,内置Git/Python/Node.js,适配Win10/11,支持微信/飞书联动与多模型切换。
4178 3
|
10月前
|
缓存 监控 数据可视化
基于C#的CAN总线BMS上位机开发方案
基于C#的CAN总线BMS上位机开发方案
754 1
|
移动开发 监控 网络协议
C#通过TCP/IP控制康奈视读码枪实现方案
C#通过TCP/IP控制康奈视读码枪实现方案
|
监控 安全 C#
使用C#如何监控选定文件夹中文件的变动情况?
使用C#如何监控选定文件夹中文件的变动情况?
623 19
winform .net6 和 framework 的图表控件,为啥项目中不存在chart控件,该如何解决?
本文讨论了在基于.NET 6和.NET Framework的WinForms项目中添加图表控件的不同方法。由于.NET 6的WinForms项目默认不包含Chart控件,可以通过NuGet包管理器安装如ScottPlot等图表插件。而对于基于.NET Framework的WinForms项目,Chart控件是默认存在的,也可以通过NuGet安装额外的图表插件,例如LiveCharts。文中提供了通过NuGet添加图表控件的步骤和截图说明。
winform .net6 和 framework 的图表控件,为啥项目中不存在chart控件,该如何解决?
|
开发框架 NoSQL 前端开发
在Winform项目和Web API的.NetCore项目中使用Serilog 来记录日志信息
在Winform项目和Web API的.NetCore项目中使用Serilog 来记录日志信息
|
存储 物联网 数据库
App Inventor 2 低功耗蓝牙 BlueToothLE 拓展中文文档(完整翻译加强版)
低功耗蓝牙,也称为蓝牙LE 或简称 BLE,是一种类似于经典蓝牙的新通信协议,不同之处在于它旨在消耗更少的功耗和成本,同时保持同等的功能。 因此,低功耗蓝牙是与耗电资源有限的物联网设备进行通信的首选。
1381 2