Adding a splash screen to a CAB application
Been awhile since I blogged as I've been sort of out of it missing the MVP Summit and all. Here's a simple way to add a splash screen to your Composite UI Application Block (CAB) based applications. It's pretty simple to implement a splash screen. This is a basic form that will popup with a logo or whatever of your choosing while the application loads. The code below is based on applications generated with the June 2006 version of the Smart Client Software Factory, but the idea is the same and can be applied to any CAB application.
First, create the splash screen. This will just be a simple Winform you add to your Shell project. Call it ShellForm and give it a splash image to display. it helps if you change a few properties to make it more "splashy":
- Change the StartPosition property to CenterScreen
- Change the ShowInTaskbar property to False
- Change the FormBorderStyle property to None
Now drop a picture box on the form and load up your image. Any image will do, but you'll probably want to size the splash screen to match the size of the image (otherwise some shearing might occur).
Now we need to modify two files. ShellApplication.cs and SmartClientApplication.cs. In ShellApplication.cs all you need to do is change the call to the base class of SmartClientApplication to accept your ShellForm. Change the declaration from this:
22 class ShellApplication : SmartClientApplication<WorkItem, ShellForm>
to this:
22 class ShellApplication : SmartClientApplication<WorkItem, ShellForm, SplashForm>
SplashForm is the name of the class you created for the new form. Finally we get down to the meat of the splash screen. In SmartClientApplication.cs we need to do two things, recognize the new parameter being passed into the class and get the splash screen going.
First add a generic to the declaration of the SmartClientApplication class as TSplash:
25 public abstract class SmartClientApplication<TWorkItem, TShell, TSplash> : FormShellApplication<TWorkItem, TShell>
Then initialize it like the WorkItem:
25 public abstract class SmartClientApplication<TWorkItem, TShell, TSplash> : FormShellApplication<TWorkItem, TShell>
26 where TWorkItem : WorkItem, new()
27 where TShell : Form
28 where TSplash : Form, new()
Add a private member variable to hold the splash screen (using the generic type "TSplash"):
30 private TSplash _splash;
Create the object in the constructor:
35 public SmartClientApplication()
36 {
37 _splash = new TSplash();
38 _splash.Show();
39 _splash.Update();
40 }
After the shell gets created, we want to kill off the splash screen. We'll do this in the AfterShellCreated method of the SmartClientApplication class by adding an event handler when the Shell gets activated. Change your AfterShellCreated method to look like this:
46 protected override void AfterShellCreated()
47 {
48 base.AfterShellCreated();
49 Shell.Activated += new EventHandler(Shell_Activated);
50 }
And create the event handler. The handler will remove the Shell.Activated event and dispose of the Splash form:
57 private void Shell_Activated(object sender, EventArgs e)
58 {
59 Shell.Activated -= new EventHandler(Shell_Activated);
60 _splash.Hide();
61 _splash.Dispose();
62 _splash = null;
63 }
That's it! A cool looking splash screen for your CAB application in about 10 minutes.
Note: There was a long thread here on the GDN forums (moved to CodePlex) on doing this. That technique works as well, and gives you the ability to intercept the "loading" of the application as it goes through it's paces. We're using it for one app, but the technique above is a more simple approach that just gets the job done so you might find it easier to implement.