|
|
Title | Make a monitor that displays a value using a vertical bar gauge that seems to float above other applications in VB .NET |
Description | This example shows how to make a vertical bar gauge that monitors a value in Visual Basic .NET. The bars in the gauge float above the desktop while showing whatever lies below them in between the bars. This example shows how to move the form to the screen's lower-right corner, how to give the form a convenient scale, how to update the display when a timer fires, and how to make parts of a form transparent. |
Keywords | gauge, bar, bar gauge, floating bar gauge, TransparencyKey, monitor, VB.NET |
Categories | Graphics, VB.NET |
|
|
When the program starts, it sets its TransparencyKey property to its background color. That makes parts of the form that have this color transparent. The program will later draw where it wants the form to be visible.
The form's Load event handler also sets the form's size and calls subroutine LowerRight.
|
|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
Me.TransparencyKey = Me.BackColor
Me.Size = New Size(16, 200)
LowerRight()
End Sub
|
|
Subroutine LowerRight moves the form to the screen's lower right corner. It uses the SystemInformation object's WorkingArea property to see where the work area is and moves the form.
|
|
' Move the form to the lower right corner.
Private Sub LowerRight()
Const GAP As Integer = 4
Dim working_area As Rectangle = _
SystemInformation.WorkingArea
Dim x As Integer = _
working_area.Left + working_area.Width - _
Me.Width - GAP
Dim y As Integer = _
working_area.Top + working_area.Height - _
Me.Height - GAP
Me.Location = New Point(x, y)
End Sub
|
|
When the tmrChangeStats timer fires its Tick event, the program changes the value of the m_Value variable and then calls the form's Refresh method to make it redraw itself.
|
|
Private m_Value As Double = 50
Private Sub tmrChangeStats_Tick(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
tmrChangeStats.Tick
Const DTHETA As Single = PI / 20
Static theta As Single
m_Value = _
Sin(theta) * 25 + _
Cos(2 * theta) * 15 + _
50
theta = theta + DTHETA
Me.Refresh()
End Sub
|
|
The Paint event handler first transforms its Graphics object so the point (0, 0) is in the form's lower left corner and the point (10, 100) is in the lower right. You can change the values 10 and 100 to make things convenient for your program.
Next the code loops over rectangles that it might want to fill. If a rectangle's Y value is less than or equal to the program's current value m_Value, then the program fills in that rectangle in blue. If the rectangle's Y value is greater than m_Value, then the program fills it with powder blue. In either case, the rectangle does not have the form's background color (red) so it is visible.
|
|
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs) Handles _
MyBase.Paint
' Transform so (0,0) is in the lower left corner
' and (10, 100) is in the upper right.
e.Graphics.ScaleTransform( _
CSng(Me.ClientSize.Width / 10), _
-CSng(Me.ClientSize.Height / 100))
e.Graphics.TranslateTransform( _
0, Me.ClientSize.Height, _
Drawing2D.MatrixOrder.Append)
' Draw the value.
Const BAR_HGT As Integer = 2
For i As Integer = 0 To 100 Step BAR_HGT + 1
If i <= m_Value Then
e.Graphics.FillRectangle(Brushes.Blue, 0, i, _
10, BAR_HGT)
Else
e.Graphics.FillRectangle(Brushes.PowderBlue, 0, _
i, 10, BAR_HGT)
End If
Next i
End Sub
|
|
|
|
|
|