yangyin
2025-02-24 b3b65a002fe68b1383314098790f5a153b87765f
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#region Imports
 
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region ProgressIndicator
 
    public class ProgressIndicator : Control
    {
        #region Variables
 
        private readonly SolidBrush BaseColor = new(Color.DarkGray);
        private readonly SolidBrush AnimationColor = new(Color.DimGray);
 
        private readonly Timer AnimationSpeed = new();
        private PointF[] FloatPoint;
        private BufferedGraphics BuffGraphics;
        private int IndicatorIndex;
        private readonly BufferedGraphicsContext GraphicsContext = BufferedGraphicsManager.Current;
 
        #endregion
 
        #region Custom Properties
 
        public Color P_BaseColor
        {
            get => BaseColor.Color;
            set => BaseColor.Color = value;
        }
 
        public Color P_AnimationColor
        {
            get => AnimationColor.Color;
            set => AnimationColor.Color = value;
        }
 
        public int P_AnimationSpeed
        {
            get => AnimationSpeed.Interval;
            set => AnimationSpeed.Interval = value;
        }
 
        #endregion
 
        #region EventArgs
 
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            SetStandardSize();
            UpdateGraphics();
            SetPoints();
        }
 
        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);
            AnimationSpeed.Enabled = Enabled;
        }
 
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            AnimationSpeed.Tick += AnimationSpeed_Tick;
            AnimationSpeed.Start();
        }
 
        private void AnimationSpeed_Tick(object sender, EventArgs e)
        {
            if (IndicatorIndex.Equals(0))
            {
                IndicatorIndex = FloatPoint.Length - 1;
            }
            else
            {
                IndicatorIndex -= 1;
            }
 
            Invalidate(false);
        }
 
        #endregion
 
        public ProgressIndicator()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
 
            Size = new(80, 80);
            Text = string.Empty;
            MinimumSize = new(50, 50);
            SetPoints();
            AnimationSpeed.Interval = 100;
        }
 
        private void SetStandardSize()
        {
            int _Size = Math.Max(Width, Height);
            Size = new(_Size, _Size);
        }
 
        private void SetPoints()
        {
            Stack<PointF> stack = new();
            PointF startingFloatPoint = new(Width / 2f, Height / 2f);
            for (float i = 0f; i < 360f; i += 45f)
            {
                SetValue(startingFloatPoint, (int)Math.Round((double)((Width / 2.0) - 15.0)), (double)i);
                PointF endPoint = EndPoint;
                endPoint = new(endPoint.X - 7.5f, endPoint.Y - 7.5f);
                stack.Push(endPoint);
            }
            FloatPoint = stack.ToArray();
        }
 
        private void UpdateGraphics()
        {
            if (Width > 0 && Height > 0)
            {
                Size size2 = new(Width + 1, Height + 1);
                GraphicsContext.MaximumBuffer = size2;
                BuffGraphics = GraphicsContext.Allocate(CreateGraphics(), ClientRectangle);
                BuffGraphics.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            }
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            BuffGraphics.Graphics.Clear(BackColor);
            int num2 = FloatPoint.Length - 1;
            for (int i = 0; i <= num2; i++)
            {
                if (IndicatorIndex == i)
                {
                    BuffGraphics.Graphics.FillEllipse(AnimationColor, FloatPoint[i].X, FloatPoint[i].Y, 15f, 15f);
                }
                else
                {
                    BuffGraphics.Graphics.FillEllipse(BaseColor, FloatPoint[i].X, FloatPoint[i].Y, 15f, 15f);
                }
            }
            BuffGraphics.Render(e.Graphics);
        }
 
        private double Rise;
        private double Run;
        private PointF _StartingFloatPoint;
 
        private X AssignValues<X>(ref X Run, X Length)
        {
            Run = Length;
            return Length;
        }
 
        private void SetValue(PointF StartingFloatPoint, int Length, double Angle)
        {
            double CircleRadian = Math.PI * Angle / 180.0;
 
            _StartingFloatPoint = StartingFloatPoint;
            Rise = AssignValues(ref Run, Length);
            Rise = Math.Sin(CircleRadian) * Rise;
            Run = Math.Cos(CircleRadian) * Run;
        }
 
        private PointF EndPoint
        {
            get
            {
                float LocationX = Convert.ToSingle(_StartingFloatPoint.Y + Rise);
                float LocationY = Convert.ToSingle(_StartingFloatPoint.X + Run);
 
                return new PointF(LocationY, LocationX);
            }
        }
    }
 
    #endregion
}