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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#region Imports
 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region DungeonProgressBar
 
    public class DungeonProgressBar : Control
    {
 
        #region Enums 
 
        public enum Alignment
        {
            Left,
            Right,
            Center
        }
 
        #endregion
        #region Variables 
 
        private int _Minimum;
        private int _Maximum = 100;
        private int _Value = 0;
        private Alignment ALN;
        private bool _DrawHatch;
 
        private bool _ShowPercentage;
        private GraphicsPath GP1;
        private GraphicsPath GP2;
        private GraphicsPath GP3;
        private Rectangle R1;
        private Rectangle R2;
        private readonly LinearGradientBrush GB1;
        private LinearGradientBrush GB2;
        private int I1;
 
        #endregion
        #region Properties
 
        public Color BorderColor { get; set; } = Color.FromArgb(180, 180, 180);
 
        public Color BackColorA { get; set; } = Color.FromArgb(244, 241, 243);
 
        public Color BackColorB { get; set; } = Color.FromArgb(244, 241, 243);
 
        public Color ProgressColorA { get; set; } = Color.FromArgb(214, 89, 37);
 
        public Color ProgressColorB { get; set; } = Color.FromArgb(223, 118, 75);
 
        public Color ProgressHatchColor { get; set; } = Color.FromArgb(25, 255, 255, 255);
 
        public int Maximum
        {
            get => _Maximum;
            set
            {
                if (value < 1)
                {
                    value = 1;
                }
 
                if (value < _Value)
                {
                    _Value = value;
                }
 
                _Maximum = value;
                Invalidate();
            }
        }
 
        public int Minimum
        {
            get => _Minimum;
            set
            {
                _Minimum = value;
 
                if (value > _Maximum)
                {
                    _Maximum = value;
                }
 
                if (value > _Value)
                {
                    _Value = value;
                }
 
                Invalidate();
            }
        }
 
        public int Value
        {
            get => _Value;
            set
            {
                if (value > _Maximum)
                {
                    value = Maximum;
                }
 
                _Value = value;
                Invalidate();
            }
        }
 
        public Alignment ValueAlignment
        {
            get => ALN;
            set
            {
                ALN = value;
                Invalidate();
            }
        }
 
        public bool DrawHatch
        {
            get => _DrawHatch;
            set
            {
                _DrawHatch = value;
                Invalidate();
            }
        }
 
        public bool ShowPercentage
        {
            get => _ShowPercentage;
            set
            {
                _ShowPercentage = value;
                Invalidate();
            }
        }
 
        #endregion
        #region EventArgs
 
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Height = 20;
            MinimumSize = new(58, 20);
        }
 
        #endregion
 
        public DungeonProgressBar()
        {
            _Maximum = 100;
            _ShowPercentage = true;
            _DrawHatch = true;
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.UserPaint, true);
            BackColor = Color.Transparent;
            ForeColor = Color.DimGray;
            DoubleBuffered = true;
        }
 
        public void Increment(int value)
        {
            _Value += value;
            Invalidate();
        }
 
        public void Deincrement(int value)
        {
            _Value -= value;
            Invalidate();
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Bitmap B = new(Width, Height);
            Graphics G = Graphics.FromImage(B);
 
            G.Clear(Color.Transparent);
            G.SmoothingMode = SmoothingMode.HighQuality;
 
            GP1 = RoundRectangle.RoundRect(new Rectangle(0, 0, Width - 1, Height - 1), 4);
            GP2 = RoundRectangle.RoundRect(new Rectangle(1, 1, Width - 3, Height - 3), 4);
 
            R1 = new(0, 2, Width - 1, Height - 1);
            //GB1 = new(R1, Color.FromArgb(255, 255, 255), Color.FromArgb(230, 230, 230), 90f);
 
            // Draw inside background
            G.FillRectangle(new SolidBrush(BackColorA), R1);
            G.SetClip(GP1);
            G.FillPath(new SolidBrush(BackColorB), RoundRectangle.RoundRect(new Rectangle(1, 1, Width - 3, (Height / 2) - 2), 4));
 
 
            I1 = (int)Math.Round((_Value - _Minimum) / (double)(_Maximum - _Minimum) * (Width - 3));
            if (I1 > 1)
            {
                GP3 = RoundRectangle.RoundRect(new Rectangle(1, 1, I1, Height - 3), 4);
 
                R2 = new(1, 1, I1, Height - 3);
                GB2 = new(R2, ProgressColorA, ProgressColorB, 90f);
 
                // Fill the value with its gradient
                G.FillPath(GB2, GP3);
 
                // Draw diagonal lines
                if (_DrawHatch == true)
                {
                    for (int i = 0; i <= (Width - 1) * _Maximum / _Value; i += 20)
                    {
                        G.DrawLine(new(new SolidBrush(ProgressHatchColor), 10.0F), new Point(Convert.ToInt32(i), 0), new Point(i - 10, Height));
                    }
                }
 
                G.SetClip(GP3);
                G.SmoothingMode = SmoothingMode.None;
                G.SmoothingMode = SmoothingMode.AntiAlias;
                G.ResetClip();
            }
 
            // Draw value as a string
            string DrawString = Convert.ToString(Convert.ToInt32(Value)) + "%";
            /*
                int textX = (int)(Width - G.MeasureString(DrawString, Font).Width - 1);
                int textY = (int)((Height / 2) - (Convert.ToInt32(G.MeasureString(DrawString, Font).Height / 2) - 2));
            */
 
            if (_ShowPercentage == true)
            {
                switch (ValueAlignment)
                {
                    case Alignment.Left:
                        G.DrawString(DrawString, new Font("Segoe UI", 8), new SolidBrush(ForeColor), new Rectangle(0, 0, Width, Height + 2), new StringFormat
                        {
                            Alignment = StringAlignment.Near,
                            LineAlignment = StringAlignment.Center
                        });
                        break;
                    case Alignment.Right:
                        G.DrawString(DrawString, new Font("Segoe UI", 8), new SolidBrush(ForeColor), new Rectangle(0, 0, Width, Height + 2), new StringFormat
                        {
                            Alignment = StringAlignment.Far,
                            LineAlignment = StringAlignment.Center
                        });
                        break;
                    case Alignment.Center:
                        G.DrawString(DrawString, new Font("Segoe UI", 8), new SolidBrush(ForeColor), new Rectangle(0, 0, Width, Height + 2), new StringFormat
                        {
                            Alignment = StringAlignment.Center,
                            LineAlignment = StringAlignment.Center
                        });
                        break;
                }
            }
 
            // Draw border
            G.DrawPath(new(BorderColor), GP2);
 
            e.Graphics.DrawImage((Image)B.Clone(), 0, 0);
            G.Dispose();
            B.Dispose();
        }
    }
 
    #endregion
}