borderAndreaVB free resources for Visual Basic developersborder

AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2013 Andrea Tincani
:: How to put a progress bar inside a statusbar of a MDI form

Author  

Andrea Rossignoli

Language  

VB5, VB6

Operating Systems  

Windows 95, 98 and NT
API Declarations
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Object : How to put a progress bar inside a statusbar of a MDI form.
'Requirements: A MDI form with
' a statusbar named sbMain with two panels
' (for this example the second panel contains the progress bar)
' a picture box with Visible property set to False
' a progress bar named pbMain
' (VERY IMPORTANT NOTE:
' the progress bar must be put inside the picture box
' at design time)
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

'====================================================
'Sub : MDIForm_Load
'Notes:
'====================================================

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

Download
** Click here to download the project (3 Kb, VB6) **
:: Navigation

Home

Form Tips

Previous Tip

Next Tip

:: Search this site
Google
:: Sponsored Links



borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2013 Andrea Tincaniborder