-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextProgressBar.cs
More file actions
76 lines (66 loc) · 1.96 KB
/
TextProgressBar.cs
File metadata and controls
76 lines (66 loc) · 1.96 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PolarBLE
{
public class TextProgressBar : Panel
{
private int _value = 0;
private int _maximum = 100;
public int Value
{
get => _value;
set
{
_value = Math.Min(_maximum, Math.Max(0, value));
this.Invalidate(); // Redraw
}
}
public int Maximum
{
get => _maximum;
set
{
_maximum = Math.Max(1, value);
this.Invalidate();
}
}
public TextProgressBar()
{
this.DoubleBuffered = true;
this.ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
float percent = (float)_value / _maximum;
int fillWidth = (int)(this.Width * percent);
// Draw progress bar
if (percent < .10)
{
using (Brush progressBrush = new SolidBrush(Color.Red))
{
e.Graphics.FillRectangle(progressBrush, 0, 0, fillWidth, this.Height);
}
}
else
{
using (Brush progressBrush = new SolidBrush(Color.Green))
{
e.Graphics.FillRectangle(progressBrush, 0, 0, fillWidth, this.Height);
}
}
// Draw text
string text = $"{_value}%";
SizeF textSize = e.Graphics.MeasureString(text, this.Font);
PointF textPos = new PointF(
(this.Width - textSize.Width) / 2,
(this.Height - textSize.Height) / 2
);
e.Graphics.DrawString(text, this.Font, Brushes.White, textPos);
}
}
}