Holy Splash Screens Batman!
- Details
- Written by David Corrales
- Last Updated: 09 October 2019
- Created: 16 April 2019
- Hits: 7518
The villainous Mr. Freeze strikes again! You innocently load some modules before displaying your GUI and Mr. Freeze puts the freeze on it!
What will the poor citizens of Gotham do? Will they double-click on the executable again?! No wait! Help is on the way! Fire off the Bat-signal!
Na Na Na Na Na! Batman! Batman! Batman!
How do you thwart Mr. Freeze's evil plan? With a splash screen of course!
Splash Screen! Splash Screen! Splash Screen! (sung to the Batman theme)
Now the good citizens of Gotham can gaze upon the hypnotizing splash screen! No longer will they wonder what happened to the GUI and double-click again!
Mr. Freeze has been put on ice! When the form is finally displayed, the splash screen goes away in triumph!
How do you call the splash screen in times of danger?
With the Bat-Signal!
What? You don't have a Bat-Signal? Then use this snippet within PowerShell Studio instead:
Shortcut: showSplashScreen
Important: The Show-SplashScreen snippet is included with PowerShell Studio 2019 v5.6.161 or greater.
The snippet contains the helpful Show-SplashScreen function:
Show-SplashScreen [-Image] <Image> [-Title <String>] [-Timeout <Int32>] [-PassThru] [<CommonParameters>]
Show-SplashScreen [-Title <String>] [-Timeout <Int32>] [-ImageLocation] <String> [-PassThru] [<CommonParameters>]
The following assemblies must be loaded to use this function outside a GUI script:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Show-SplashScreen to the rescue!
To protect your GUI from Mr. Freeze's holdup, simply call the function in the load event of your form:
$formSample_Load = {
$imagePath = Join-Path -Path (Get-ScriptDirectory) -ChildPath 'SplashImage.png'
Show-SplashScreen -ImageLocation $imagePath -Title 'Loading...'
}
The Show-SplashScreen function accepts an Image object or a path to an image:
Important: An image must be supplied to the splash screen in order for the function to work.
Image (Mandatory)
Specifies the image object to be displayed in the splash screen.
Show-SplashScreen -Image $imageList1.Images[0]
This example uses an ImageList control to supply the image to the function.
ImageLocation (Mandatory)
Specifies the path to the image to be displayed in the splash screen.
$imagePath = Join-Path -Path (Get-ScriptDirectory) -ChildPath 'SplashImage.png'
Show-SplashScreen -ImageLocation $imagePath
Using a relative path? Use the Get-ScriptDirectory function snippet to ensure the path works when the script is packaged into an executable.
Title
You can set an optional title on your splash screen.
Show-SplashScreen -ImageLocation $imagePath -Title 'Loading...'
The text will be displayed on the splash screen's title bar:
Timeout
Specifies the number of seconds the splash screen should display before it closes itself.
Default: 2 seconds
Note: While the parent form is loading, the splash screen will remain visible even if the timeout time has elapsed.
PassThru
Use the PassThru switch parameter to return the splash screen form object. You can use the form to close the splash screen or update the title while loading your main form:
$formSample_Load = {
$imagePath = Join-Path -Path (Get-ScriptDirectory) -ChildPath 'SplashImage.png'
#Show a splash screen before loading
$splashForm = Show-SplashScreen -ImageLocation $imagePath -Title 'Loading...' -PassThru
#Initialize Step 1
…
#Initialize Step 2 and update splash screen text
$splashForm.Text = 'Loading Modules...'
…
#Optional: Close the splash screen when we are done
$splashForm.Close()
}
The splash screen's title is updated:
If you want to have the main form to show up before displaying the splash screen, then move the initialize script and the Show-SplashScreen call to the Form's Shown event:
$formSample_Shown={
$imagePath = Join-Path -Path (Get-ScriptDirectory) -ChildPath 'SAPIEN.png'
#Show a splash screen before loading
$splashForm = Show-SplashScreen -ImageLocation $imagePath -Title 'Loading...'
…
}
Safe at last!
Gotham city is safe once again! Until next time! Stay tuned on the same Bat-Time, same Bat-Channel!
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.