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 bars floating over the desktop
DescriptionThis example shows how to make a vertical bar gauge 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, how to update the display when a timer fires, and how to draw a value using bars. It also shows how to set the form's region to include only the bars so the bars seem to float over the other windows on the screen.
Keywordsgauge, bar, monitor, SetWindowRgn, region
CategoriesGraphics, API
Subroutine MakeBarRegions makes a series of window regions for the gauge's bars. It combines them using the CombineRgn API function and then uses SetWindowRgn to restrict the form to the regions. Whatever the program draws is restricted to the regions.
Private Sub MakeBarRegions()
Dim i As Integer
Dim all_rgn As Long
Dim new_rgn As Long
Dim all_rect As RECT

    ' Make the form 200 pixels tall.
    Me.Height = ScaleY(200, vbPixels, vbTwips)

    ' Make the first rectanglular region.
    all_rgn = CreateRectRgn(0, 0, Me.ScaleWidth, BAR_HGT)

    ' Make the other rectangular regions.
    Me.ScaleMode = vbPixels
    For i = BAR_HGT * 2 To Me.ScaleHeight Step BAR_HGT * 2
        new_rgn = CreateRectRgn(0, i, Me.ScaleWidth, i + _
        CombineRgn all_rgn, all_rgn, new_rgn, RGN_OR
    Next i

    ' Set the form's region.
    SetWindowRgn Me.hWnd, all_rgn, True
End Sub
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 MakeBarRegions to restrict the form to its bars, calls 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()
    ' Make the window's regions.

    ' 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.