July 19, 2013

Creating Splash Screen

This article will show how to create a splash screen which is displayed over the main form. It was designed for Smart 1.1.2 and may not work with older versions.

Start by creating a new visual project. Add another form which will contain the splash screen. Rename it to something sensible (say, SplashForm).

In reality you cannot rename it in the Smart IDE as rename function is broken in the 1.1.2 beta. As a temporary workaround you can open the .opp file in the Notepad (make a backup copy first!) and rename all occurrences of ‘Form2’  to ‘SplashForm’.

Edit the main unit to create the new form.

uses 
w3system, w3components, w3forms, w3application, form1,
SplashForm;

type
TApplication = class
(TW3CustomApplication)
private
FForm1:
TForm1;
FSplashForm:
TSplashForm;
public
procedure ApplicationStarting; override
;
end
;

implementation

{ TApplication}

procedure
TApplication.ApplicationStarting;
begin
FForm1 :=
TForm1.Create(Display.View);
FForm1.Name := 'Form1'
;
RegisterFormInstance(FForm1,
True);

FSplashForm :=
TSplashForm.Create(Display.View);
FSplashForm.Name := 'SplashForm'
;
RegisterFormInstance(FSplashForm, False);

inherited
;
end
;

Show the splash screen when the main form is activated for the first time. The best place for that is in the overridden FormActivated method.

type
TForm1=class
(TW3form)
private
FFirst:
boolean;
{$I 'Form1:intf'}
public
procedure FormActivated; override
;
protected
procedure InitializeObject; override
;
end
;

implementation

{ TForm1}

procedure
TForm1.FormActivated;
begin
if FFirst then
begin
FFirst :=
false;
Application.ShowModal('SplashForm', 'SplashPanel', ''
);
end
;
end
;

procedure
TForm1.InitializeObject;
begin
inherited
;
{$I 'Form1:impl'}
W3Label1.Font.Size := 24
;
FFirst :=
true;
end
;

Now you only have to design the splash screen. Place all components on a panel named SplashPanel (as this name is used in the ShowModal call) and write a simple FormActivated method which will close the splash screen after three seconds.

uses 
w3system, w3graphics, w3components, w3forms, w3fonts,
w3borders,
w3application, w3panel, w3label,
w3time;

type
TSplashForm=class
(TW3form)
private
{$I 'SplashForm:intf'}
public
procedure FormActivated; override
;
protected
procedure InitializeObject; override
;
end
;

implementation

{ TSplashForm}

procedure
TSplashForm.FormActivated;
begin
TW3EventRepeater.After(3000, lambda Application.HideModal(mrOK); end
);
// do program initialization here
end
;

procedure
TSplashForm.InitializeObject;
begin
inherited
;
{$I 'SplashForm:impl'}
W3Label1.Font.Size := 24
;
end
;

If you want to do any slow initialization (such as loading resources from the server), the FormActivated method is the proper place for it.


Run and the splash screen will be displayed over the main form.


image


The splash screen will disappear after three seconds and you’ll be able to use the program.


image



You can test the program here or download it from here.

3 comments:

  1. Thank You! I appreciate the tutorial!

    ReplyDelete
  2. "If you want to do any slow initialization (such as loading resources from the server), the FormActivated method is the proper place for it."

    Would you do this in the SplashScreen's FormActivated, or the form you are loading the resources for?

    Thanx

    ReplyDelete
  3. Actually, you can do this in any place. If you have started initialization in the main form's Initialize method, it will continue to run "in the background". Or you can start your initialization from the splash screen's FormActivated, wait until initialization is complete and then close the splash screen.

    ReplyDelete