Search code examples
delphimobilefiremonkeysplash-screendelphi-10-seattle

Create own Splashscreen Delphi 10 seattle


Instead of using the png images through the project options for a splashscreen I want to use my own Form for a splashscreen.

I've found a solution for XE2 in the following link, but it doesn't work for Delphi 10 Seattle: https://stackoverflow.com/a/9080804/2728408

Below I have some examples I've tried in my project .dpr:

Example 1:

program Project2;

uses
  FMX.Forms,
  System.SysUtils,
  Unit1 in 'Unit1.pas' {MainForm},
  Unit2 in 'Unit2.pas' {SplashForm};

{$R *.res}

begin
  Application.Initialize;
  SplashForm := TSplashForm.Create(nil);
  SplashForm.Show;
  Application.ProcessMessages;
  Sleep(1000);   // Whatever to control display time of splash screen

  Application.CreateForm(TMainForm, MainForm);
  SplashForm.Close;
  SplashForm.Free;
  Application.Run;
end.

Example 2:

program Project2;

uses
  FMX.Forms,
  System.SysUtils,
  Unit1 in 'Unit1.pas' {MainForm},
  Unit2 in 'Unit2.pas' {SplashForm};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TSplashForm, SplashForm);
  Application.Run;
  Sleep(1000);
  Application.Terminate;// Also tried Application.Destroy
  Application.Initialize;
  Application.CreateForm(TMainForm, MainForm);
  Application.Run;
end.

Example 3:

program Project2;

uses
  FMX.Forms,
  System.SysUtils,
  Unit1 in 'Unit1.pas' {MainForm},
  Unit2 in 'Unit2.pas' {SplashForm};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TSplashForm, SplashForm);
  Application.Run;
  Sleep(1000);
  Application.CreateForm(TMainForm, MainForm);
  SplashForm.Close;
  Application.ProcessMessages;     
  Application.Run;
end.

Anyone has a solution to my problem?


Solution

  • you should not interfere with Application.Terminare/Inititalse the way you do it in the code.

    In Firemonkey, you can change the main form of the application in runtime. So, you should show your splash form first, do all the job you want and then switch to your main form.

    See this for an example: http://www.uweraabe.de/Blog/2016/01/22/a-splash-form-in-firemonkey/

    procedure TFormSplash.FormCreate(Sender: TObject);
    begin
      StartupTimer.Enabled := false;
      StartupTimer.Interval := 500; // can be changed to improve startup speed in later releases
    end;
    
    procedure TFormSplash.SplashImagePaint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
    begin
      StartupTimer.Enabled := not FInitialized;
    end;
    
    procedure TFormSplash.StartupTimerTimer(Sender: TObject);
    begin
      StartupTimer.Enabled := false;
      if not FInitialized then begin
        FInitialized := true;
        LoadMainForm;
      end;
    end;
    
    procedure TFormSplash.LoadMainForm;
    var
      form: TForm;
    begin
      form := TMainForm.Create(Application);
      form.Show;
      Application.MainForm := form;
      Close;
    end;