Creating Splash Screens


A splash screen is a form that is displayed at application startup, typically while the application is busy creating other forms. The splash screen usually displays the company's name and logo and the application's name and version.

From a technical point of view, a splash screen is totally unnecessary in small applications because they often load very quickly. Splash screens are used in more complex applications to distract the user from counting the seconds (yeah, right) it takes for an application to load.

The first thing you have to do is add a new form to the project, name it SplashForm, and remove it from the auto-create list.

Splash forms are usually borderless and positioned in the center of the screen. So, set the BorderStyle property to bsNone and the Position property to poScreenCenter.

Now, open the main project file and dynamically create the SplashForm form. See Listing 12-21A for the Delphi version and Listing 12-21B for the C++ version. To use the SplashForm form in the main project file, don't forget to #include the form's header file.

Listing 12-21A: Creating the splash form

image from book
program Project1; uses   Forms,   Unit1 in 'Unit1.pas' {Form1},   Unit2 in 'Unit2.pas' {SplashForm}; {$R *.res} begin   Application.Initialize;   { Create & display the splash screen }   SplashForm := TSplashForm.Create(Application);   SplashForm.Show;   SplashForm.Update;   Application.CreateForm(TForm1, Form1);   { Destroy the splash screen }   SplashForm.Free;   Application.Run; end.
image from book

Listing 12-21B: Creating the splash form, C++ version

image from book
#include <vcl.h> #include <windows.h> #pragma hdrstop #include "Unit2.h" // SplashForm USEFORM("Unit1.cpp", Form1); USEFORM("Unit2.cpp", SplashForm); WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {    try    {       Application->Initialize();       SplashForm = new TSplashForm(Application);       SplashForm->Show();       SplashForm->Update();       Application->CreateForm(__classid(TForm1), &Form1);       delete SplashForm;       Application->Run();    }    catch (Exception &exception)    {       Application->ShowException(&exception);    }    catch (...)    {       try       {         throw Exception("");       }       catch (Exception &exception)       {         Application->ShowException(&exception);       }    }    return 0; }
image from book

As you can see, the splash screen is a modeless form. If you display the splash screen as a modal form, the application will not run until you close it. The splash screen is displayed with two methods: Show and Update. The Update method actually displays the form in this case — it forces the form to display itself.

Since this example only creates the main form automatically, the splash screen will only be displayed for a short time. To see the splash screen a bit longer, you have to pause the application for a second or two by calling the Sleep procedure. The Sleep procedure is declared in the Windows unit (or windows.h if you're using C++Builder) and accepts a single integer value: the number of milliseconds the application should remain inactive. You should call the Sleep procedure before you release the splash screen.

Listing 12-22: Displaying the splash screen

image from book
program Project1; uses   { The Sleep procedure is declared in the Windows unit }   Forms, Windows,   Unit1 in 'Unit1.pas' {Form1},   Unit2 in 'Unit2.pas' {SplashForm}; {$R *.res} begin   Application.Initialize;   { Create & display the splash screen }   SplashForm := TSplashForm.Create(Application);   SplashForm.Show;   SplashForm.Update;   Application.CreateForm(TForm1, Form1);   { Destroy the splash screen, but first wait 2 seconds. }   Sleep(2000);   SplashForm.Free;   Application.Run; end. 
image from book

image from book
Figure 12-23: A splash screen



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net