Microsoft KB Archive/113999

= VB3 How to Create a Setup-like Status Bar in Visual Basic =

Article ID: 113999

Article Last Modified on 10/29/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q113999





SUMMARY
Many applications use a status bar to display the progress of an installation or other lengthy process. Often the percent completed is printed in the middle of the bar and it changes color as the status bar passes over it. This article illustrates how to achieve this effect using Visual Basic for Windows.



MORE INFORMATION
The simplest way to specify the range of the status bar is to determine what the zero-based range will be and specify that value as the ScaleWidth of the horizontal scroll bar. Then you don't have to scale the data for each new sample.

Changing the color of the percentage displayed within the picture box is done by specifying the DrawMode as Not XOR Pen with a compatible background. When the bar is drawn, an exclusive OR is performed on each pixel. If the pixel is red, it is made white and vice versa. The text must be placed first because Print does not support DrawMode.

The following program demonstrates how to display a red status bar with a red or white text message centered in it. Colors other than red are specified by changing the ForeColor property of the Picture Box Control.

Step-by-Step Example
 Start a new project in Visual Basic. Form1 is created by default. Add a picture box (Picture1) and a command button (Command1) to the form.  Add the following code to the Form_Load event: Sub Form_Load Picture1.AutoRedraw = True Picture1.BackColor = &H80000003& Picture1.DrawMode  = 10 Picture1.FillStyle = 0 Picture1.ForeColor = &H00000080 End Sub   Add the following code to the general declarations section of Form1: Dim tenth as Long

' Enter the following Declare statement as one, single line: 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

Sub UpdateStatus (FileBytes As Long)

'     ' Update the Picture1 status bar '      Static progress 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

' Enter the following two lines as one, single line of code: Picture1.CurrentX = (Picture1.ScaleWidth - Picture1.TextWidth(Txt$)) \ 2

' Enter the following two lines as one, single line of code: Picture1.CurrentY = (Picture1.ScaleHeight - Picture1.TextHeight(Txt$)) \ 2

Picture1.Print Txt$

' Enter the following two lines as one, single line of code: Picture1.Line (0,0)-(progress,Picture1.ScaleHeight), Picture1.ForeColor,BF

' Enter the following two lines as one, single line of code: r = BitBlt(Picture1.hDC, 0, 0,Picture1.ScaleWidth,         Picture1.ScaleHeight, Picture1.hDC, 0, 0, SRCCOPY)

End Sub   Add the following code to Click event procedure of Command1: Sub Command1_Click Picture1.ScaleWidth = 109 tenth = 10 For i = 1 to 10 Call UpdateStatus(tenth) x = timer While Timer < x + .75 doevents Wend Next End Sub  Press the F5 key to run the program, and then click the Command1 button.</li></ol>

<div class="references_section">