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

发布时间 2023-06-26 15:10:13作者: hanzq_go

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

 通过双击实现切换操作

实现代码如下:

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