0

(Unit1.pas)

    unit Unit1;

    interface

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

    type
      TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        Timer1: TTimer;
        procedure Timer1Timer(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure OpenSplash;
        procedure ShowProgress;
        procedure CloseSplash;
      end;

    var
      Form1: TForm1;
          X: Integer;
          Total: Integer;
          Percent: Integer;

    implementation

    {$R *.dfm}
     procedure TForm1.OpenSplash;
    begin
      Label1.Caption := '';
      Show;
      Update;



    end;

procedure TForm1.CloseSplash;
begin
  Form1.Destroy;
end;


procedure TForm1.ShowProgress;
begin
Label1.caption:='';
   Total := 1000;
      for X := 1 to Total do
      begin
        Sleep(5);
        Percent := (x * 100) div Total;
        Label1.caption := StringOfChar('|', Percent) + IntToStr(Percent) + '%';
        Label1.Repaint;

      end;
end;

 procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Close;
  Release;
  Form1 := nil;
end;

end.

(Unit2.pas)

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    memo1: TMemo;

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}
function Splash: TForm2;
begin
  if Form2 = nil then begin
    Form2 := TForm2.Create(Application);
  end;
  result := Form2;
end;
end.

(*.dpr)

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

begin
  Application.Initialize;
  Form1.OpenSplash;
  Form1.ShowProgress;
  Application.CreateForm(TForm1, Form1);
  Form1.CloseSplash;
  Application.Run;
end.
Otip88
  • 13
  • 1
  • 8

5 Answers5

1

In your *.dpr, try something like this:

begin
  Application.Initialize;
  FormSplash := TFormSplash.Create( Application );
  FormSplash.OpenSplash;
  // Do the rest of your initialisation...
  // MAKE SURE THERE'S NO CreateForm FOR FormSplash!
  FormSplash.ShowProgress( "Creating a form..." );
  Application.CreateForm( ... , ... );
  ...
  // When you want to modify the splashscreen, do something like this:
  FormSplash.ShowProgress( "Doing something else..." );
  ...
  // Close the splash screen
  FormSplash.CloseSplash;
  Application.Run;
end.

You shouldn't need any references to your FormUtama type.

moobaa
  • 4,482
  • 1
  • 29
  • 31
  • i've done it like this program Project1; uses Forms, Splash in 'Splash.pas' {FormSplash}, SplashProject in 'SplashProject.pas' {Form1}; {$R *.res} begin Application.Initialize; Application.OpenSplash; Application.CreateForm(TForm1, Form1); Splash.CloseSplash; Application.Run; end. still found some error – Otip88 May 28 '09 at 05:11
  • I just need 3 process : 1. Application.Intialize 2.FormSplash.OpenSplash; FormSplash.ShowProgress(FormSplash, TFormSplash); 3.Application.CreateForm(FormUtama, TFormUtama); FormSplash.CloseSplash; Application.Run; but still I have same problem with "ShowProgress" on FormUtama – Otip88 May 28 '09 at 06:43
  • Assigning Application as owner when you create the splashform is a waste of processor cycles if you free it yourself (which you didn't do, BTW - you left it hanging around in memory for the entire lifetime of the program run). – Ken White May 28 '09 at 13:55
  • Yep, you're right - I was coding blind & modifying my preferred code (class methods, etc) to suit Otip88's existing code. Good spot :} – moobaa May 28 '09 at 22:49
1

In your project code you are doing like this:

  Form1.OpenSplash;
  Form1.ShowProgress;
  Application.CreateForm(TForm1, Form1);
  Form1.CloseSplash;

Actually you are using methods from Form1 before creating it. This is dedicated to give problems...

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
1

Maybe this is interesting for you.

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
0

Add this code after the begin statement of the Project Source code (the .dpr file):

Application.Initialize; //this line exists!
 SplashScreen := TMySplashScreen.Create(nil) ;
 SplashScreen.Show;
 SplashScreen.Update; 

After the final Application.Create() and before Application.Run:

SplashScreen.Hide;
SplashScreen.Free;
tbutton
  • 111
  • 6
0

Here are the conclution

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

begin
  Application.Initialize;
  Form1 := TForm1.Create( Application );
  Form1.OpenSplash;
  Form1.ShowProgress;
  Application.CreateForm(TForm2, Form2);
  Form1.CloseSplash;
  Application.Run;
end.

Unit 1

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
  private
    { Private declarations }
  public
    { Public declarations }
    procedure OpenSplash;
    procedure ShowProgress;
    procedure CloseSplash;
  end;

var
  Form1: TForm1;
      X: Integer;
      Total: Integer;
      Percent: Integer;

implementation

{$R *.dfm}
 procedure TForm1.OpenSplash;
begin
  Label1.Caption := '';
  Show;
  Update;



end;

procedure TForm1.CloseSplash;
begin
  Form1.Destroy;
end;


procedure TForm1.ShowProgress;
begin
Label1.caption:='';
   Total := 1000;
      for X := 1 to Total do
      begin
        Sleep(5);
        Percent := (x * 100) div Total;
        Label1.caption := StringOfChar('|', Percent) + IntToStr(Percent) + '%';
        Label1.Repaint;

      end;
end;

end.

Unit 2

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    memo1: TMemo;

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

end.

Thanks a lot for those who answer my question.

Otip88
  • 13
  • 1
  • 8