Tuesday, May 5, 2009

Understanding Delphi Project Files

Since it is quite common for Delphi applications to share code or previously customized forms, Delphi organizes applications into what is called projects.

A project is made up of the visual interface along with the code that activates the interface. Each project can have multiple forms, allowing us to build applications that have multiple windows. The code that is needed for a form in our project is stored in a separate Unit file that Delphi automatically associates to the form. General code that we want to be shared by all the forms in our application is placed in unit files as well. Simply put, a Delphi project is a collection of files that make up an application.

What this means is that each project is made of one or more Form files (files with the .dfm extension) and one or more Unit files (.pas extension). We can also add resource files, and they are compiled into .RES files and linked when we compile the project.

Project File

Each project is made up of a single project file (.dpr). Project files contain directions for building an application. This is normally a set of simple routines which open the main form and any other forms that are set to be opened automatically and then starts the program by calling the Initialize, CreateForm and Run methods of the global Application object (which is actually a form of zero width and height, so it never actually appears on the screen).

Note: The global variable Application, of type TApplication, is in every Delphi Windows application. Application encapsulates your application as well as provides many functions that occur in the background of the program. For instance, Application would handle how you would call a help file from the menu of your program.

Project Unit

Use Project - View Source to display the project file for the current project.

Althogh you can look and edit the Project File, in most cases, you'll let Delphi maintain the DPR file. The main reason to view the project file is so we can see the units and forms that make up the project, and which form is specified as the application's main form.

Another reason to work with the project file is when we are creating a DLL rather than a stand-alone application or need some start-up code, such as a splash screen before the main form is created by Delphi.

Here is the default project file for a new application (containing one form: "Form1"):

program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1) ;
Application.Run;
end.
The program keyword identifies this unit as a program's main source unit. You can see that the unit name, Project1, follows the program keyword (Delphi gives the project a default name until you save the project with a more meaningful name). When we run a project file from the IDE, Delphi uses the name of the Project file for the name of the EXE file that it creates.

Delphi reads the uses clause of the project file to determine which units are part of a project.

The .dpr file is linked with the .pas file with the compile directive {$R *.RES} (in this case ‘*’ represents the root of the .pas filename rather than "any file"). This compiler directive tells Delphi to include this project's resource file. The project's resource file contains such items as the project's icon image.

The begin..end block is the main source-code block for the project.

Although Initialize is the first method called in the main project source code, it is not the first code that is executed in an application. The application first executes the "initialization" section of all the units used by the application.

The Application.CreateForm statement loads the form specified in its argument. Delphi adds an Application.CreateForm statement to the project file for each form you add to the project. This code's job is to first allocate memory for the form. The statements are listed in the order the forms are added to the project. This is the order that the forms will be created in memory at runtime. If you want to change this order, do not edit the project source code. Use the ProjectOptions menu command.

The Application.Run statement starts your application. This instruction tells the predeclared object called Application to begin processing the events that occur during the run of a program.

An Example: Hide Main Form / Hide Taskbar Button

The Application object's ShowMainForm property determines whether or not a form will show at startup. The only condition of setting this property is that it has to be called before the Application.Run line.
//Presume: Form1 is the MAIN FORM
Application.CreateForm(TForm1, Form1) ;
Application.ShowMainForm := False;
Application.Run;

No comments: