yangyin
2025-03-27 b0de14c2670b9ff0079dacfb4b7457b438368f11
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
186
187
188
189
190
191
192
193
194
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Colors;
using DPumpHydr.WinFrmUI.RLT.Util;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region HopeProgressBar
 
    public class HopeProgressBar : Control
    {
        #region Variable
 
        public enum Style
        {
            ToolTip = 0,
            ValueInSide = 1,
            ValueOutSide = 2
        }
 
        #endregion
 
        #region Settings
 
        private Style _style = Style.ToolTip;
        public Style ProgressBarStyle
        {
            get => _style;
            set
            {
                _style = value;
                Invalidate();
            }
        }
 
        private bool _isError = false;
        public bool IsError
        {
            get => _isError;
            set
            {
                _isError = value;
                Invalidate();
            }
        }
 
        private int _valueNumber = 0;
        public int ValueNumber
        {
            get => _valueNumber;
            set
            {
                _valueNumber = value > 100 ? 100 : (value < 0 ? 0 : value);
                Invalidate();
            }
        }
 
        public Color DangerColor { get; set; } = HopeColors.Danger;
        public Color BaseColor { get; set; } = HopeColors.PrimaryColor;
        public Color FullBallonColor { get; set; } = HopeColors.Success;
        public Color FullBarColor { get; set; } = HopeColors.Success;
        public Color BarColor { get; set; } = HopeColors.OneLevelBorder;
        public string FullBallonText { get; set; } = "Ok!";
 
        #endregion
 
        #region Events
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            switch (_style)
            {
                case Style.ToolTip:
                    Height = 32;
                    break;
                case Style.ValueInSide:
                    Height = 14;
                    break;
                case Style.ValueOutSide:
                    Height = 14;
                    break;
            }
        }
 
        #endregion
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            graphics.Clear(BackColor);
 
            Color tempColor = _isError ? DangerColor : BaseColor;
 
            switch (_style)
            {
                case Style.ToolTip:
                    float x = (_valueNumber * (Width - 32) / 100) + 16f;
                    int y = 25;
                    graphics.FillPolygon(new SolidBrush(_valueNumber == 100 && !_isError ? FullBallonColor : tempColor), new PointF[]
                    {
                         new(x,y),new(x+5,y-5),new(x+16,y-5),new(x+16,y-25),new(x-16,y-25),new(x-16,y-5),new(x-5,y-5)
                    });
                    graphics.DrawString(_valueNumber != 100 ? _valueNumber.ToString() + "%" : FullBallonText, Font, new SolidBrush(ForeColor), new RectangleF(x - 16, y - 25, 32, 20), HopeStringAlign.Center);
 
                    graphics.FillRectangle(new SolidBrush(BarColor), new RectangleF(16, 25, Width - 32, Height - 25));
                    graphics.FillRectangle(new SolidBrush(_valueNumber == 100 && !_isError ? FullBarColor : tempColor), new RectangleF(16, 25, x - 16, Height - 25));
                    break;
 
                case Style.ValueInSide:
                    GraphicsPath path1 = new();
                    path1.AddArc(new RectangleF(0, 0, Height, Height), 90, 180);
                    path1.AddArc(new RectangleF(Width - Height, 0, Height, Height), -90, 180);
                    path1.CloseAllFigures();
                    graphics.FillPath(new SolidBrush(BarColor), path1);
 
                    if (_valueNumber == 0)
                    {
                        graphics.DrawString("0%", new Font("Segoe UI", 9f), new SolidBrush(ForeColor), new RectangleF(5, 0, 50, Height), HopeStringAlign.Left);
                    }
                    else
                    {
                        GraphicsPath path2 = new();
                        path2.AddArc(new RectangleF(0, 0, Height, Height), 90, 180);
                        path2.AddArc(new RectangleF(_valueNumber * (Width - Height) / 100, 0, Height, Height), -90, 180);
                        path2.CloseAllFigures();
                        graphics.FillPath(new SolidBrush(_valueNumber == 100 && !_isError ? FullBarColor : tempColor), path2);
 
                        graphics.DrawString(_valueNumber.ToString() + "%", new Font("Segoe UI", 9f), new SolidBrush(ForeColor), new RectangleF((_valueNumber * (Width - Height) / 100) - 33, 0, 45, Height), HopeStringAlign.Right);
                    }
                    break;
                case Style.ValueOutSide:
                    GraphicsPath path3 = new();
                    path3.AddArc(new RectangleF(0, 4, Height - 8, Height - 8), 90, 180);
                    path3.AddArc(new RectangleF(Width - 50, 4, Height - 8, Height - 8), -90, 180);
                    path3.CloseAllFigures();
                    graphics.FillPath(new SolidBrush(BarColor), path3);
 
                    if (_valueNumber != 0)
                    {
                        GraphicsPath path4 = new();
                        path4.AddArc(new RectangleF(0, 4, Height - 8, Height - 8), 90, 180);
                        path4.AddArc(new RectangleF(_valueNumber * (Width - 50) / 100, 4, Height - 8, Height - 8), -90, 180);
                        path4.CloseAllFigures();
                        graphics.FillPath(new SolidBrush(_valueNumber == 100 && !_isError ? FullBarColor : tempColor), path4);
                    }
 
                    if (_isError)
                    {
                        graphics.FillEllipse(new SolidBrush(DangerColor), new RectangleF(Width - 40, 0, Height, Height));
                        int a = Width - 40 + 4;
                        int b = Height - 4;
                        graphics.DrawLine(new(ForeColor), a, b - 6, a + 6, b);
                        graphics.DrawLine(new(ForeColor), a + 6, b - 6, a, b);
                    }
                    else
                    {
                        if (_valueNumber == 100)
                        {
                            graphics.FillEllipse(new SolidBrush(FullBarColor), new RectangleF(Width - 40, 0, Height, Height));
                            int a = Width - 40 + 4;
                            int b = Height - 4;
                            graphics.DrawLine(new(ForeColor), a, b - 3, a + 3, b);
                            graphics.DrawLine(new(ForeColor), a + 3, b, a + 6, b - 6);
                        }
                        else
                        {
                            graphics.DrawString(_valueNumber.ToString() + "%", new Font("Segoe UI", 10f), new SolidBrush(ForeColor), new RectangleF(Width - 40, 0, 50, Height), HopeStringAlign.Left);
                        }
                    }
                    break;
            }
        }
 
        public HopeProgressBar()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
            DoubleBuffered = true;
            Font = new("Segoe UI", 10);
            ForeColor = HopeColors.FourLevelBorder;
        }
    }
 
    #endregion
}