Visual Basic Helper Routines

SetWindowPos: How to Create a 'Splash Screen' in Visual Basic
     
Posted:   Friday July 30, 1999
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Prerequisites
None.

At some point every VB developer will want or need to create the splash screen that uniquely identifies their app while loading, as well as providing something for the viewer to see while critical initialization sections of the code execute.

This page shows one tried-and-true method of displaying the splash screen before the main application form appears, closing it once initialization has completed. And, depending on your needs (i.e. as a tool for registration reminders), the code can be easily customized to compel non-registered clients to press a close button or click the window before the splash screen will close and allow access to the application.

All applications should start with the Sub Main routine in a BAS module, and the fact that code demos here at VBnet typically do not do so is purely a reflection of the desire to provide the least code possible to demonstrate the sample code. In a 'real' application, using Sub Main provides key benefits: it provides the opportunity to load setting, to check for the existence of key files required by your application, and to load and display a 'control-light' form (the splash screen) before encountering the overhead that initializing and loading your 'control-heavy' main form will display. Doing so also means that, should an error be encountered while processing the Sub Main code (such as a missing file or reference), the application can gracefully shut down having only the incurred the overhead of displaying the splash. And finally, because you are not calling the splash screen from the Load even of a potentially complicated main form, it will appear on-screen much more quickly than had the same code been called from the main form's Form_Load event.

And remember - the splash screen becomes the focal point of your app as it is the first thing that a user of your application sees, so spend some time creating a screen that is unique, artfully designed, and fast loading.

Note: the use of the Sleep API below is for demonstration purposes only. That is, its use is to simulate a delay that may occur while your application performs its initialization routines. I am not advocating its use to persist the splash screen, and it is not meant to be utilized in the final application.

 BAS Module Code
Place the following code into the general declarations area of a bas module:

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 Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public Sub Main()
   
  'show the splash screen. Since the screen doubles 
  'as an About screen, setting 'ShowAsSplash' hides the 
  'OK button and Initializing message as appropriate.
   With dlgSplash
   
      .ShowAsSplash = True
      .Show
      .Refresh

     'perform any really-pre-application initialization required

     '{code}
  
     'Calling Sleep in Sub Main is *only* done to simulate,
     'for this demo, delays that may normally occur as 
     'different initialization actions are performed by 
     'your application on start-up. 
     '
     'Unless you required a forced delay in starting 
     'the application, *all* Sleep statements in this sub 
     'should be removed for when used in your application.
      Call Sleep(1000)  '1 seconds
   
      'show the main application and pause for
      'its paint messages to process
      .Label3.Caption = "Creating application window..."
      .Label3.Refresh
   
      'Sleep used only for delay simulation; remove for real use.
       Call Sleep(2000)  '2 seconds
  
       frmMain.Show

      'perform any other start-up functions as required
      '{code}
  
      .Label3 = "Finalizing settings..."
      .Label3.Refresh
  
     'Sleep used only for delay simulation; remove for real use.
      Call Sleep(2000)  '2 seconds
  
   End With   'dlgSplash
   
 'unload the splash screen
   Unload dlgSplash
   
End Sub
 Form Code
Design your splash form as desired. For this demo, as a minimum, have three labels (Label1 - Label3) and a command button (Command1). Add the following code:

Option Explicit

'Constants for topmost.
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
Private Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE

Private Declare Function SetWindowPos Lib "user32" _
   (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
    ByVal X As Long, ByVal Y As Long, _
    ByVal cx As Long, ByVal cy As Long, _
    ByVal wFlags As Long) As Long

'For property. If using in VB3, VB4-16 or VB4-32, 
'delete this variable and delete the two form  
'Property routines in this code. Declare  
'ShowAsSplash as a Boolean Public (or Global)  
'variable in the BAS module instead.
Private m_mode As Boolean


Private Sub Command1_Click()

  'if the command button is visible,
  'then the splash screen is being
  'shown as a modal About dialog, so
  'the user is responsible for closing
  'the form.
   Unload Me
   
End Sub


Private Sub Form_Load()

   Dim tmp As String
   
  'some text
   tmp = "Put your legal license text here."
   
   Label1.Caption = "Your application name " & App.Major & "." & App.Minor
   Label2.Caption = tmp
   Label3.Caption = "Initializing application. Please wait..."
   
  'initialize the form members.  If the mode 
  'is 'splash', hide the command button (use
  'that when showing the form as an About form) 
  'and display the loading status label.
   Command1.Visible = ShowAsSplash = False  'the OK button
   Label3.Visible = ShowAsSplash = True     'the "Initializing ..." label
   
  'centre (sort of), set topmost, and force 
  'a refresh to assure its graphic is on-screen 
  'before the rest of Sub Main executes.
   Me.Move (Screen.Width - Width) \ 2, ((Screen.Height - Height) \ 2) - 500
   Call SetWindowPos(Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS)
   Me.Refresh

End Sub


Public Property Get ShowAsSplash() As Boolean

   ShowAsSplash = m_mode

End Property


Public Property Let ShowAsSplash(ByVal vNewValue As Boolean)

   m_mode = vNewValue

End Property
 Comments
You should not use the optional Show parameter available in VB5 and VB6 to set the parent form of the dlgSplash to frmMain. Referencing any control or property of a form always causes that form to load, so doing this simply introduces unneeded delays into the displaying of the splash screen by causing your main form to load prematurely, and perhaps before appropriate initialization has completed. The Splash Rule Of Thumb -- get it on-screen fast, then load additional elements for your app.

 
 

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