Newer
Older
laserPTZ_CS / BzToggleButton .cs
wangxitong on 11 Sep 4 KB first commit
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 laserPTZ
{
    public partial class BzToggleButton : CheckBox
    {
        private Color onBackColor = Color.MediumSlateBlue;
        private Color onToggleColor = Color.WhiteSmoke;
        private Color offBackColor = Color.Gray;
        private Color offToggleColor = Color.Gainsboro;

        private bool solidStyle = true;

        [Category("BZ Advance")]
        public Color OnBackColor
        {
            get
            {
                return onBackColor;
            }

            set
            {
                onBackColor = value;
                this.Invalidate();
            }
        }
        [Category("BZ Advance")]
        public Color OnToggleColor
        {
            get
            {
                return onToggleColor;
            }

            set
            {
                onToggleColor = value;
                this.Invalidate();
            }
        }
        [Category("BZ Advance")]
        public Color OffBackColor
        {
            get
            {
                return offBackColor;
            }

            set
            {
                offBackColor = value;
                this.Invalidate();
            }
        }
        [Category("BZ Advance")]
        public Color OffToggleColor
        {
            get
            {
                return offToggleColor;
            }

            set
            {
                offToggleColor = value;
                this.Invalidate();
            }
        }

        public override string Text
        {
            get
            {
                return base.Text;
            }

            set
            {

            }
        }

        [Category("BZ Advance")]
        public bool SolidStyle
        {
            get
            {
                return solidStyle;
            }

            set
            {
                solidStyle = value;
                this.Invalidate();
            }
        }

        public BzToggleButton()
        {
            this.MinimumSize = new Size(45, 28);
        }

        private GraphicsPath GetFigurePath()
        {
            int arcSize = this.Height - 1;
            Rectangle leftArc = new Rectangle(0, 0, arcSize, arcSize);
            Rectangle rightArc = new Rectangle(this.Width - arcSize - 2, 0, arcSize, arcSize);
            GraphicsPath path = new GraphicsPath();
            path.StartFigure();
            path.AddArc(leftArc, 90, 180);
            path.AddArc(rightArc, 270, 180);
            path.CloseAllFigures();
            return path;
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            int toggleSize = this.Height - 5;
            Graphics graphics = pevent.Graphics;
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.Clear(Color.FromArgb(41, 42, 73));
            if (this.Checked)
            {
                if (solidStyle)
                {
                    graphics.FillPath(new SolidBrush(onBackColor), GetFigurePath());
                }
                else
                {
                    graphics.DrawPath(new Pen(OnBackColor, 2), GetFigurePath());
                }
                graphics.FillEllipse(new SolidBrush(onToggleColor), new Rectangle(this.Width - this.Height + 1
                    , 2, toggleSize, toggleSize));
            }
            else
            {
                if (solidStyle)
                {
                    graphics.FillPath(new SolidBrush(offBackColor), GetFigurePath());
                }
                else
                {
                    graphics.DrawPath(new Pen(offBackColor, 2), GetFigurePath());
                }
                graphics.FillEllipse(new SolidBrush(offToggleColor), new Rectangle(2
                    , 2, toggleSize, toggleSize));
            }
        }
    }
}