How To Create a Setup-Like Status Bar in Visual Basic

This article was previously published under Q147809
This article has been archived. It is offered "as is" and will no longer be updated.
Many applications use a status bar to display the progress of aninstallation or other lengthy process. Often the percent completed isprinted in the middle of the bar and it changes color as the status barpasses over it. This article illustrates how to achieve this effect usingVisual Basic for Windows. As a note, the professional and enterpriseedition contain a progress bar control that could be used instead. ForWin16 applications, the sample calldlls in vb\samples\calldlls uses shapecontrols to achieve the same effect.
The simplest way to specify the range of the status bar is to determinewhat the zero-based range will be and specify that value as the ScaleWidthof the horizontal scroll bar. Then you don't have to scale the data foreach new sample.

Changing the color of the percentage displayed within the picture box isdone by specifying the DrawMode as Not XOR Pen with a compatiblebackground. When the bar is drawn, an exclusive OR is performed on eachpixel. If the pixel is red, it is made white and vice versa. The text mustbe placed first because Print does not support DrawMode. The followingprogram demonstrates how to display a red status bar with a red or whitetext message centered in it. Colors other than red are specified bychanging the ForeColor property of the Picture Box Control.

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add a picture box (Picture1) and a command button (Command1) to the form.
  3. Add the following code to the general declarations section of Form1:
    Dim tenth As Long#If Win32 ThenPrivate Declare Function BitBlt Lib "gdi32" _(ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, _ByVal nWidth As Long, ByVal nHeight As Long, _ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, _ByVal dwRop As Long) As Long#ElsePrivate Declare Function BitBlt Lib "GDI" (ByVal hDestDC As _Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth _As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, _ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As _Long) As Integer#End IfSub UpdateStatus(FileBytes As Long)'--------------------------------------------------------------------' Update the Picture1 status bar'--------------------------------------------------------------------    Static progress As Long    Dim r As Long    Const SRCCOPY = &HCC0020    Dim Txt$    progress = progress + FileBytes    If progress > Picture1.ScaleWidth Then        progress = Picture1.ScaleWidth    End If    Txt$ = Format$(CLng((progress / Picture1.ScaleWidth) * 100)) + "%"    Picture1.Cls    Picture1.CurrentX = _    (Picture1.ScaleWidth - Picture1.TextWidth(Txt$)) \ 2    Picture1.CurrentY = _    (Picture1.ScaleHeight - Picture1.TextHeight(Txt$)) \ 2    Picture1.Print Txt$    Picture1.Line (0, 0)-(progress, Picture1.ScaleHeight), _    Picture1.ForeColor, BF    r = BitBlt(Picture1.hDC, 0, 0, Picture1.ScaleWidth, _        Picture1.ScaleHeight, Picture1.hDC, 0, 0, SRCCOPY)End SubPrivate Sub Command1_Click()    Picture1.ScaleWidth = 109    tenth = 10    For i = 1 To 11        Call UpdateStatus(tenth)        x = Timer        While Timer < x + 0.75            DoEvents        Wend    NextEnd SubPrivate Sub Form_Load()    Picture1.FontBold = True    Picture1.AutoRedraw = True    Picture1.BackColor = vbWhite    Picture1.DrawMode = 10    Picture1.FillStyle = 0    Picture1.ForeColor = vbBlueEnd Sub						
  4. Select F5 to run the program, and then click Command1.
Visual Basic Setup Wizard file SETUP1.BAS.

Article ID: 147809 - Last Review: 02/24/2014 08:24:13 - Revision: 2.2

  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 Enterprise Edition
  • kbnosurvey kbarchive kbhowto kbapi KB147809