Search Tools Links Login

Progress Bar Inside Status Bar

Posted: 2019-09-23
By: AndreaRossignoli
Viewed: 376

Filed Under:

VB6 Custom Controls/Forms/Menus, VB6 Custom Functions, VB6 Windows API, VB6 Code Cache

Title Uploaded Size 9/23/2019 7:58:37 AM 2,515

This great chunk of code shows how to leverage the Windows API to put a progress bar inside of a status bar, on an MDI form.

Option Explicit

Private Declare Function SetParent Lib "user32.dll" _
   (ByVal hWndChild As Long, _
ByVal hWndNewParent As Long) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" _
   (ByVal hWnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long

'Type RECT
Private Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

Private Const WM_USER As Long = &H400
Private Const SB_GETRECT As Long = (WM_USER + 10)


Private Sub MDIForm_Load()
   Dim rectPanel As RECT
   'Read panel coordinates and dimensions (pixel scale)
   '1 means the second panel of status bar (0-index based)
   SendMessage sbMain.hWnd, SB_GETRECT, 1, rectPanel
   'Transform coordinates from pixel to twip
   'Bottom contains the height, Right contains the width
   rectPanel.Top = (rectPanel.Top * Screen.TwipsPerPixelY)
   rectPanel.Left = (rectPanel.Left * Screen.TwipsPerPixelX)
   rectPanel.Bottom = (rectPanel.Bottom * Screen.TwipsPerPixelY) - rectPanel.Top
   rectPanel.Right = (rectPanel.Right * Screen.TwipsPerPixelX) - rectPanel.Left
   'Move progress bar inside the statusbar panel
   SetParent pbMain.hWnd, sbMain.hWnd
   pbMain.Move rectPanel.Left, rectPanel.Top, rectPanel.Right, rectPanel.Bottom
End Sub

'Sub: ShowStatusBarPercent
Public Sub ShowStatusBarPercent(ByVal iPercent_ As Integer)
   'The progress bar as default accepts only values from 0 to 100
   'This check is necessary to avoid errors
   If iPercent_ > pbMain.Max Then
      iPercent_ = pbMain.Max
   ElseIf iPercent_ < pbMain.Min Then
      iPercent_ = pbMain.Min
   End If
   pbMain.Value = iPercent_
End Sub

'Sub: MyExample
Public Sub MyExample()
   Dim n As Integer

   For n = 1 To 10000
      ShowStatusBarPercent n / 100
      'Do something
   Next n
   ShowStatusBarPercent 0
End Sub

Private Sub sbMain_PanelClick(ByVal Panel As MSComctlLib.Panel)
End Sub


(VERY IMPORTANT NOTE: the progress bar must be put inside the picture box at design time)

Special Instructions

This code originally appeared on, and has been republished here with the permission of Andrea Tincani.

Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.