首页 > 其他分享 >winform控件开发一之复合控件开发(6)切换按钮(Switch)

winform控件开发一之复合控件开发(6)切换按钮(Switch)

时间:2023-06-26 15:11:35浏览次数:41  
标签:控件 一之 truePath Switch borderWidth 90 new Rectangle CornerRadius

使用自定义控件,实现一个切换按钮,显示效果如下:

 通过双击实现切换操作

实现代码如下:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace 各种C_sharp功能测试
{
    public partial class Switch : Control
    {
        public Switch()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.DoubleClick += Switch_DoubleClick;

        }

        private void Switch_DoubleClick(object sender, System.EventArgs e)
        {
            if (isChecked == true)
            {
                isChecked = false;
            }
            else
            {
                isChecked = true;
            }
            Invalidate();
        }

        //开关属性
        private bool isChecked;

        public bool IsChecked
        {
            get { return isChecked; }
            set { isChecked = value; Invalidate(); }
        }

        //开状态颜色
        private Color trueColor = Color.Green;

        public Color TrueColor
        {
            get { return trueColor; }
            set { trueColor = value; Invalidate(); }
        }


        //关状态颜色
        private Color falseColor = Color.Gray;

        public Color FalseColor
        {
            get { return falseColor; }
            set { falseColor = value; Invalidate(); }
        }

        //圆角的半径
        private int cornerRadius = 5;

        public int CornerRadius
        {
            get { return cornerRadius; }
            set { cornerRadius = value; Invalidate(); }
        }
        //边框宽度
        private int borderWidth = 2;

        public int DorderWidth
        {
            get { return borderWidth; }
            set { borderWidth = value; Invalidate(); }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            //获取绘图对象
            Graphics g = pe.Graphics;
            //呈现质量设置为高质量
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//HighQuality和AntiAlias执行效果相同

            //先绘制整体轮廓
            GraphicsPath path = new GraphicsPath();
            Color fillColor = isChecked ? TrueColor : falseColor;

            path.AddArc(new Rectangle(0, 0, CornerRadius * 2, CornerRadius * 2), 180, 90);
            path.AddArc(new Rectangle(Width - 1 - CornerRadius * 2, 0, CornerRadius * 2, CornerRadius * 2), -90, 90);
            path.AddArc(new Rectangle(Width - 1 - CornerRadius * 2, Height - 1 - CornerRadius * 2, CornerRadius * 2, CornerRadius * 2), 0, 90);
            path.AddArc(new Rectangle(0, Height - 1 - CornerRadius * 2, CornerRadius * 2, CornerRadius * 2), 90, 90);
            path.CloseFigure();
            g.FillPath(new SolidBrush(fillColor), path);

            //再填充内部形状
            if (isChecked)
            {
                GraphicsPath truePath = new GraphicsPath();
                truePath.AddArc(new Rectangle(Width / 2, borderWidth, CornerRadius * 2 - borderWidth * 2, CornerRadius * 2 - borderWidth * 2), 180, 90);
                truePath.AddArc(new Rectangle(Width - 1 + borderWidth - CornerRadius * 2, borderWidth, CornerRadius * 2 - borderWidth * 2, CornerRadius * 2 - borderWidth * 2), -90, 90);
                truePath.AddArc(new Rectangle(Width - 1 + borderWidth - CornerRadius * 2, Height - 1 + borderWidth - CornerRadius * 2, CornerRadius * 2 - borderWidth * 2, CornerRadius * 2 - borderWidth * 2), 0, 90);
                truePath.AddArc(new Rectangle(Width / 2, Height - 1 + borderWidth - CornerRadius * 2, CornerRadius * 2 - borderWidth * 2, CornerRadius * 2 - borderWidth * 2), 90, 90);

                g.FillPath(new SolidBrush(Color.White), truePath);
                //中心点
                int x1 = 3 * Width / 4;
                int y1 = Height / 2;
                //半径
                int r = Height / 4;
                g.DrawEllipse(new Pen(trueColor, borderWidth), new Rectangle(x1 - r, y1 - r, 2 * r, 2 * r));
            }
            else
            {
                GraphicsPath truePath = new GraphicsPath();
                truePath.AddArc(new Rectangle(borderWidth, borderWidth, CornerRadius * 2 - borderWidth * 2, CornerRadius * 2 - borderWidth * 2), 180, 90);
                truePath.AddArc(new Rectangle(Width / 2 + borderWidth - CornerRadius * 2, borderWidth, CornerRadius * 2 - borderWidth * 2, CornerRadius * 2 - borderWidth * 2), -90, 90);
                truePath.AddArc(new Rectangle(Width / 2 + borderWidth - CornerRadius * 2, Height - 1 + borderWidth - CornerRadius * 2, CornerRadius * 2 - borderWidth * 2, CornerRadius * 2 - borderWidth * 2), 0, 90);
                truePath.AddArc(new Rectangle(borderWidth, Height - 1 + borderWidth - CornerRadius * 2, CornerRadius * 2 - borderWidth * 2, CornerRadius * 2 - borderWidth * 2), 90, 90);

                g.FillPath(new SolidBrush(Color.White), truePath);
                //中心点
                int x1 = Width / 4;
                int y1 = Height / 2;
                //半径
                int r = Height / 4;
                g.DrawEllipse(new Pen(falseColor, borderWidth), new Rectangle(x1 - r, y1 - r, 2 * r, 2 * r));
            }
        }
    }
}

 

标签:控件,一之,truePath,Switch,borderWidth,90,new,Rectangle,CornerRadius
From: https://www.cnblogs.com/hanzq/p/17505662.html

相关文章

  • ActiveX 控件在过去是非常流行的技术,但近年来已经逐渐被其他技术所取代。由于其局限性
    ActiveX控件是一种可重用的软件组件,它们基于微软的COM(ComponentObjectModel)技术,并被广泛应用于Windows平台上的应用程序开发。ActiveX控件可以包含图形用户界面元素、功能模块、数据处理等,并提供给其他应用程序使用。下面是关于ActiveX控件的一些常见信息:安装和注册:使用A......
  • 用pageOffice控件实现 office word文档 强制留痕编辑Word
    OA办公中,业务需要多人编辑word文档,需要强制留痕功能,用来查看文档编辑过程中的具体修改痕迹。怎么实现word文档的强制留痕呢?1实现方法通过pageOffice实现简单的在线打开编辑word时,WebOpen方法的第二个参数使用docRevisionOnly,第三个参数传用户名,以强制留痕模式在线打开编辑Wor......
  • [QML]从零开始QML开发(二)QML开发,浅谈控件、槽函数、锚等基本概念。QML和C++怎么交互?贯
    [QML]从零开始QML开发(二)QML开发,浅谈控件、槽函数、锚等基本概念。QML和C++怎么交互?贯彻落实MVC原则先看代码:importQtQuick2.12importQtQuick.Window2.12importQtQuick.Controls2.5Window{visible:truewidth:320height:480title:qsTr("HelloW......
  • 浅谈OpenCV的多对象匹配图像的实现,以及如何匹配透明控件,不规则图像
    浅谈OpenCV的多对象匹配透明图像的实现,以及如何匹配半透明控件引子OpenCV提供的templateMatch只负责将(相关性等)计算出来,并不会直接提供目标的对应坐标,一般来说我们直接遍历最高的相关度,就可以得到匹配度最高的坐标。但是这样一般只能得到一个坐标。在实际操作中,我们可能需要......
  • 字符转换(switch语句)
    #include<iostream>usingnamespacestd;intmain(intargc,char**argv){ charn; cout<<"请输入要转换的字符"; cin>>n;switch(n){ case'a':n=n-32;break;case'b':n=n-32;break; case'c':n=n......
  • chart控件
    Chart控件五大核心“图表属性”——它们均是“集合” 均是集合属性。其中中间的三个属性ChartAreas、Series、Legends是最为核心的三个,1、ChartAreas属性ChartAreas可以理解为是一个图表的绘图区,例如,你想在一幅图上呈现两个不同属性的内容,一个是用户流量,另一个则是......
  • ScrollView下面固定一个控件
    <?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:......
  • MONGODB 奇怪12问 switch over
    开头还是介绍一下群,如果感兴趣polardb,mongodb,mysql,postgresql ,redis等有问题,有需求都可以加群群内有各大数据库行业大咖,CTO,可以解决你的问题。最近我们公司的REDISDBA和MONGODBDBA在SWITCHOVER, 我提出了一些问题关于MONGODB的问题,来让两个DBA能更快的融合对方的......
  • winform控件开发一之复合控件开发(3)调节阀(regulate valve)
    使用自定义控件,实现一个调节阀,显示效果如下: 实现代码如下:usingSystem.Drawing;usingSystem.Drawing.Drawing2D;usingSystem.Windows.Forms;namespace各种C_sharp功能测试{publicpartialclassRegulateValve:Control{publicRegulateValve()......
  • winform控件开发一之复合控件开发(2)开关阀(shut-off valve)
    实现一个开关阀控件,使用自定义控件,显示效果如下所示:其中,上方为线圈状态,下方为阀体状态,如下所示: 当线圈给电时,线圈为绿色;当线圈失电时,线圈为红色;代码中用value1控制当阀门开到位时,阀体状态显示为绿色;当阀门关到位时,阀体状态显示为红色;代码中用value2控制 代码如下:usingS......