Progress Bar Inside Status Bar

By | 2019-09-23

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
'*******************************************************************
'API
'*******************************************************************

'SetParent
Private Declare Function SetParent Lib "user32.dll" _
   (ByVal hWndChild As Long, _
ByVal hWndNewParent As Long) As Long
'SendMessage
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

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

Usage

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
      DoEvents
      'Do something
   Next n
   ShowStatusBarPercent 0
End Sub

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

Attachments

FileUploadedSize
936-20190923-075837-tip050006.zip9/23/2019 7:58:37 AM2515
Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.