What is the best way to implement splash screen in Delphi?
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!
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: int64;
FOKToClose: boolean;
public
property OKToClose: boolean read FOKToClose write FOKToClose;
end;
var
SplashForm: TSplashForm;
In the FormCreate:
procedure TSplashForm.FormCreate(Sender: TObject);
begin
FStartTicks := GetTickCount64;
end;
procedure TSplashForm.CloseTimerTimer(Sender: TObject);
const
CTimeout = 3000;
begin
if (GetTickCount64 - 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)
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;