What is the best way to implement splash screen in Delphi?
-
3Don't forget to think about form position / dual screen monitors. – Tobiasopdenbrouw Jul 29 '10 at 07:16
3 Answers
Create a form, make it's FormStyle
= fsStayOnTop
, set it's border style to none and it's caption to blank. This will create a form that doesn't have a caption bar at the top. Drop a TImage
on the form and load your bitmap into it.
Drop a TTimer on the form (this will be used to make sure the splash screen stays up for at least some period.
Here's the code I have in my splash form:
TSplashForm = class (TForm)
Image1: TImage;
CloseTimer: TTimer;
procedure CloseTimerTimer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
private
FStartTicks: integer;
FOKToClose: boolean;
public
property OKToClose: boolean read FOKToClose write FOKToClose;
end;
var
SplashForm: TSplashForm;
In the FormCreate:
procedure TSplashForm.FormCreate(Sender: TObject);
begin
FStartTicks := GetTickCount;
end;
procedure TSplashForm.CloseTimerTimer(Sender: TObject);
const
CTimeout = 3000;
begin
if (GetTickCount - FStartTicks > CTimeout) and OKToClose then
Close;
end;
procedure TSplashForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TSplashForm.FormDestroy(Sender: TObject);
begin
SplashForm := nil;
end;
In your project file, do something like this:
begin
SplashForm := TSplashForm.Create(nil)
Application.Initialize;
Application.Title := 'My Program';
//create your forms, initialise database connections etc here
Application.CreateForm(TForm1, Form1);
if Assigned(SplashForm) then
SplashForm.OkToClose := True;
Application.Run;
end.
(most of this code was written off the top of my head, it might not compile right off the bat)

- 5,414
- 26
- 38
-
3Small addition: To not interfere with debugging in the IDE it is better to set `FormStyle := fsStayOnTop;` in the `OnCreate` handler of the form, but only if `DebugHook` is not set. Otherwise the splash will obscure some of the IDE. If you are unlucky enough to debug with one monitor, that is. – mghie Jul 29 '10 at 06:24
-
Yes, that's a good idea. I actually have my software check the command line for `/nosplash`, and run the program with that in the debugger – Nat Jul 29 '10 at 06:28
-
3Even better, skip the "fsStayOnTop". Your splash screen has no business obscuring my other applications, especially since that only happens if your splash screen is on the screen for so long, I get bored looking at it and switch to some other, more entertaining app. – Cosmin Prund Jul 29 '10 at 06:29
-
That is also true... If I could guarantee that it stayed above other windows in my program I would do that, but I suggest my clients to use the `/nosplash` command line option if they really feel strongly about it. – Nat Jul 29 '10 at 06:31
-
@Nat: about the "/nosplash". That only makes sense if the splash screen is purely decorative. The current trend is to keep the splash screen on screen while the application loads, and display some sort of progress indication of what's going on. In such a context, removing the splash screen serves no purpose: the application takes about the same time to start, but displays nothing while it's loading. An other trick I'm using for client-server applications is to turn the splash screen into a very good looking "login" form. That way the user sees the splash screen but is not annoyed by it. – Cosmin Prund Jul 29 '10 at 06:33
-
All true. It's all horses for courses really... I have yet to have anyone complain, but I do understand the sentiment. I think it really does depend on how long your splash screen is up for. – Nat Jul 29 '10 at 06:38
-
Just be careful that GetTickCount wraps around after 40-some days of uptime. I've been bitten by that one. – Scott W Jul 29 '10 at 12:58
-
-
Missing code: procedure TSplashForm.FormDestroy(Sender: TObject); begin SplashForm := nil; end; Without this code the "if Assigned(SplashForm) then" is always true, even after the form is destroyed. – BennyBechDk Jul 30 '10 at 07:20
-
Instead of FStartTicks, I think is smarter to use FStopTicks and that as "GetTickCount64 + cTimeout" in the create, and change the if to "if (GetTickCount64 > FStopTicks) and OKToClose then", then the calculation is done only once. – BennyBechDk Jul 30 '10 at 07:25
-
@BennyBechDK: Thanks for that... I have added the FormDestroy. As far as the time out calculation is concerned, what performance problem are we exactly trying to fix here? I challenge you to measure a perceptible performance benefit in a real world use of this splash screen. – Nat Jul 30 '10 at 11:29
-
@Nat: I performance benefit is not significant. It was just a general comment about how to implement a timeout check, so the code is most effecient in all cases. It is just an appiniom about codeing style. – BennyBechDk Aug 02 '10 at 06:44
-
-
@Nat in what unit is GetTickCount64 declared under? Delphi is telling me that its undeclared. – ple103 Mar 20 '13 at 01:29
-
1@petersmileyface You can just use `GetTickCount`... It was added as a suggestion... But it really doesn't need to be there. – Nat Mar 20 '13 at 10:00
There's nothing technically difficult about the splash screen, it's just a form that pops up and then goes away. So the best way to implement a splash screen in Delphi is: Get a graphics designer to draw one for you!

- 25,498
- 2
- 60
- 104
this is how i do it: first create a new unit by adding an empty form to your project(file->new->form),lets call this unit splashy, set its(form's) border style to bsnone and set its name property to 'splashscreen' or what ever you desire, design it(form) by first designing a picture using mspaint or some thing then dropping a timage component on form and openening the image file through it ,add line: 'splashscreen: Tsplashscreen;(agian you can name it what ever you want)' to units var section then add this unit's name to to first unit's uses clause and the code below to first units form oncreate event:
procedure TForm1.FormCreate(Sender: TObject);
var
splash : Tsplashscreen;
begin
Splash := TSplashScreen.Create(Application);
Splash.Show;
Sleep(1000); //as long as you want screen to be displayed 1000 = 1 second
Splash.Hide;
Splash.Free;
end;

- 1,820
- 1
- 28
- 41
-
6Sleep(whatever) from your main thread is a poor idea: Your GUI will freeze! Try dragging some other window over your splash screen while it's sleeping and you'll see why. Nat's idea of using an timer is better because message processing can at least carry on, repainting the splash screen if needed. – Cosmin Prund Jul 29 '10 at 09:12