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中就可以使用。

目录
相关文章
Winform中Textbox、NumericUpDown等修改高度,禁止输入数字或内容的实现
Winform中的Textbox、NumericUpDown控件通常在单行的情况下,无法直接通过`Height`属性修改高度,但很多时候我们需要调整其高度,使其显示的更加合理,主要介绍三种方法...
3812 0
|
C# 图形学 Windows
Winform控件优化之背景透明那些事2:窗体背景透明、镂空穿透、SetStyle、GDI透明效果等
两行代码就能实现Form窗体的(背景)透明效果,它不是Opacity属性的整个窗体透明,`TransparencyKey`实现窗体的透明、窗体中间部分镂空效果...
4743 0
Winform控件优化之背景透明那些事2:窗体背景透明、镂空穿透、SetStyle、GDI透明效果等
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控件,该如何解决?
|
Java API Maven
【异常】Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlType
【异常】Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlType
653 0
|
设计模式
实践中面向对象的优缺点
【7月更文挑战第1天】本文介绍对象命名方案影响代码可读性。优点包括模块化、可重用性、可扩展性和易维护性。缺点包括类和对象识别的困难,学习曲线及可能的重用挑战。引用:《面向对象开发的陷阱》- 布鲁斯.韦伯斯特。
489 4
实践中面向对象的优缺点
|
存储 编解码 虚拟化
【2025最新】VMware Workstation Pro 虚拟机配置与安装VMware Tools 感受它的强大~
VMware Tools 是 VMware 虚拟机中自带的一种增强工具,能够显著提升虚拟机的性能和用户体验。它提供了优化的显卡驱动程序、文件共享与拖放功能、时间同步以及跨虚拟机和宿主机的复制粘贴功能。安装 VMware Tools 可以让虚拟机在 VMware 平台上运行得更加高效,并且对于老旧系统(如 Win98、Win2000、WinXP)也能提供必要的驱动支持。每个虚拟机都需要独立安装与其操作系统版本相匹配的 VMware Tools,以确保最佳兼容性和性能表现。
1590 6
【2025最新】VMware Workstation Pro 虚拟机配置与安装VMware Tools 感受它的强大~
|
人工智能 自然语言处理 关系型数据库
DMS+AnalyticDB助力钉钉AI助理,轻松玩转智能问数
DMS+AnalyticDB助力钉钉AI助理,轻松玩转智能问数
550 3
|
算法 API C#
Winform控件优化之圆角按钮【各种实现中的推荐做法】(下)
最终优化实现ButtonPro按钮(继承自Button),既提供Button原生功能,又提供扩展功能,除了圆角以外,还实现了圆形、圆角矩形的脚尖效果、边框大小和颜色、背景渐变颜色...
3844 0
Winform控件优化之圆角按钮【各种实现中的推荐做法】(下)
|
设计模式 程序员 C#
C# 使用 WinForm MDI 模式管理多个子窗体程序的详细步骤
WinForm MDI 模式就像是有超能力一般,让多个子窗体井然有序地排列在一个主窗体之下,既美观又实用。不过,也要小心管理好子窗体们的生命周期哦,否则一不小心就会出现一些意想不到的小bug
1533 0
|
监控 安全 C#
使用C#如何监控选定文件夹中文件的变动情况?
使用C#如何监控选定文件夹中文件的变动情况?
434 19