What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a monitor that displays a value using a vertical gauge
DescriptionThis example shows how to make a vertical bar that monitors a value in Visual Basic 6. For example, you could use this to monitor network bandwidth, CPU usage, or some other value that changes over time. This example shows how to move the form to the screen's lower-right corner, how to give the form a convenient scale, and how to update the display when a timer fires.
Keywordsgauge, bar, monitor
Subroutine LowerRight moves the form to the screen's lower right corner. It uses the SystemParametersInfo API function to get the work area bounds and moves the form.
' Move the form to the lower right corner
' taking the task bar into account.
Private Sub LowerRight(ByVal frm As Form)
Const GAP As Integer = 120
Dim wa_info As RECT
Dim wa_wid As Single
Dim wa_hgt As Single
Dim wa_left As Single
Dim wa_top As Single

    If SystemParametersInfo(SPI_GETWORKAREA, 0, wa_info, 0) _
        <> 0 Then
        ' We got the work area bounds.
        ' Position the form in the work area.
        wa_wid = ScaleX(wa_info.Right, vbPixels, vbTwips)
        wa_hgt = ScaleY(wa_info.Bottom, vbPixels, vbTwips)
        wa_left = ScaleX(wa_info.Left, vbPixels, vbTwips)
        wa_top = ScaleY(wa_info.Top, vbPixels, vbTwips)
        ' We did not get the work area bounds.
        ' Position the form on the whole screen.
        wa_wid = Screen.Width
        wa_hgt = Screen.Height
    End If

    ' Move the form.
    frm.Move wa_left + wa_wid - Width - GAP, _
             wa_top + wa_hgt - Height - GAP
End Sub
When it loads, the form calls subroutine DrawBar to draw the first value and calls LowerRight to position the form.

It then uses the form's scale properties to make the coordinate system put (0, 0) in the lower left corner and (1, 100) in the upper right corner.

Private m_Value As Single

' Move to the lower right corner.
Private Sub Form_Load()
    ' Start in the middle.
    m_Value = 50

    ' Move the form.
    LowerRight Me

    ' Set scale so (0, 0) is in the lower left corner
    ' and (1, 100) is in the upper right.
    Me.ScaleLeft = 0
    Me.ScaleTop = 100
    Me.ScaleWidth = 1
    Me.ScaleHeight = -100
End Sub
Subroutine DrawBar fills in the form from the bottom up to the position represented by m_Value. The form's scale properties make drawing the bar easier.

When the form's tmrChangeStats Timer control triggers its Timer event, the program changes m_Value.

Private Sub DrawBar()
    Me.Line (0, 0)-(1, m_Value), vbBlue, BF
End Sub

Private Sub tmrChangeStats_Timer()
Const PI As Single = 3.14159265
Const DTHETA As Single = PI / 20
Static theta As Single

    m_Value = _
        Sin(theta) * 25 + _
        Cos(2 * theta) * 15 + _
    theta = theta + DTHETA

End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.