Understanding Delphi Project and Unit Source Files

An Explanation of Delphi's .DPR and .PAS File Formats

Businessmen talking about file in office
Jose Luis Pelaez Inc/Getty Images

In short, a Delphi project is just a collection of files that make up an application created by Delphi. DPR is the file extension used for the Delphi Project file format to store all the files related to the project. This includes other Delphi file types like Form files (DFMs) and Unit Source files (.PASs).

Since it's quite common for Delphi applications to share code or previously customized forms, Delphi organizes applications into these project files.

The project is made up of the visual interface along with the code that activates the interface.

Each project can have multiple forms that let you build applications that have multiple windows. The code that's needed for a form is stored in the DFM file, which can also contain general source code information that can be shared by all the application's forms.

A Delphi project can not be compiled unless a Windows Resource file (RES) is used, which holds the program's icon and version information. It might also contain other resources too, like images, tables, cursors, etc. RES files are generated automatically by Delphi.

Note: Files that end in the DPR file extension are also Digital InterPlot files used by the Bentley Digital InterPlot program, but they have nothing to do with Delphi projects.

More Information on DPR Files

The DPR file contains directories 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.

It then starts the program by calling the Initialize, CreateForm, and Run methods of the global Application object.

The global variable Application, of type TApplication, is in every Delphi Windows application. Application encapsulates your program as well as provides many functions that occur in the background of the software.

For example, Application handles how you would call a help file from the menu of your program.

DPROJ is another file format for Delphi Project files, but instead stores project settings in the XML format.

More Information on PAS Files

The PAS file format is reserved for the Delphi Unit Source files. You can view the current project's source code through the Project > View Source menu.

Although you can read and edit the project file like you would any source code, in most cases, you will let Delphi maintain the DPR file. The main reason to view the project file is to see the units and forms that make up the project, as well as to see which form is specified as the application's "main" form.

Another reason to work with the project file is when you're creating a DLL file rather than a standalone application. Or, if you need some startup code, such as a splash screen before the main form is created by Delphi.

This is the default project file source code for a new application that has one form called "Form1:"

 program Project1;
 uses
 Forms,
 Unit1 in 'Unit1.pas' {Form1};
 {$R *.RES}
 begin
 Application.Initialize;
 Application.CreateForm(TForm1, Form1) ;
 Application.Run;
 end. 

Below is an explanation of each of the PAS file's components:

"program"

This 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 it as something different.

When you 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. It reads the "uses" clause of the project file to determine which units are part of a project.

"{$R *.RES}"

The DPR file is linked to the PAS file with the compile directive {$R *.RES}. In this case, the asterisk represents the root of the PAS file name rather than "any file." This compiler directive tells Delphi to include this project's resource file, like its icon image.

"begin and end"

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

"Initialize"

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

"Application.CreateForm"

The "Application.CreateForm" statement loads the form specified in its argument. Delphi adds an Application.CreateForm statement to the project file for each form that's included.

This code's job is to first allocate memory for the form. The statements are listed in the order that 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. Instead, use the Project > Options menu.

"Application.Run"

The "Application.Run" statement starts the application. This instruction tells the pre-declared object called Application, to begin processing the events that occur during the run of a program.

Example of Hiding the Main Form/Taskbar Button

The Application object's "ShowMainForm" property determines whether or not a form will show at startup. The only condition for 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;