Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




Share:
Notifications
Clear all

[Solved] Lazarus Pascal - How to show a splash screen when a program starts

2 Posts
1 Users
0 Likes
4,328 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

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.

 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2660
Topic starter  

Tip: in some cases calling Application.ProcessMessages doesn't do the trick.

I solved this in my code by adding another Application.ProcessMessages call:

  // Create and show splash screen
  fmLoading:=TfmLoading.create(Application);
  fmLoading.show;              
  Application.ProcessMessages;
  fmLoading.Update;
  Application.ProcessMessages;

 


   
ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: