Pascal. Windows Application in Delphi and Lazarus

Application of type VCL Forms Application in Delphi. The Application in Lazarus

This topic is a continuation of the topic:


Contents


Search other resources:




1. The structure of VCL Forms Application in Delphi

If you create an application of the VCL Forms Application type in the Delphi system, then the program will consist of the following parts:

  • the main module of the program;
  • modules that are connected to the main program.

 

1.1. Main module *.dpr

When creating an application, the main module of the program is placed in a file with the *.dpr extension and has the following code

program Project1;

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

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

As you can see from the above code, an additional module is connected to the main module using the line

Unit1 in 'Unit1.pas' {Form1};

here

  • Unit1 – the name of the module in the program;
  • Unit1.pas – the name of the file, which implements the code of the Unit1 module.

This code does not need to be changed when building applications that use the Windows interface. It is automatically corrected by the Delphi system when new module files are added to the program. For example, after adding a second form named Unit2, which is located in the file Unit2.pas, the uses section will look like

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

...

Also, a new line will be automatically added to the operators section

...

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.

 

1.2. Additional modules. Structure

In applications of the VCL Forms Application type, each meaningful element of the program is usually formed into a separate module (file). A meaningful element can be, for example, a form or a separate file with a set (library) of functions, and the like. In the most general case, the structure of any module is added, the following

// 1. Module header
unit ModuleName;

// 2. Interface section. Public data structures, variables,
// constants, procedures and functions are declared here.
interface

// 2.1. Including additional modules.
uses
...

// 2.2. The descriptive part in the interface part.
// Here are the sections const, type, var.
// The label section cannot be used.

...

// 2.3. Declaring procedures and functions without implementing them.

...

// 3. Implementation section. Internal (hidden) are declared here
// data structures, variables, constants, procedures and functions.
implementation

// 3.1. Including external modules (libraries)
uses
...

// 3.2. Description sections: label, const, type, var

...

// 3.3. Implementation of prcedures and functions.
// Here both public (clause 2.3) and internal (hidden)
// procedures and functions are declared.

...

end.

 

1.3. An example of the structure of an application of type VCL Forms Application. Figure

Figure 1 shows the structure of a VCL Forms Application. The case is considered with two included modules named Unit1, Unit2.

Delphi. The structure of VCL Forms Application

Figure 1. Delphi. The VCL Forms Application. The case with two included forms, which are located in the files Unit1.pas and Unit2.pas

 

2. The structure of an application of type Application in the Lazarus system

The Lazarus system has the ability to create applications that support the Windows interface and are cross-platform. These are applications of type Application. In Lazarus applications, the program structure is similar to that of Delphi Windows applications and consists of the following main parts:

  • the main project file *.lpr (Lazarus Project Main Source);
  • additional modules (unit) connected to the main project file.

 

2.1. Main project file *.lpr

The main project file is as follows

program project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms, Unit1
{ you can add units after this };

{$R *.res}

begin
  RequireDerivedFormResource:=True;
  Application.Scaled:=True;
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

During the process of creating an application in the Lazarus system, this file does not need to be corrected, it is corrected automatically when additional parts are added (removed) in the program. These can be new forms, module files with function libraries, and the like.

 

2.2. The files of additional modules

In the Lazarus system, the component of the plug-in file is the same as in the Delphi system.

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs;

type
  TForm1 = class(TForm)
  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

end.

Modules have two main sections:

  • interface – public components of the module are declared here;
  • implementation – hidden components of the module and the implementation itself are declared here.

 

2.3. An example of a structure for an application of type Application. Figure

Figure 2 shows the structure of Application type in Lazarus system, which has one main form and one additional module.

Lazarus. The structure of Applicaiton type application

Figure 2. The structure of Applicaiton type application in the Lazarus system

The arrow shows the including in the uses section of the Unit1 unit, which corresponds to the main form of the program and is located in the Unit1.pas file. If you need to connect a second unit, then in the uses section this unit is appended to the previous units separated by commas.

 


Related topics