Who needs a 1MB MS Common Controls file for a progress bar?! This example shows you how to create your own.
Add a PictureBox to your form called picStatus, and add the code below. You can then call UpdateStatus 0 for 0%, UpdateStatus 0.5 for 50%, UpdateStatus 0.25 for 25% and so on...
Private Sub Form_Load()
picStatus.FillColor = &HFF0000
picStatus.AutoRedraw = True
' Set progress to 0%
UpdateStatus picStatus, 0
End Sub
Sub UpdateStatus(pic As PictureBox, ByVal sngPercent As Single)
Dim strPercent As String
Dim intX As Integer
Dim intY As Integer
Dim intWidth As Integer
Dim intHeight As Integer
'For this to work well, we need a white background and any
color foreground (blue)
Const colBackground = &HFFFFFF ' white
Const colForeground = &H800000 ' dark blue
pic.ForeColor = colForeground
pic.BackColor = colBackground
'
'Format percentage and get attributes of text
'
Dim intPercent
intPercent = Int(100 * sngPercent + 0.5)
'Never allow the percentage to be 0 or 100 unless it is
exactly that value. This
'prevents, for instance, the status bar from reaching 100%
until we are entirely done.
strPercent = Format$(intPercent) & "%"
intWidth = pic.TextWidth(strPercent)
intHeight = pic.TextHeight(strPercent)
'
'Now set intX and intY to the starting location for printing
the percentage
'
intX = pic.Width / 2 - intWidth / 2
intY = pic.Height / 2 - intHeight / 2
'
'Need to draw a filled box with the pics background color to
wipe out previous
'percentage display (if any)
'
pic.DrawMode = 13 ' Copy Pen
pic.Line (intX, intY)-Step(intWidth, intHeight),
pic.BackColor, BF
'
'Back to the center print position and print the text
'
pic.CurrentX = intX
pic.CurrentY = intY
pic.Print strPercent
'
'Now fill in the box with the ribbon color to the desired
percentage
'If percentage is 0, fill the whole box with the background
color to clear it
'Use the "Not XOR" pen so that we change the color
of the text to white
'wherever we touch it, and change the color of the background
to blue
'wherever we touch it.
'
pic.DrawMode = 10 ' Not XOR Pen
If sngPercent > 0 Then
pic.Line (0, 0)-(pic.Width *
sngPercent, pic.Height), pic.ForeColor, BF
Else
pic.Line (0, 0)-(pic.Width,
pic.Height), pic.BackColor, BF
End If
pic.Refresh
DoEvents
End Sub
Comments