Occasionally, you'd like to see a splash screen when your application is preparing to get started.
I had this with one of my applications, where the initial database loading took some time.
So far that purpose I wanted to give visual feedback to the user with a splash screen, showing that it is loading the database.
It took me so searching to find how to do this, hence this post in the forum.
Step 1 - Create a new form, add whatever you want like a regular form. Set FormStyle of the new form to fsSplash.
Note : I called the unit "LoadingForm" and the name of the form is "fmLoadingForm" (which also defines the TfmLoadingForm type).
Step 2 - Make sure the unit is added to the LPR (project) file:
program MyProgram;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
{$IFDEF HASAMIGA}
athreads,
{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, mainunit, About, LoadingForm
{ you can add units after this };
{$R *.res}
Step 3 - Make sure the Splash form is created and shown before other forms are created. This is also done in the LPR file:
begin
RequireDerivedFormResource:=True;
Application.Scaled:=True;
Application.Initialize;
// Create and show splash screen
fmLoading:=TfmLoading.create(Application);
fmLoading.show;
fmLoading.Update;
Application.ProcessMessages;
Application.CreateForm(TMainForm, MainForm);
// remove this line:
// Application.CreateForm(TfmLoading, fmLoading);
Application.CreateForm(TAboutForm, AboutForm);
// Hide and destroy splash screen
fmLoading.close;
fmLoading.Release; // comment this out if you'd like to use it in the application again
Application.Run;
end.
And that is all there is to it. 😊
In short:
We manually create an instance of TfmLoading, which we show and update.
To make sure we see it, we call Application.ProcessMessages.
Next we have the code do the usual creation of the other form(s) - make sure to remove the line that was added by default to create the SplashScreen form.
Optional:
In my program I have scenario's where I need the SplashScreen again (have the user open another database).
For this purpose, you can commented out the fmLoading.Release line, so the form remains available.