C# Winform自定义Switch控件

news/2024/7/8 1:59:23

1、创建Switch控件
在控件库中添加用户控件(Window窗体),控件名UcSwitch;
在属性/布局栏中的Size设置为70,30。
2、修改UcSwitch.cs

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

namespace UcLib
{
    [DefaultEvent("CheckedChanged")]
    public partial class UcSwitch : UserControl
    {
        public UcSwitch()
        {
            InitializeComponent();
            //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.MouseDown += UcSwitch_MouseDown;
            Texts=new string[2] { "开","关"};
        }
        [Description("选中改变事件"), Category("自定义")]
        public event EventHandler CheckedChanged;
        private Color m_trueColor = Color.FromArgb(34, 163, 169);

        [Description("选中时颜色"), Category("自定义")]
        public Color TrueColor
        {
            get { return m_trueColor; }
            set
            {
                m_trueColor = value;
                Refresh();
            }
        }

        private Color m_falseColor = Color.FromArgb(111, 122, 126);

        [Description("没有选中时颜色"), Category("自定义")]
        public Color FalseColor
        {
            get { return m_falseColor; }
            set
            {
                m_falseColor = value;
                Refresh();
            }
        }

        private bool m_checked;

        [Description("是否选中"), Category("自定义")]
        public bool Checked
        {
            get { return m_checked; }
            set
            {
                m_checked = value;
                Refresh();
                if (CheckedChanged != null)
                {
                    CheckedChanged(this, null);
                }
            }
        }

        private string[] m_texts;

        [Description("文本值,当选中或没有选中时显示,必须是长度为2的数组"), Category("自定义")]
        public string[] Texts
        {
            get { return m_texts; }
            set
            {
                m_texts = value;
                Refresh();
            }
        }

        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
                Refresh();
            }
        }

        void UcSwitch_MouseDown(object sender, MouseEventArgs e)
        {
            Checked = !Checked;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            //设置呈现质量
            g.SmoothingMode = SmoothingMode.AntiAlias;
                var fillColor = m_checked ? m_trueColor : m_falseColor;
                GraphicsPath path = new GraphicsPath();
                path.AddLine(new Point(this.Height / 2, 1), new Point(this.Width - this.Height / 2, 1));
                path.AddArc(new Rectangle(this.Width - this.Height - 1, 1, this.Height - 2, this.Height - 2), -90, 180);
                path.AddLine(new Point(this.Width - this.Height / 2, this.Height - 1), new Point(this.Height / 2, this.Height - 1));
                path.AddArc(new Rectangle(1, 1, this.Height - 2, this.Height - 2), 90, 180);
                g.FillPath(new SolidBrush(fillColor), path);

                string strText = string.Empty;
                if (m_texts != null && m_texts.Length == 2)
                {
                    if (m_checked)
                    {
                        strText = m_texts[0];
                    }
                    else
                    {
                        strText = m_texts[1];
                    }
                }

                if (m_checked)
                {
                    g.FillEllipse(Brushes.White, new Rectangle(this.Width - this.Height - 1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
                    if (string.IsNullOrEmpty(strText))
                    {
                        g.DrawEllipse(new Pen(Color.White, 2), new Rectangle((this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2));
                    }
                    else
                    {
                        System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
                        int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
                        g.DrawString(strText, Font, Brushes.White, new Point((this.Height - 2 - 4) / 2, intTextY));
                    }
                }
                else
                {
                    g.FillEllipse(Brushes.White, new Rectangle(1 + 2, 1 + 2, this.Height - 2 - 4, this.Height - 2 - 4));
                    if (string.IsNullOrEmpty(strText))
                    {
                        g.DrawEllipse(new Pen(Color.White, 2), new Rectangle(this.Width - 2 - (this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2, (this.Height - 2 - (this.Height - 2 - 4) / 2) / 2 + 1, (this.Height - 2 - 4) / 2, (this.Height - 2 - 4) / 2));
                    }
                    else
                    {
                        System.Drawing.SizeF sizeF = g.MeasureString(strText.Replace(" ", "A"), Font);
                        int intTextY = (this.Height - (int)sizeF.Height) / 2 + 2;
                        g.DrawString(strText, Font, Brushes.White, new Point(this.Width - 2 - (this.Height - 2 - 4) / 2 - ((this.Height - 2 - 4) / 2) / 2 - (int)sizeF.Width / 2, intTextY));
                    }
                }
            
        }
    }
}

http://lihuaxi.xjx100.cn/news/1764831.html

相关文章

Xilinx microblaze axi can 使用说明

本次主要描述在SDK上如何配置波特率和采样率这两个关键的信息。 XILINX官方提供的驱动包括循环方式和中断方式。配置波特率和采样率主要依靠XCan_SetBaudRatePrescaler和XCan_SetBitTiming这两个函数。主要参数: #define TEST_BRPR_BAUD_PRESCALAR //分频系…

如何使用Node编写开发小工具

在做有些项目的时候有时候会遇到要重复创建内容大概相同的文件,但是命名和文件夹存放不一样的业务。比如说组件参考文档框架的Storybook的编写,就是需要大量的拷贝相同的文件代码,其目录大概如下 |--| | src | |--Buttom | | |--Button…

聊聊芯片制造中的金属杂质

在半导体制造过程中,杂质控制至关重要。杂质可以影响半导体的电导率,导致性能降低或者失效。在这些杂质中,金属杂质是最主要的来源之一,这些金属杂质可以通过扩散或者电迁移的方式在芯片中迅速扩散,因此控制金属杂质的…

lightdb 普通用户拥有XMLTYPE类型的访问权限

文章目录 概述示例总结 概述 在信创移植的SQL语句中,有来源于Oracle数据库的SQL语句。 在Oracle中存在getClobVal函数,这个函数是Oracle中sys.XMLType的成员方法。 因此在LightDB23.3版本中实现了TYPE支持定义成员方法并且在新定义的XMLType类型中实现…

易点易动设备管理系统:提升设备巡检和维修效率,延长设备使用寿命的利器

在现代企业中,设备管理是一个至关重要的环节。然而,许多企业在设备巡检和维修方面面临挑战,如效率低下、信息不透明等问题。为了帮助企业提升设备巡检和维修效率,并延长设备的使用寿命,易点易动设备管理系统应运而生。…

Python+Selenium+Unittest 之selenium12--WebDriver操作方法2-鼠标操作1(ActionChains类简介)

在我们平时的使用过程中,会使用鼠标去进行很多操作,比如鼠标左键点击、双击、鼠标右键点击,鼠标指针悬浮、拖拽等操作。在selenium中,我们也可以去实现常用的这些鼠标操作,这时候就需要用到selenium中的ActionChains类…

windows cmake x86 x64 下载与安装

cmake 下载路径:cmake 下载选择: 界面下拉选取适合自己的版本 这里是windows x86 x64 (x86是32位系统;x64是64位系统) 安装: 点击安装。 此处选择添加环境变量 命令提示符 验证查看 cmake 桌面可以…

平面和射线交点

设平面 A x B y C z D 0 ( A 2 B 2 C 2 ≠ 0 ) AxByCz D0\left(A^2B^2C^2\neq 0\right) AxByCzD0(A2B2C20), 点 P ( P x , P y , P z ) P(P_x, P_y, P_z) P(Px​,Py​,Pz​) (1)求点 P P P到平面距离 (2)过点 P P P作直线,方向为 ( D x , D y , D z ) \lef…