RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TApplication.Initialize Method

Provides an opportunity to initialize subsystems.

Pascal
procedure Initialize;
C++
__fastcall Initialize();

Initialize is the first method called by the project source file. It calls the InitProc procedure pointer. By default, the call to Initialize for the application does nothing because the default InitProc pointer is nil (Delphi) or NULL (C++) . 

To use Initialize, the InitProc pointer must be predefined. This can be accomplished in one of two ways: 

In Delphi, you can include a unit that assigns a procedure to InitProc in its initialization section, such as the ComObj unit. You can make this assignment in the initialization section of any of your units. 

In both Delphi and C++, you can create a custom initialization procedure that assigns a value to the InitProc pointer, and add a call to this procedure to the project source prior to the call to Initialize. (In Delphi, you can add it to the initialization section of the unit in which it is declared. In C++, you can use the pragma startup directive in that unit.)

Warning: Only one instance of InitProc can be defined in an application. If more than one unit assigns a value to InitProc, only the last assignment will work. You can, however, call the previous value of InitProc from an initialization procedure, so that all initialization procedures are executed.
For projects that do not assign a value to InitProc, the call to Initialize can be safely deleted from the project source.
Note: Although Initialize is the first method called in the main project source code, it is not the first code that is executed in a GUI application. For example, in Delphi, the application first executes the initialization section of all the units used by the application.
 

C++ Examples: 

 

/*
This example shows progress of loading forms as an
application starts up. The code example is placed in the
project source (*_proj.cpp) file. To see project source, right
click on the executable file in the Project Manager and
select the View Source menu item.
You will need to set up your project with the following steps
before using the code example:
o          Add four additional forms to a default project.
o          Place a TProgressBar on Form5
o          Take the Project|Options|Forms menu option and place
               Form5 on the available forms list.
o          Change the code of your project (*_proj.cpp) file to
               look like the example.
*/
#include <vcl\vcl.h>
#include "Unit5.h"
#pragma hdrstop
//---------------------------------------------------------------------------
USEFORM("TAppCreateForm.cpp", Form1);
USERES("Project1.res");
USEFORM("Unit2.cpp", Form2);
USEFORM("Unit3.cpp", Form3);
USEFORM("Unit4.cpp", Form4);
USEFORM("Unit5.cpp", Form5);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
  try
  {
    Application->Initialize();
    SetApplicationMainFormOnTaskBar(Application, true);
    Application->CreateForm(__classid(TForm5), &Form5);
    Form5->ProgressBar1->Max = 100;
    Form5->Show();   // show a splash screen contain ProgressBar control
    Form5->Update(); // force display of Form5
    Application->CreateForm(__classid(TForm1), &Form1);
    Form5->ProgressBar1->StepBy(25);
    Form5->Label1->Caption =  "Form1 loaded successfully.";
    Form5->Update(); // force display of Form5
    Sleep(3000);
    Application->CreateForm(__classid(TForm2), &Form2);
    Form5->ProgressBar1->StepBy(25);
    Form5->Label1->Caption =  "Form2 loaded successfully.";
    Form5->Update(); // force display of Form5
    Sleep(3000);
    Application->CreateForm(__classid(TForm3), &Form3);
    Form5->ProgressBar1->StepBy(25);
    Form5->Label1->Caption =  "Form3 loaded successfully.";
    Form5->Update(); // force display of Form5
    Sleep(3000);
    Application->CreateForm(__classid(TForm4), &Form4);
    Form5->ProgressBar1->StepBy(25);
    Form5->Label1->Caption =  "Form4 loaded successfully.";
    Form5->Update(); // force display of Form5
    Sleep(3000);
//  delete Form5;
    Application->Run();
  }
  catch (Exception &exception)
  {
    Application->ShowException(&exception);
  }
  return 0;
}

 

Delphi Examples: 

{
This example shows progress of loading forms as an
application starts up. The code example is placed in the
project source (*.dpr) file.  To see project source, right
click on the executable file in the Project Manager and
select the View Source menu item.  You will need to set up
your project with the following steps before using the codeexample:    Add four additional forms to a default project. 
    Place a TProgressBar on Form5
    Take the Project|Options|Forms menu option and place 
          Form5 on the available forms list.
    Change the code of your project (*.dpr) file to look
          like the example.
}
begin
  Application.Initialize;
  with TForm5.Create(nil) do
  try
    Application.MainFormOnTaskbar := True;
    ProgressBar1.Max := 100;
    Show;  // show a splash screen contain ProgressBar control
    Update; // force display of Form5
    Application.CreateForm(TForm1, Form1);
    ProgressBar1.StepBy(25);
    Label1.Caption :=  'Form1 loaded successfully.';
    Update; // force display of Form5
    Sleep(3000);
    Application.CreateForm(TForm2, Form2);
    ProgressBar1.StepBy(25);
    Label1.Caption :=  'Form2 loaded successfully.';
    Update; // force display of Form5
    Sleep(3000);
    Application.CreateForm(TForm3, Form3);
    ProgressBar1.StepBy(25);
    Label1.Caption :=  'Form3 loaded successfully.';
    Update; // force display of Form5
    Sleep(3000);
    Application.CreateForm(TForm4, Form4);
    ProgressBar1.StepBy(25);
    Label1.Caption :=  'Form4 loaded successfully.';
    Update; // force display of Form5
    Sleep(3000);
  finally
    Free;
  end;
  Application.Run;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!