【Winfrom 滑动开关自定义控件无标题】

using NPOI.OpenXmlFormats.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HB_RLSPSCXS_NEW
{

    // 1. 自定义事件参数类(与SwitchButton同级)
    public class SwitchToggleEventArgs : EventArgs
    {
        public bool IsOn { get; }  // 存储开关状态
        public SwitchToggleEventArgs(bool isOn)
        {
            IsOn = isOn;
        }
    }


    // 2. 开关按钮控件类
    public class SwitchButton : Control
    {
        private bool _isOn = false;
        private string _textOn = "开";
        private string _textOff = "关";
        private float _fontSize = 35f;  // 添加字体大小属性,默认值35
        private string _fonttype = "微软雅黑"; //添加字体类型属性,默认值微软雅黑
        private float _textX = 35;


        // 事件声明(使用自定义参数类)
        public event EventHandler<SwitchToggleEventArgs> OnToggle;

        public bool IsOn
        {
            get => _isOn;
            set
            {
                if (_isOn != value)
                {
                    _isOn = value;
                    Invalidate();
                    // 触发事件时传递自定义参数
                    OnToggle?.Invoke(this, new SwitchToggleEventArgs(_isOn));
                }
            }
        }

        public string TextOn
        {
            get => _textOn;
            set
            {
                _textOn = value;
                Invalidate();
            }
        }

        public string TextOff
        {
            get => _textOff;
            set
            {
                _textOff = value;
                Invalidate();
            }
        }

        // 添加字体大小属性
        public float FontSize
        {
            get => _fontSize;
            set
            {
                if (_fontSize != value && value > 0)  // 确保字体大小为正数
                {
                    _fontSize = value;
                    Invalidate();  // 重绘控件
                }
            }
        }
        
        public float textX
        {
            get => _textX;
            set
            {
                if (_textX != value && value > 0)  // 确保字体大小为正数
                {
                    _textX = value;
                    Invalidate();  // 重绘控件
                }
            }
        }
        public string FontType
        {
            get => _fonttype;
            set
            {
                _fonttype = value;
                Invalidate();  // 重绘控件
            }
        }


        public SwitchButton()
        {
            Size = new Size(80, 40);
            DoubleBuffered = true;
            SetStyle(ControlStyles.UserMouse, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            var g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            // 绘制背景(圆角矩形)
            var bgRect = new Rectangle(0, 0, Width, Height);
            var bgColor = IsOn ? Color.FromArgb(76, 175, 80) : Color.FromArgb(200, 200, 200);
            int borderRadius = Height / 2;

            using (var bgBrush = new SolidBrush(bgColor))
            using (var path = new GraphicsPath())
            {
                path.AddArc(bgRect.X, bgRect.Y, borderRadius * 2, borderRadius * 2, 180, 90);
                path.AddArc(bgRect.Right - borderRadius * 2, bgRect.Y, borderRadius * 2, borderRadius * 2, 270, 90);
                path.AddArc(bgRect.Right - borderRadius * 2, bgRect.Bottom - borderRadius * 2, borderRadius * 2, borderRadius * 2, 0, 90);
                path.AddArc(bgRect.X, bgRect.Bottom - borderRadius * 2, borderRadius * 2, borderRadius * 2, 90, 90);
                path.CloseAllFigures();
                g.FillPath(bgBrush, path);
            }

            // 绘制滑块(圆形)
            int thumbSize = (int)(Height * 0.8);
            int thumbX = IsOn ? (Width - thumbSize - 4) : 4;
            int thumbY = (Height - thumbSize) / 2;
            var thumbRect = new Rectangle(thumbX, thumbY, thumbSize, thumbSize);

            using (var thumbBrush = new SolidBrush(Color.White))
            using (var pen = new Pen(Color.LightGray, 1))
            {
                g.FillEllipse(thumbBrush, thumbRect);
                g.DrawEllipse(pen, thumbRect);
            }

            // 绘制文字
            string displayText = IsOn ? TextOn : TextOff;
            using (var font = new Font(_fonttype, _fontSize, FontStyle.Bold))
            using (var textBrush = new SolidBrush(Color.White))
            {
                SizeF textSize = g.MeasureString(displayText, font);
                float textX;

                // 关键修改:开状态靠左,关状态靠右
                if (IsOn)
                {
                    // 开状态 - 左边留一些间距
                    textX = _textX;
                }
                else
                {
                    // 关状态 - 右边留一些间距
                    textX = Width - textSize.Width - _textX;
                }

                // 垂直居中保持不变
                float textY = (Height - textSize.Height) / 2;
                g.DrawString(displayText, font, textBrush, textX, textY);
            }
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);
            IsOn = !IsOn;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (Width < Height) Width = Height * 2;
            if (Height < 20) Height = 20;
        }
    }




}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值