Search Tools Links Login

Progress Bar Inside Status Bar


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

About this post

Posted: 2019-09-23
By: AndreaRossignoli
Viewed: 421 times

Categories

Visual Basic 6

Attachments

tip050006.zip
Posted: 9/23/2019 7:58:37 AM
Size: 2,515 bytes

Requirements

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

Special Instructions

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


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.