Visual Basic Common Control API Routines
SetParent: Display Modal Form Activity on a Parent Form's 'PhotoShop-style' ProgressBar
     
Posted:   Thursday June 29, 1999
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

SetParent: Add a VB Progress Bar to a VB StatusBar
SendMessage: Change the Colour of a VB ProgressBar
Pure VB: Customizable PhotoShop-Style ProgressBar
Pure VB: Customizable PhotoShop-Style ProgressBar in a MDI App
CreateWindowEx: Creating a Common Control ProgressBar- Overview
CreateWindowEx: Creating the Common Control Flood Panel via the API
SetParent: Display Modal Form Activity in a Parent Form's VB ProgressBar
     
 Prerequisites
None.

Like its sister example, this shows how to call a progress bar housed on a parent form (SDI or MDI) to track activity being performed in another form, even when that form is modal. Unlike the VB progress bar demo however, this demo is for those preferring to use the 'PhotoShop' style progress bar that can be constructed using a simple picture box. 

The demo also shows how to call SetParent to embed the progress bar inside the status bar panel.

 BAS Module Code
None.

 Form Code - Parent Form
Create two new forms.

To the main 'parent' form, add a command button (Command1), a status bar (StatusBar1) with at least 1 panel, and a picture box (Picture1).

To the 'child' modal form, add a command button (Command1). 

Add the following code to the 'Parent' (calling) form:


Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private PSProgBarTextHeight As Integer
Private PSProgBarTextMsg As String

Private Declare Function SetParent Lib "user32" _
  (ByVal hWndChild As Long, _
   ByVal hWndNewParent As Long) As Long
   
   
   
Private Sub Form_Load()

   Me.Move (Screen.Width - Me.Width) \ 2, _
           (Screen.Height - Me.Height) \ 2
   
   SetParent Picture1.hWnd, StatusBar1.hWnd
   
   With Picture1
      .Visible = False
      .Align = vbAlignNone
      .Appearance = ccFlat
      .BorderStyle = ccNone
      .Move 20, _
            40, _
            StatusBar1.Panels(1).Width - 20, _
            StatusBar1.Height - 40
               
      .ZOrder 1
      .BackColor = vbButtonFace
   End With
                      
   StatusBar1.Panels(1).Text = "ProgressBarwill appear here."
   
End Sub


Private Sub Command1_Click()

   Command1.Enabled = False
   
   With Form2
      Set .ParentForm = Me
      .Show vbModal
   End With
   
   Command1.Enabled = True

End Sub


Public Sub PSProgBarInitialize(pbMax As Integer, msg As String)
  
   With Picture1
      .BackColor = vbButtonFace
      .DrawMode = 10         'not Xor pen
      .FillStyle = 0         'solid fill
      .ForeColor = &H808000  'teal (the fill color)
      .ZOrder 0
   
      .ScaleWidth = pbMax
      PSProgBarTextHeight = .TextHeight(msg)
      
      .Visible = True
      
   End With
   
   PSProgBarTextMsg = " " & msg
   
End Sub


Public Sub PSProgBarTerminate()
  
   Picture1.Visible = False
   
End Sub


Public Sub PSProgBarUpdate(progress)
   
   With Picture1
    
     'make sure that the display
     'hasn't already hit 100%
      If progress <= .ScaleWidth Then
    
        'erase the flood
         .Cls
     
         'calculate the string's X & Y
         'coordinates in the picture box
         .CurrentX = 2
         .CurrentY = (Picture1.ScaleHeight - PSProgBarTextHeight) \ 2

         'Print the string in the text 
         'colour using the position above
         'Have to reference Picture1
         'directly since the Print method
         'is not available when using With.
         'Ditto for the Line method below.
         Picture1.Print PSProgBarTextMsg
     
         'print the flood bar to the new
         'progress length in the line colour
         Picture1.Line (0, 0)-(progress, .ScaleHeight), .ForeColor, BF
    
         'required to allow redraw ...
         'safer than using DoEvents.
         .Refresh
      
      End If
    
    End With

End Sub
 Form Code - Modal (Child) Form
Add the following code to the Child form:

Option Explicit
Private frmParent As Form


Private Sub Form_Load()

   Me.Move (Screen.Width - Me.Width) \ 2, _
           (Screen.Height - Me.Height) \ 2
           
End Sub


Private Sub Command1_Click()

   Dim progress As Integer
   
   With frmParent
   
      'initialize the progress bar:
      'max = 2500 (initial and min
      'default to 0)
      .PSProgBarInitialize 2500, "Performing activity .. please wait."
   
      For progress = 1 To 2500
   
         '[activity code here]
     
         'show progress
         .PSProgBarUpdate progress
      
      Next
      
      .PSProgBarTerminate
   
   End With
   
End Sub


Public Property Set ParentForm(ByVal frm As Form)

   Set frmParent = frm

End Property


Private Sub Form_Unload(Cancel As Integer)

   Set frmParent = Nothing

End Sub
 Comments
When run, the progress bar in the parent form will update based on the modal child's activity.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter