|
|
Title | Add progress indicators to a StatusBar control |
Description | This example shows how to add progress indicators to a StatusBar control in Visual Basic 6. It displays a ProgressBar in the StatusBar, shows a blinking "Working" label, and runs a small animation. |
Keywords | StatusBar, ProgressBar, animation, progress indicator, working |
Categories | Controls, Graphics, Multimedia |
|
|
When the form loads, the program adds several Panels to its StatusBar. It calls subroutine MoveIntoStatusBar to move its ProgressBar control into the first Panel and to move a command button into the third Panel. It sets the picture in the fourth Panel and displays the time in the fifth. It sets the fifth Panel's AutoSize property to sbrSpring so it adjusts its width to fill the status bar on the right.
|
|
Private Sub Form_Load()
Dim new_panel As Panel
StatusBar1.Panels.Clear
With StatusBar1.Panels.Add()
.Width = 2 * 1440
End With
MoveIntoStatusBar StatusBar1, ProgressBar1, 1
With StatusBar1.Panels.Add()
.Text = ""
End With
With StatusBar1.Panels.Add()
.Width = cmdSayHi.Width
End With
MoveIntoStatusBar StatusBar1, cmdSayHi, 3
With StatusBar1.Panels.Add()
.AutoSize = sbrContents
.Width = picStanding.Width
.Picture = picStanding.Picture
End With
With StatusBar1.Panels.Add()
.Style = sbrTime
.AutoSize = sbrSpring
End With
End Sub
|
|
Subroutine MoveIntoStatusBar reparents a control into a StatusBar Panel. It gets the Panel's dimensions and makes the control occupy thePanel.
|
|
Public Sub MoveIntoStatusBar(ByVal sbr As StatusBar, ByVal _
ctl As Control, ByVal panel_number As Long)
Dim r As RECT
' Reparent the control into the status bar.
SetParent ctl.hWnd, sbr.hWnd
' Get the status bar's panel's rectangle.
SendMessage sbr.hWnd, SB_GETRECT, panel_number - 1, r
' Position the control in the panel.
MoveWindow ctl.hWnd, r.Left, r.Top, r.Right - r.Left, _
r.Bottom - r.Top, True
End Sub
|
|
When you click the Go button, the program resets its ProgressBar and enables its timer.
|
|
Private Sub cmdGo_Click()
ProgressBar1.Value = ProgressBar1.Min
ProgressBar1.Visible = True
Timer1.Enabled = True
End Sub
|
|
When the timer fires, the program determines whether the ProgressBar has reached the end. If it has, the program hides the ProgressBar, disables the timer, clears the second Panel's text, and resets the fourth Panel's picture to the "doing nothing" picture.
If the ProgressBar hasn't reached the end, the program increases the ProgressBar's value. It toggles the second Panel's text between a blank string and "Working..." Finally, it loops the fourth Panel's picture through a small animation.
|
|
Private Sub Timer1_Timer()
Static walker As Integer
If ProgressBar1.Value + 5 > ProgressBar1.Max Then
ProgressBar1.Visible = False
Timer1.Enabled = False
StatusBar1.Panels(2).Text = ""
StatusBar1.Panels(4).Picture = picStanding.Picture
Else
ProgressBar1.Value = ProgressBar1.Value + 5
If StatusBar1.Panels(2).Text = "" Then
StatusBar1.Panels(2).Text = "Working..."
Else
StatusBar1.Panels(2).Text = ""
End If
walker = (walker + 1) Mod 4
StatusBar1.Panels(4).Picture = _
picWalking(walker).Picture
End If
End Sub
|
|
This example isn't perfect.
- Moving the command button into third Panel messes up its events so it doesn't receive a Click event. You could probably fix that with subclassing.
- The fourth Panel resizes its pictures so it distorts them slightly.
If you solve these problems, let me know.
Usama Foad found a workaround for the first issue. While the command button doesn't receive a Click event as it should, it does receive MouseUp event. Unfortunately MouseUp fires if the user presses the button, moves the mouse off of the button, and then releases the mouse. So you need this slightly more complicated code to determine whether the mouse is over the button during the event.
|
|
Private Sub cmdSayHi_MouseUp(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
' See if the mouse is over the button.
If X < 0 Or _
Y < 0 Or _
X > cmdSayHi.Width Or _
Y > cmdSayHi.Height Then Exit Sub
MsgBox "MouseUp"
End Sub
|
|
Thanks Usama!
|
|
|
|
|
|