1. The Core Project Files
A standard Delphi VCL (Visual Component Library) Windows application consists of two main file types: the Project file and one or more Unit files.
| File Extension | Example Name | Purpose |
.dpr | Project1.dpr | Project File. The starting point and compiler entry point. It contains the main execution block of the application. |
.pas | Unit1.pas | Unit File. Contains the Object Pascal code for a specific module, usually associated with a form. |
.dfm | Unit1.dfm | Form File. The binary file that stores the visual design of the form (components, properties, layout). |
.dproj | Project1.dproj | Project Options. An XML file storing all project settings (compiler directives, linking options, search paths). |
2. The Project File (.dpr)
The .dpr file is the entry point of your application, similar to the main() function in C/C++. It tells the compiler which units to include and defines the sequence of execution.
program Project1;
uses
Vcl.Forms,
Unit1 in 'Unit1.pas' {Form1}; // Tells the compiler to include Unit1
{$R *.res} // Compiler directive for resource files
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1); // Creates the instance of Form1
Application.Run; // Enters the main event loop
end.
program Project1;: Declares the name of the program.uses: Lists all units (code files) required by the project.Application.CreateForm: A crucial line that instantiates the form object (Form1) from the class definition (TForm1).
3. The Unit File (.pas)
The Unit is the fundamental building block for organizing code in Delphi. Every form you create automatically generates a new .pas unit to hold its corresponding code.
A unit file is divided into three major sections:
A. Interface Section
This section declares everything that the outside world (other units) needs to know about this unit.
unit Unit1;
interface // Start of Interface Section
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
// Declaration of the TForm1 class
TForm1 = class(TForm)
Button1: TButton; // Component declaration
// Declaration of methods/events visible to other units
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1; // Global variable that references the form instance
implementation // End of Interface Section
B. Implementation Section
This section contains the actual Object Pascal code for the procedures and functions declared in the interface section. Code and variables declared here are private and hidden from other units.
implementation // Start of Implementation Section
{$R *.dfm} // Links the visual form file (.dfm)
// The actual code for the button's click event
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Button was clicked!');
end;
end. // End of Unit1
C. Initialization/Finalization (Optional)
These optional blocks allow you to execute code when the unit is loaded into memory (initialization) or when the application shuts down (finalization). This is typically used for library setup or cleanup.
4. The uses Clause (Dependencies)
The uses clause manages dependencies:
- Placement: It appears in both the
interfaceandimplementationsections. - Rule: If you need a unit (e.g.,
System.IOUtilsfor file operations) only for code in the implementation section, place it there. This speeds up compilation and avoids circular unit references (the “interface uses implementation” problem). If it is needed for a type declaration in the interface, place it in the interface section.
// Example: IOUtils is only needed for the code logic, not the class definition
implementation
uses
System.IOUtils;
// ... code follows
