Link to home
Start Free TrialLog in
Avatar of hyper66
hyper66

asked on

How do i get my splash screen to display only once

How do i get my splash screen to display only once and add somthing to the registry so it won't load the next time the program loads.
ASKER CERTIFIED SOLUTION
Avatar of inter
inter
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,
Here's a way to do it. It has not been tested for typos:

Unit MySplashForm;

Uses ...reg;
  ...
  ...

procedure Splash;
begin
  with TRegistry.Create do
  begin
    if ( OpenKey( '\software\mybasekey\mykey', False ) and ValueExists( 'MyValue' ) and ReadBool( 'MyValue' ) ) then
    begin
      Self.Free; // or something
    end else
    begin
      Show;
      Update;
      OpenKey( '\software\mybasekey\mykey', True );
      WriteBool( 'MyValue', True );
    end;
    Free; // TRegistry class instance
  end;
end;

/// John
Avatar of hyper66
hyper66

ASKER

Hello Igor!

   Thank's for replying, I can get a splash screen to display with code listed about but. How do add the code to the project source!! here is the code I am trying to add it to.

================================================================
Splash.dpr
================================================================
program TSplash;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Splash in 'Splash.pas' {SplashScreen};

{$R *.RES}

begin
   try
    SplashScreen := TSplashScreen.Create(Application);
    SplashScreen.Show;
    SplashScreen.Update;
    Application.Initialize;
    Application.CreateForm(TForm1, Form1);
  Repeat
      Application.ProcessMessages;
       Until SplashScreen.CloseQuery;
      SplashScreen.Close;
     finally
    SplashScreen.Free;
  end;
  Application.Run;
end.


================================================================
Splash.pas
================================================================

unit Splash;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, jpeg;

type
  TSplashScreen = class(TForm)
    Timer1: TTimer;
    Image1: TImage;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  SplashScreen: TSplashScreen;

implementation

{$R *.DFM}

procedure TSplashScreen.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
end;

procedure TSplashScreen.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
CanClose := Not Timer1.Enabled;
end;

end.


How Can i Add the code above to this code i already have. i tryied to add it but i crash splash.exe everytime.  Thank's Hyper66

                                                     
 
Avatar of hyper66

ASKER

Adjusted points to 150
So first lets change our checking proc as

function DoISplash:boolean;
var
  T : TRegistry;
const
  MyKey = 'SOFTWARE\Hyper66\testkey';
begin
  T :=TRegistry.Create;
  try
    T.RootKey := HKEY_LOCAL_MACHINE;
    Result := not T.KeyExists(MyKey);
    if Result then
      T.CreateKey(MyKey);
    T.CloseKey;
  finally
    T.Free;
  end;
end;

so in your main proc code you can do the following:

var
  doI : boolean;
begin
 DoI := DoISplash;
 try
   if DoI then
   begin
     SplashScreen := TSplashScreen.Create(Application);
     SplashScreen.Show;
     SplashScreen.Update;
   end;
   Application.Initialize;
   Application.CreateForm(TForm1, Form1);
   if DoI then
   begin
     Repeat
       Application.ProcessMessages;
     Until SplashScreen.CloseQuery;
     SplashScreen.Close;
   end;
  finally
   if DoI then SplashScreen.Free;
  end;
  Application.Run;
end.

but it is more convinient to do the following modifications

1 - In the splash form delete Closequerry and Add just OnClose event and inside it set
    Action := caFree;
    also modify TimerEvent as follows
procedure TSplashScreen.Timer1Timer(Sender: TObject);
begin
  Enabled := false;
  Close;
end;
    so when the timer expires the form is closed and freed
2 - Use the following code (the form automatically destroy itself)
.
begin
 if DoISplash then
 begin
   SplashScreen := TSplashScreen.Create(nil);
   SplashScreen.Show;
   SplashScreen.Update;
 end;
 Application.Initialize;
 Application.CreateForm(TForm1, Form1);
 Application.Run;
end.

regards, igor
Avatar of hyper66

ASKER

Thank's, Igor I could'nt get it to work with T.RootKey := HKEY_LOCAL_MACHINE; but the code work's great without Rootkey any ideas why???