Monday 12/15/2008 13:31

Easy Splashscreen Writing Guide by Nullified

How to create your own splash screen in less than 3 minutes!


See also:


Notes
Naming conventions
Position
Shapes and transparency is a follow up on eswg and involves tranparency to get even more cool effects while using a splashscreen, you can use the sourcefiles of eswg and then continue to eswgv2 or first follow this one and use the files you created yourself


Quick Start

This is a basic and very short tutorial for creating your own splash screen. The best way to learn Delphi is to have an example, and study the code. Do not rely on 3rd party components when you do not have to! Create your own splash screen in less than 3 minutes. Following this tutorial, you will learn to create a splash screen, that can be disabled by un checking a check box on the mainform, it makes use of an Inifile to store the value and enable and disable the appearance of the Splash screen. About the included example, to test the splash screen a simple restartbutton was added to it, in order to make that work Shellapi was added to the uses clausule of Form1.



1.
Create a new project


2.
Add another form to it [menu File= New= Form]


3.
To the uses clausule of Form1 add: Inifiles
...
uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ExtCtrls, IniFiles;

...


4.
To the private section of Form1 add: Inifile : Tinifile;
...
private

    IniFile: TIniFile

...


5.
Add the Unit2 to the uses clausule of Form1 just below Implementation
...
implementation

uses

  Unit2;

{$R *.dfm}

...


6.
Drop a check box on Form1 [Checkbox1], and a label [Label1], set the Label its caption property to: ShowSplash [or any Variable name, that you prefer to use as indicator that the check box serves for enabling and/or disabling the splash screen]


7.
Doubleclick Form1 and change the OnFormCreate event as below:
...
procedure TForm1.FormCreate(Sender: TObject);

var

   Splash: Boolean;

begin

Inifile := TIniFile.Create(Changefileext(application.exename, '.ini'));

with Inifile do

  try

    Splash:= ReadBool('ShowSplash','LastChoice', True );

    Checkbox1.Checked := Splash;

   if Splash then

   begin

     Sleep(1000); /This is the time you want the splash to show in miliseconds

     Form2.Close;

     Form2.Release;

   end;

  finally

    Free;

end;


end;

...


8.
Create the OnFormClose event on Form1 and change it to the following:
...
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

var

   Splash: Boolean;


begin

  Inifile := TIniFile.Create(Changefileext(application.exename, '.ini'));

  Splash := Checkbox1.Checked;

  with Inifile do

  try

   WriteBool('ShowSplash','LastChoice', Splash);

  finally

   Free;

  end;


end;

...


9.
Open Form2, add inifiles to the uses clausule  of Form2:
...
uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, ExtCtrls, IniFiles;

...


10.
Declare the boolean and the Tinifile just below the var section, after the Form2 declaration:
...
var

  Form2: TForm2;

  Splash: Boolean;

  Inifile: TIniFile;


implementation

{$R *.dfm}

...


11.
Below the inclusion of the Delphi Form [{$R *.dfm}], in Form2 add the following code in green
...
implementation

{$R *.dfm}




initialization

Inifile := TIniFile.Create(Changefileext(application.exename, '.ini'));

  with Inifile do

    try

      Splash:= ReadBool('ShowSplash','LastChoice', True);

     finally

      Free;

    end;

    if Splash then

      begin

        Form2 := TForm2.Create(nil);

        Form2.Show;

        Form2.Update;

      end;



end.

 ...

12.
Change the borderstyle of Form2 to bsNone in the object inspector

Done!


Notes


Of course a splash screen that has the default color and is empty, is not really cool so add an image to it, or anything you want to appear there, see the included example for a simple basic idea, but technically speaking you are finished and have just created your own Splash Screen, and you no longer have to rely on 3rd party components!

splash screen writing guide example



Naming conventions



Note that variables can be called anything you wish [except for keywords etc], it is good practice to name them to something useful, so the name of it already tells you at least a bit what is it about. Then if you are finished with your project you can always do a search and place to make the variables shorter [saving a bit of space] and making them more difficult to guess, should you wanna'. Also, it is a good thing to name the components, and the forms, and units to something meaningful. The advantage of properly named forms, are so you can tell them apart from the standard forms with names given to them by delphi itself, and they wont be written over accidentally easy, plus it is easier to work with them in a single project that has more forms. However, for ease of use of this tutorial, I used the standard names.

Also, for your own sake [readability] it is best to name your forms like something more clear. In this tutorial however, I used the standard names Delphi gives them, to not make it more complex and lengthy. My own splash screen would be named something like: SplashForm, while Form1 would be named: MainForm. Same goes for components, Checkbox1 does not say anything what it is used for or what it is about, I myself would name it something like: chkSplash.  


Position


The position can and maybe should be altered too, but this is purely personal, so I did not discuss that. In the included example, position of Form1 was set to poDesktopCenter, and position of Form2 was set to poMainformCenter, so the splashscreen appears centered in where the Form1 will appear.


The finished forms are below:
===============================================================
Unit1.pas
================================================================

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ExtCtrls, IniFiles;

type

  TForm1 = class(TForm)

    Checkbox1: TCheckBox;

    Label1: TLabel;

    procedure FormCreate(Sender: TObject);

     procedure FormClose(Sender: TObject; var Action: TCloseAction);

  private

    IniFile: TIniFile;

  public

    { Public declarations }

  end;

var

  Form1: TForm1;

implementation

uses

  Unit2;


{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);

var

   Splash: Boolean;

begin

Inifile := TIniFile.Create(Changefileext(application.exename, '.ini'));

with Inifile do

  try

    Splash:= ReadBool('ShowSplash','LastChoice', True );

    Checkbox1.Checked := Splash;

   if Splash then

   begin

     Sleep(1000);

     Form2.Close;

     Form2.Release;

   end;

  finally

    Free;

end;

end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

var

   Splash: Boolean;

begin

  Inifile := TIniFile.Create(Changefileext(application.exename, '.ini'));

  Splash := Checkbox1.Checked;

  with Inifile do

     try

       WriteBool('ShowSplash','LastChoice', Splash);

     finally

   Free;

  end;

end;

end.

================================================================


================================================================
Unit2.pas [Your splashscreen]
================================================================

unit Unit2;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, ExtCtrls, IniFiles;


type

  TForm2 = class(TForm)

  private

  public

  end;

var

  Form2: TForm2;

  Splash: Boolean;

  Inifile: TIniFile;

implementation

{$R *.dfm}

initialization

Inifile := TIniFile.Create(Changefileext(application.exename, '.ini'));

  with Inifile do

    try

      Splash:= ReadBool('ShowSplash','LastChoice', True);

     finally

      Free;

    end;

    if Splash then

      begin

        Form2 := TForm2.Create(nil);

        Form2.Show;

        Form2.Update;

      end;

end.

================================================================
End of Easy Splashscreen Writing Guide
================================================================

 

 

===============================================================
Easy Splashscreen Writing Guide v2 using Tramsparency
================================================================

Shapes and transparency


You can get really cool results if you add an image that has a non rectangular shape, see the below pictures.
splash screen writing guide examplesplash screen writing guide examplesplash screen writing guide example using transparency

Note that in order to make use of transparency you can add extra units to delphi like gif or png components, however since pngimage has some sort of trouble because of it being used for commercial purposes, I will not offer it for download here, but it still can be found easily, give me a message in case you need help.

If you want to read how to create partially transparent splashscreens then click here which sends you to eswgv2, which is a minute of your time


================================================================

Easy Splashscreen Writing Guide by Nullified can be downloaded together with demo files at my site for free for always go here