tangxu
2024-12-24 91105b77c916d06dd30380e20594e29f85eae3da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Colors;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region HopeToggle
 
    public class HopeToggle : System.Windows.Forms.CheckBox
    {
        #region Variables
 
        private readonly Timer AnimationTimer = new() { Interval = 1 };
        private int PointAnimationNum = 4;
 
        #endregion
 
        #region Settings
        public Color BaseColor { get; set; } = Color.FromArgb(44, 55, 66);
        public Color BaseColorA { get; set; } = HopeColors.OneLevelBorder;
        public Color BaseColorB { get; set; } = Color.FromArgb(100, HopeColors.PrimaryColor);
        public Color HeadColorA { get; set; } = HopeColors.OneLevelBorder;
        public Color HeadColorB { get; set; } = Color.White;
        public Color HeadColorC { get; set; } = HopeColors.PrimaryColor;
        public Color HeadColorD { get; set; } = HopeColors.PrimaryColor;
        #endregion
 
        #region Events
 
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            AnimationTimer.Start();
        }
 
        protected override void OnResize(EventArgs e)
        {
            Height = 20; Width = 48;
            Invalidate();
        }
 
        #endregion
 
        protected override void OnPaint(PaintEventArgs pevent)
        {
            Graphics graphics = pevent.Graphics;
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            graphics.Clear(BaseColor);
 
            GraphicsPath roundRectangle = new();
            int radius = 9;
            roundRectangle.AddArc(11, 5, radius - 1, radius, 180, 90);
            roundRectangle.AddArc(Width - 21, 5, radius - 1, radius, -90, 90);
            roundRectangle.AddArc(Width - 21, Height - 14, radius - 1, radius, 0, 90);
            roundRectangle.AddArc(11, Height - 14, radius - 1, radius, 90, 90);
            roundRectangle.CloseAllFigures();
 
            graphics.FillPath(new SolidBrush(Checked ? BaseColorB : BaseColorA), roundRectangle);
 
            graphics.FillEllipse(new SolidBrush(Checked ? HeadColorC : HeadColorA), new RectangleF(PointAnimationNum, 1, 18, 18));
            graphics.FillEllipse(new SolidBrush(Checked ? HeadColorD : HeadColorB), new RectangleF(PointAnimationNum + 2, 3, 14, 14));
        }
 
        public HopeToggle()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
            DoubleBuffered = true;
            Height = 20; Width = 47;
            AnimationTimer.Tick += new EventHandler(AnimationTick);
            Cursor = Cursors.Hand;
        }
 
        private void AnimationTick(object sender, EventArgs e)
        {
            if (Checked)
            {
                if (PointAnimationNum < 24)
                {
                    PointAnimationNum += 2;
                    Invalidate();
                }
            }
            else if (PointAnimationNum > 4)
            {
                PointAnimationNum -= 2;
                Invalidate();
            }
        }
    }
 
    #endregion
}