Skip to content

Commit 1a7587f

Browse files
committed
Finish implementation of UPSVarGauge
Still needs testing.
1 parent 94ace7c commit 1a7587f

File tree

2 files changed

+241
-94
lines changed

2 files changed

+241
-94
lines changed

WinNUT_V2/WinNUT-Client/Controls/UPSVarGauge.Designer.vb

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WinNUT_V2/WinNUT-Client/Controls/UPSVarGauge.vb

Lines changed: 240 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -8,120 +8,267 @@
88
' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
99

1010
Imports System.ComponentModel
11+
Imports System.Drawing.Drawing2D
1112

12-
Namespace Controls
13-
Public Class UPSVarGauge
14-
Inherits AGauge
13+
Friend Class UPSVarGauge
14+
Inherits AGauge
1515

1616
#Region "Private Fields"
1717

18-
Private m_value1 As Single
19-
Private m_value2 As Single
20-
21-
Private m_Center = New Point(74, 70)
22-
Private m_MinValue As Single = 0
23-
Private m_MaxValue As Single = 100
24-
25-
Private m_BaseArcRadius = 45
26-
Private m_BaseArcStart = 135
27-
Private m_BaseArcSweep = 270
28-
Private m_BaseArcWidth = 5
29-
30-
Private m_ScaleLinesInterInnerRadius = 40
31-
Private m_ScaleLinesInterOuterRadius = 48
32-
Private m_ScaleLinesInterWidth = 1
33-
34-
Private m_ScaleLinesMinorTicks = 9
35-
Private m_ScaleLinesMinorInnerRadius = 42
36-
Private m_ScaleLinesMinorOuterRadius = 48
37-
Private m_ScaleLinesMinorWidth = 1
38-
39-
Private m_ScaleLinesMajorStepValue = 50.0F
40-
Private m_ScaleLinesMajorInnerRadius = 40
41-
Private m_ScaleLinesMajorOuterRadius = 48
42-
Private m_ScaleLinesMajorWidth = 2
43-
44-
Private m_ScaleNumbersRadius = 60
45-
Private m_ScaleNumbersFormat As String
46-
Private m_ScaleNumbersStartScaleLine As Integer
47-
Private m_ScaleNumbersStepScaleLines = 1
48-
Private m_ScaleNumbersRotation As Integer
49-
50-
Private m_NeedleType As NeedleType
51-
Private m_NeedleRadius = 32
52-
Private m_NeedleColor1 = AGaugeNeedleColor.Gray
53-
Private m_NeedleColor2 = Color.DimGray
54-
Private m_NeedleWidth = 2
55-
56-
Private m_gradientType = GradientType.RedGreen
57-
Private m_gradientOrientation = GradientOrientation.BottomToUp
58-
Private m_unitvalue1 = UnitValue.Volts
59-
Private m_unitvalue2 = UnitValue.None
18+
Private ReadOnly drawGaugeBackground = True
19+
20+
Private ReadOnly m_value1 As Single
21+
Private m_value2 As Single
22+
23+
Private ReadOnly m_Center = New Point(74, 70)
24+
Private ReadOnly m_MinValue As Single = 0
25+
Private ReadOnly m_MaxValue As Single = 100
26+
27+
Private ReadOnly m_BaseArcRadius = 45
28+
Private ReadOnly m_BaseArcStart = 135
29+
Private ReadOnly m_BaseArcSweep = 270
30+
Private ReadOnly m_BaseArcWidth = 5
31+
32+
Private ReadOnly m_ScaleLinesInterInnerRadius = 40
33+
Private ReadOnly m_ScaleLinesInterOuterRadius = 48
34+
Private ReadOnly m_ScaleLinesInterWidth = 1
35+
36+
Private ReadOnly m_ScaleLinesMinorTicks = 9
37+
Private ReadOnly m_ScaleLinesMinorInnerRadius = 42
38+
Private ReadOnly m_ScaleLinesMinorOuterRadius = 48
39+
Private ReadOnly m_ScaleLinesMinorWidth = 1
40+
41+
Private ReadOnly m_ScaleLinesMajorStepValue = 50.0F
42+
Private ReadOnly m_ScaleLinesMajorInnerRadius = 40
43+
Private ReadOnly m_ScaleLinesMajorOuterRadius = 48
44+
Private ReadOnly m_ScaleLinesMajorWidth = 2
45+
46+
Private ReadOnly m_ScaleNumbersRadius = 60
47+
Private ReadOnly m_ScaleNumbersFormat As String
48+
Private ReadOnly m_ScaleNumbersStartScaleLine As Integer
49+
Private ReadOnly m_ScaleNumbersStepScaleLines = 1
50+
Private ReadOnly m_ScaleNumbersRotation As Integer
51+
52+
Private ReadOnly m_NeedleType As NeedleType
53+
Private ReadOnly m_NeedleRadius = 32
54+
Private ReadOnly m_NeedleColor1 = AGaugeNeedleColor.Gray
55+
Private ReadOnly m_NeedleColor2 = Color.DimGray
56+
Private ReadOnly m_NeedleWidth = 2
57+
58+
Private m_gradientType = GradientTypeEnum.RedGreen
59+
Private m_gradientOrientation = GradientOrientationEnum.BottomToUp
60+
Private m_unitvalue1 = UnitValueEnum.Volts
61+
Private m_unitvalue2 = UnitValueEnum.None
6062

6163
#End Region
6264

6365
#Region "Properties"
6466

65-
' Hide the old Value property
66-
Private Overloads Property Value As Single
67-
Get
68-
Return Nothing
69-
End Get
70-
Set(value As Single)
67+
' Map Value1 onto Value
68+
<Browsable(True),
69+
Category("UPSVarGauge"),
70+
Description("First gauge value.")>
71+
Public Property Value1 As Single
72+
Get
73+
Return Value
74+
End Get
75+
Set(value As Single)
76+
MyBase.Value = value
77+
End Set
78+
End Property
79+
80+
<Browsable(True),
81+
Category("AGauge"),
82+
Description("Second Value To Display.")>
83+
Public Property Value2 As Single
84+
Get
85+
Return m_value2
86+
End Get
87+
Set(value As Single)
88+
If m_value2 <> value Then
89+
m_value2 = value
90+
OnValueChanged(Me, Nothing)
91+
Refresh()
92+
End If
93+
End Set
94+
End Property
7195

72-
End Set
73-
End Property
96+
<Browsable(True),
97+
Category("AGauge"),
98+
Description("UseColor For Arc Base Color.")>
99+
Public Property GradientType As GradientTypeEnum
100+
Get
101+
Return m_gradientType
102+
End Get
103+
Set(value As GradientTypeEnum)
104+
m_gradientType = value
105+
Refresh()
106+
End Set
107+
End Property
74108

75-
<Browsable(True),
76-
Category("UPSVarGauge"),
77-
Description("First gauge value.")>
78-
Public Property Value1 As Single
79-
Get
80-
Return m_value1
81-
End Get
82-
Set(value As Single)
83-
value = Math.Min(Math.Max(value, m_MinValue), m_MaxValue)
84-
If m_value1 <> value Then
109+
<Browsable(True),
110+
Category("AGauge"),
111+
Description("Orientation Of Gradient Colors.")>
112+
Public Property GradientOrientation As GradientOrientationEnum
113+
Get
114+
Return m_gradientOrientation
115+
End Get
116+
Set(value As GradientOrientationEnum)
85117

86-
End If
87-
End Set
118+
If m_gradientOrientation <> value Then
119+
m_gradientOrientation = value
120+
Refresh()
121+
End If
122+
End Set
123+
End Property
88124

89-
End Property
125+
<Browsable(True),
126+
Category("AGauge"),
127+
Description("Units For Value 1")>
128+
Public Property UnitValue1 As UnitValueEnum
129+
Get
130+
Return m_unitvalue1
131+
End Get
132+
Set(value As UnitValueEnum)
90133

134+
If m_unitvalue1 <> value Then
135+
m_unitvalue1 = value
136+
Refresh()
137+
End If
138+
End Set
139+
End Property
140+
141+
<Browsable(True),
142+
Category("AGauge"),
143+
Description("UseColor For Arc Base Color.")>
144+
Public Property UnitValue2 As UnitValueEnum
145+
Get
146+
Return m_unitvalue2
147+
End Get
148+
Set(value As UnitValueEnum)
149+
150+
If m_unitvalue2 <> value Then
151+
m_unitvalue2 = value
152+
Refresh()
153+
End If
154+
End Set
155+
End Property
91156

92157
#End Region
93158

94-
Public Enum GradientType
95-
None
96-
RedGreen
97-
End Enum
159+
Public Enum GradientTypeEnum
160+
None
161+
RedGreen
162+
End Enum
163+
164+
Public Enum GradientOrientationEnum
165+
UpToBottom
166+
BottomToUp
167+
RightToLeft
168+
LeftToRight
169+
End Enum
170+
171+
Public Enum UnitValueEnum
172+
None
173+
Hertz
174+
Percent
175+
Volts
176+
Watts
177+
End Enum
178+
179+
Public Sub New()
180+
MyBase.New()
181+
182+
Size = New Size(148, 130)
183+
Center = New Point(148, 130)
184+
End Sub
185+
186+
Overrides Sub RenderDefaultArc(graphics As Graphics, center As Point, centerFactor As Single)
187+
If m_BaseArcRadius > 0 Then
188+
Dim baseArcRadius As Integer = m_BaseArcRadius * centerFactor
189+
If m_gradientType <> GradientTypeEnum.None Then
190+
Dim GradientP1Brush = New Point(0, (center.X + baseArcRadius + m_BaseArcWidth + 2))
191+
Dim GradientP2Brush = New Point(0, (center.X - baseArcRadius - m_BaseArcWidth - 2))
192+
193+
Select Case m_gradientOrientation
194+
Case GradientOrientationEnum.UpToBottom
195+
GradientP1Brush = New Point(0, (center.X - baseArcRadius - m_BaseArcWidth - 2))
196+
GradientP2Brush = New Point(0, (center.X + baseArcRadius + m_BaseArcWidth + 2))
197+
Case GradientOrientationEnum.BottomToUp
198+
GradientP1Brush = New Point(0, (center.X + baseArcRadius + m_BaseArcWidth + 2))
199+
GradientP2Brush = New Point(0, (center.X - baseArcRadius - m_BaseArcWidth - 2))
200+
Case GradientOrientationEnum.RightToLeft
201+
GradientP1Brush = New Point((center.Y + baseArcRadius + m_BaseArcWidth + 2), 0)
202+
GradientP2Brush = New Point((center.Y - baseArcRadius - m_BaseArcWidth - 2), 0)
203+
Case GradientOrientationEnum.LeftToRight
204+
GradientP1Brush = New Point((center.Y - baseArcRadius - m_BaseArcWidth - 2), 0)
205+
GradientP2Brush = New Point((center.Y + baseArcRadius + m_BaseArcWidth + 2), 0)
206+
End Select
207+
208+
Dim myArc1Gradient = New LinearGradientBrush(GradientP1Brush, GradientP2Brush, Color.Red, Color.Green)
209+
Using pnArc = New Pen(myArc1Gradient, m_BaseArcWidth * centerFactor)
210+
graphics.DrawArc(pnArc, New Rectangle(center.X - baseArcRadius,
211+
center.Y - baseArcRadius,
212+
2 * baseArcRadius,
213+
2 * baseArcRadius),
214+
m_BaseArcStart, m_BaseArcSweep)
215+
End Using
216+
Else
217+
Using pnArc = New Pen(BaseArcColor, m_BaseArcWidth * centerFactor)
218+
graphics.DrawArc(pnArc, New Rectangle(center.X - baseArcRadius,
219+
center.Y - baseArcRadius,
220+
2 * baseArcRadius,
221+
2 * baseArcRadius),
222+
m_BaseArcStart, m_BaseArcSweep)
223+
End Using
224+
End If
225+
End If
226+
End Sub
98227

99-
Public Enum GradientOrientation
100-
UpToBottom
101-
BottomToUp
102-
RightToLeft
103-
LeftToRight
104-
End Enum
228+
''' <summary>
229+
''' Override PostRender and render the value of the gauge with unit.
230+
''' </summary>
231+
Overrides Sub PostRender(graphics As Graphics, center As Point, centerFactor As Single)
232+
Dim PenString = New Pen(Color.Black)
233+
Dim PenFontV1 = New Font("Microsoft Sans Serif", 8, FontStyle.Bold)
234+
Dim PenFontV2 = New Font("Microsoft Sans Serif", 7, FontStyle.Bold)
235+
Dim StringPen = New SolidBrush(Color.Black)
236+
Dim LineHeight = 15
237+
Dim StrPos = center
238+
StrPos.Y += 5
105239

106-
Public Enum UnitValue
107-
None
108-
Hertz
109-
Percent
110-
Volts
111-
Watts
112-
End Enum
240+
If UnitValue1 <> UnitValueEnum.None Then
241+
Dim StringToDraw = ApplyUnit(Value1, UnitValue1)
242+
Dim StringSize = TextRenderer.MeasureText(StringToDraw, PenFontV1)
243+
StrPos.Y += LineHeight
244+
graphics.DrawString(StringToDraw, PenFontV1, StringPen,
245+
New PointF((StrPos.X - (StringSize.Width / 2) + 5), StrPos.Y))
246+
End If
113247

114-
Public Sub New()
115-
MyBase.New()
248+
If UnitValue2 <> UnitValueEnum.None Then
249+
Dim StringToDraw = ApplyUnit(Value2, UnitValue2)
250+
Dim StringSize = TextRenderer.MeasureText(StringToDraw, PenFontV2)
251+
StrPos.Y += LineHeight
252+
graphics.DrawString(StringToDraw, PenFontV2, StringPen,
253+
New PointF((StrPos.X - (StringSize.Width / 2) + 7), StrPos.Y))
254+
End If
255+
End Sub
116256

117-
Size = New Size(148, 130)
118-
End Sub
257+
Private Function ApplyUnit(value As String, unit As UnitValueEnum)
258+
Dim returnStr = value
119259

120-
'Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
121-
' MyBase.OnPaint(e)
260+
Select Case unit
261+
Case UnitValueEnum.Hertz
262+
returnStr &= " Hz"
263+
Case UnitValueEnum.Percent
264+
returnStr &= " %"
265+
Case UnitValueEnum.Volts
266+
returnStr &= " V"
267+
Case UnitValueEnum.Watts
268+
returnStr &= " W"
269+
End Select
122270

123-
' 'Add your custom paint code here
124-
'End Sub
271+
Return returnStr
272+
End Function
125273

126-
End Class
127-
End Namespace
274+
End Class

0 commit comments

Comments
 (0)