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

目录
相关文章
|
6月前
|
关系型数据库 MySQL C#
C# winform 一个窗体需要调用自定义用户控件的控件名称
给用户控件ucQRCode增加属性: //二维码图片 private PictureBox _pictureBoxFSHLQrCode; public PictureBox PictureBoxFSHLQrCode {   get { return _pictureBoxFSHLQrCode; }   set { this.pictureBoxFSHLQrCode = value; } } 在Form1窗体直接调用即可: ucQRCode uQRCode=new ucQRCode(); ucQRCode.PictureBoxFSHLQrCode.属性= 要复制或传给用户控件上的控件的值
37 0
|
API C# Windows
Winform控件优化之无边框窗体及其拖动、调整大小和实现最大最小化关闭功能的自定义标题栏效果
Winform中实现无边框窗体只需要设置FormBorderStyle = FormBorderStyle.None,但是无边框下我们需要保留移动窗体、拖拽调整大小、自定义美观好看的标题栏等...
2639 0
Winform控件优化之无边框窗体及其拖动、调整大小和实现最大最小化关闭功能的自定义标题栏效果
|
程序员 开发工具 C语言
Qt编写自定义控件47-面板区域控件
一、前言 在很多web网页上,经常可以看到一个设备对应一个面板,或者某种同等类型的信息全部放在一个面板上,该面板还可以拖来拖去的,这个控件首次用在智能访客管理平台中,比如身份证信息一个面板,访客信息一个面板,被访人信息一个面板,这样相当于分类展示了,还提供了对应的标题栏有文字显示,这个控件的使用场景也是非常多,还有个子标题可以设置,拓展了报警闪烁的接口。
1196 0
|
开发工具 C语言
Qt编写自定义控件31-面板仪表盘控件
一、前言 在Qt自定义控件中,仪表盘控件是数量最多的,写仪表盘都写到快要吐血,可能是因为各种工业控制领域用的比较多吧,而且仪表盘又是比较生动直观的,这次看到百度的echart中有这个控件,所以也来模仿做了一个,其实掌握了一两个仪表盘的绘制方法以后,其他仪表盘的绘制都是如鱼得水,基本上变化很小。
1233 1
|
开发工具 C语言 索引
Qt编写自定义控件8-动画按钮组控件
一、前言 动画按钮组控件可以用来当做各种漂亮的导航条用,既可以设置成顶部底部+左侧右侧,还自带精美的滑动效果,还可以设置悬停滑动等各种颜色,原创作者雨田哥(QQ:3246214072),驰骋Qt控件界多年,雨田哥是我见过的在这块水平相当牛逼的,在我之上,想要什么效果都可以搞出来,大家也可以找他定制...
1296 0
窗体界面控件源码解析
这是我早期自己编写的一段代码,注释不是很详细,只是对必要部分做了简短说明。现在把所有的源码发布出来,对希望对这方面编程感兴趣的朋友提供可参考的资料
517 0
|
C#
自定义WPF 窗口样式
原文:自定义WPF 窗口样式 自定义 Window 在客户端程序中,经常需要用到自定义一个 Window ,大部分是为了好看吧。
1289 0
|
前端开发 C# 数据格式
WPF MVVM模式中,通过命令实现窗体拖动、跳转以及显隐控制
原文:WPF MVVM模式中,通过命令实现窗体拖动、跳转以及显隐控制 在WPF中使用MVVM模式,可以让我们的程序实现界面与功能的分离,方便开发,易于维护。但是,很多初学者会在使用MVVM的过程中遇到一个显而易见且无法回避的问题,那就是不同的窗体之间如何跳转?很多人在介绍MVVM的使用时,都没有明显提到该如何解决这一问题,不知是因为觉得太简单了还是其他原因。
2505 0
|
C# 开发工具 git
WPF实现选项卡效果(3)——自定义动态添加的AvalonDock选项卡内容
原文:WPF实现选项卡效果(3)——自定义动态添加的AvalonDock选项卡内容 简介   在前面一篇文章里面,我们实现了AvalonDock选项卡的动态添加,但是对于选项卡里面的内容,我们并没有实现任何有用的功能。
1292 0