Compiling and running a Delphi project. "Window" and "graphics" components


In the Delphi environment, a project is developed - a set of files that make up the application. It is recommended to store project-related files in a separate folder.

Any project includes at least six files:

  • project1.dpr – main file project, generated by the system when creating a new application;
  • unit1.pas – the first module (unit) of the program, which automatically appears at the beginning of work;
  • unit1.dfm – form description file, used to save information about the appearance of the main form;
  • project1.res – resource file, icons are stored in it, raster images, cursors. At a minimum, it contains an application icon;
  • project1.dof – options file, is a text file for saving settings associated with this project (for example, compiler directives);
  • project1.cfg – configuration file, contains information about the state of the environment.

In addition, the project may include files with pictures, video clips, sounds, help system files, etc. However the listed elements it is controlled by the programmer himself.

The default names given by the system can be changed. It is considered good programming style to use names that carry meaning.

After compiling the program, files with the extensions are obtained:

  • dcu – compiled modules;
  • exe – executable file;
  • ~pa, ~dp – backup files (previous versions).

In addition to the modules associated with the form, you can create separate modules that are designed according to the usual rules of the Object Pascal (Delphi) language and are saved in separate files and whose names are indicated in the uses section of the project or the modules in which they are used. The main project file is a text file containing program code written in Object Pascal. This file includes all used program modules and contains statements for launching the application. When you create a new application, Delphi automatically creates a project file. The code for a project file containing one form is given below.

program Project1; uses Forms, Unit1 in "Unit1.pas" (Form1) ; ($R *.begin res) Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end.

The uses section connects the Forms system module and the Unit1 form module. The name of the form is given in curly brackets. The compiler directive ($R *.res) connects resources to the resulting exe file.

The body of the program contains statements that prepare the application for operation (initialize), create a form, and begin executing the application. As new forms are created, the contents of this file change automatically. This file is manually adjusted only in special cases.

To see the code for the project file, you need to execute the View|Unit command through the menu or using the button on the toolbar, and then select Project1 in the dialog box. The ViewUnit window is used to display the code of the project file and its modules.

When creating an application, Delphi generates an empty form, the module text of which is given below.

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class (TForm) private ( Private declarations ) public ( Public declarations ) end ; var Form1: TForm1; implementation ($R *.dfm) end .

A module begins with the reserved word unit, followed by the name of the module. The module name matches the name of the file in which it is saved.

The interface section describes program elements (types, variables, constants, procedures, functions) that can be used by other program modules. Everything that goes into the implementation section is only available within the module. In addition to declarations, this section includes the implementation of all procedures and functions mentioned in the interface part, as well as the code for any additional procedures and functions. In general, the module structure looks like:

  • title;
  • interface declaration section interface;
  • implementation section;
  • initialization section initialization;
  • finalization section;
  • terminator end.

The optional initialization section contains operators that perform the initial configuration of the module and are executed once when the module is accessed for the first time. The optional finalization section contains statements that are executed only once upon any program termination: normal or emergency. The finalization section is introduced into the program only if there is an initialization section.

A Delphi project consists of forms, modules, project settings, resources, etc. All this information is placed in files. Many of these files are automatically created by Delphi when you build your application. Resources, such as bitmaps, icons, etc., are found in files that you obtain from other sources or create using the many tools and resource editors at your disposal. In addition, the compiler also creates files. Let's take a quick look at some of these files, since knowing which files contain what information will often help you in difficult situations. When you design your application, Delphi creates the following files:

Project file (.dpr)

This text file used to store information about forms and modules. It contains operators for initializing and launching the program for execution.

Module file (.pas)

Each form you create, and starting with Delphi 5, each frame, has a corresponding module text file used to store the code. Sometimes you may want to create modules that don't involve forms. Many of Delphi's functions and procedures are stored in modules.

Shape File (.dfm)

This is a binary or text file that is created by Delphi to store information about your forms. Each form file has a corresponding module file (.pas).

Project Settings File (.dfo)

This file stores project parameter settings.

Package information file (.drf)

This binary is used by Delphi when working with packages.

Resource file (.res)

This binary file contains the icon used by the project and other resources.

File group file (.bpg)

This is the file created if you are working with a group of projects.

Package file (.dpk)

This is a binary package file.

Backup files (.-dp, .~df, .~pa)

These are backup files for project, form and module files, respectively. If you have hopelessly messed up something in your project, you can change the extensions of these files accordingly and thus return to the previous, uncorrupted version.

Windows configuration file (.dsk)

The file stores the configuration of all windows.

Diagram Files (ddp)

Files of diagrams created on the diagrams page of the Code Editor window.

The following group of files is created by the compiler:

Executable file (.exe)

This is the executable file of your application. It is a standalone executable that doesn't require anything else unless you use libraries contained in DLLs, OCXs, etc., and unless you use runtime package support.

Module object file (.dcu)

This is a compiled module file (.pas) that is linked into the final executable file.

Dynamic link library (.dll)

This file is created if you are designing a DLL.

This is a binary file containing a package header and a list of files belonging to it

Compiled runtime package file (.bpl)

This is a runtime package - a DLL file with certain Delphi specifics.

And finally, other Windows files that can be used by Delphi:

Help files (.hip)

These are standard Windows help files that can be used by your Delphi application.

Image files or graphic files (.wmf, .bmp, .ico)

These files are commonly used in Windows applications to create an attractive and friendly user interface.

The main part of your application is the project file (.dpr), which contains the Object Pascal code that starts your program and initializes other modules. In most cases, you can create a complete Delphi application without ever looking at this file. It is created and modified automatically by Delphi as you develop your application. The name you give the project file when you save it becomes the name of the executable file.

The code below is an example of what the project file looks like if you started a new project and did not change the file names and forms in it.

program Project1
uses
Forms
Unit1 in "unit1.pas" (Form 1);

Begin
Application.CreateForm(TForm, Form1);
Application.Run(Form1);
end.

This code contains only two executable statements. The first one creates Form1, and the second one starts the application.

All changes to the project file when adding new forms, changing form names, etc. are supported automatically by Delphi. If you need to view the source file of the project, you need to run the View | Project Source. But usually you don't need this.

You should not change the project file unnecessarily. This can lead to inconsistency in the names used and other problems.

Information about Delphi forms is stored in two files. A file with the .dfm extension stores information about the appearance of the form, its dimensions, location on the screen, etc. A text file with the .pas extension stores the code of the module corresponding to this form. The names of both of these files are the same. You specify this name the first time you save your module.

In versions of Delphi prior to Delphi 5, the .dfm file was a binary file. In order to view it in text form, you had to right-click on the form and select the View as Text section from the pop-up menu. Then a text file of the form appeared in the Code Editor window. It could be edited if desired. To return to the graphical representation of the form, you had to right-click on the file text and select the View As Form command.

All these features are retained in newer versions. But, starting with Delphi 6, the user is given the opportunity to choose in what form he prefers to save the form file: text or binary. If you right-click on the form, you will see the Text DFM indicator in the pop-up menu. By default it is enabled and form files are stored as text. If you turn off this indicator, form files will be stored in binary form. You can create modules that are not tied to specific forms. For example, in a large application, it is useful to have a module containing constants, variables, procedures, and functions used in various modules. The presence of such a module allows you to reduce the number of mutual links between different modules.

In addition, such a module can be used in your different projects. To create a new module in your project that is not associated with any form, you need to run the File | New | Other and in the New Items window that opens, on the New page, click on the Unit icon.

Now we need to say a few words about the executable file.exe and about the package files. There are two types of application programs you can develop in Delphi: stand-alone executable files and programs that support runtime packages. In the latter case, the size of the executable file is noticeably reduced, but you must supply the user not only with the executable module, but also with the package files that support it. The following lessons will explore the pros and cons of using runtime packages. Without dwelling on these issues here, we note that when debugging, it probably always makes sense to enable support for runtime packages. This will significantly reduce the size of executable files and save you a lot of disk space.

To use this feature, run Project | Options. In the project options window that opens, select the Packages page and on the Packages page turn on the Built with runtime packages indicator. At the same time, it is useful to turn on the Default indicator in the same window, which will ensure that this setting remains the default setting for all your future projects.

Just be sure to turn off the package support option when you final compile your completed project if you intend to deliver a standalone executable module to users.

The problem solved on a computer is implemented in the form application program, which is called an application for short. The basis for developing an application in the Delphi environment is a project. The central part of the project is the form on which the components necessary to solve a specific problem are placed. In this sequence - project - forms - components - we will consider the process of creating an application in the Delphi environment. As we present the material, we will often refer to the example of calculating the ideal weight, which was discussed in the first chapter. If you forgot it, read the first chapter again.

7.1. Project

7.1.1. Project concept

The application is assembled from many elements: forms, software modules, external libraries, pictures, icons, etc. Each element is placed in a separate file and has a strictly defined purpose. The collection of all the files needed to create an application is called a project. The compiler processes the project files sequentially and builds an executable file from them. The main project files can be divided into several types:

  • Form description files are text files with the DFM extension that describe forms with components. These files remember the initial property values ​​you set in the Properties window.
  • Program module files are text files with the PAS extension containing source program codes in the Delphi language. In these files, you write methods for handling events generated by forms and components.
  • The main project file is a text file with the DPR extension containing the main program block. The project file includes all the software modules used and contains statements to run the application. The Delphi environment creates and controls this file itself.

Based on the above, we can depict the process of creating an application in the Delphi environment from setting the task to obtaining a finished executable file (Figure 7.1):

Figure 7.1. The process of creating an application in the Delphi environment

Let's look at the purpose and internal structure of project files. This will help you navigate the project more easily.

7.1.2. Form Description Files

Do you remember how you started getting acquainted with the Delphi environment? Of course, from the form. So, the first component of the project is a text file with the DFM extension that describes the form. The DFM file stores the properties of the form and its components that you set in the properties window when designing the application. The number of DFM files is equal to the number of forms used in the application. For example, in our example about the ideal weight, only one form is used, therefore there is only one DFM file - Unit1.DFM.

If you want to look at the contents of the DFM file, call the context menu of the form by right-clicking and select the command View as Text(Figure 7.2).


Figure 7.2. Switching to the text view of the form using the View as Text command of the context menu

In response, the Delphi environment instead graphic image the form will show the following text in the code editor :

object Form1: TForm1 Left = 250 Top = 150 Width = 400 Height = 303 Caption = "Weight Calculator" Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = "MS Sans Serif" Font.Style = OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object Label1: TLabel Left = 64 Top = 48 Width = 93 Height = 13 Caption = "Specify your height:" end object Label2: TLabel Left = 64 Top = 144 Width = 84 Height = 13 Caption = "Your ideal weight:" end object Button1: TButton Left = 248 Top = 64 Width = 75 Height = 25 Caption = "Compute" TabOrder = 0 OnClick = Button1Click end object Button2: TButton Left = 248 Top = 160 Width = 75 Height = 25 Caption = "Close" TabOrder = 1 end object Edit1: TEdit Left = 64 Top = 64 Width = 121 Height = 21 TabOrder = 2 end object Edit2: TEdit Left = 64 Top = 160 Width = 121 Height = 21 TabOrder = 3 end end

Despite such a long description text, it is not at all difficult to understand it. Here, in a special language, the initial values ​​for the properties of the form Form1 and its components Button1, Button2, Edit1, Edit2, Label1, Label2 are set. You don't need to know much more because you will always use visual design tools and work with a graphical representation of the form rather than a textual description. If so, let's quickly return to the graphical representation without making any changes to the text. To do this, call the code editor context menu and select the command View as Form(Figure 7.3).


Figure 7.3. Switching to a graphical representation of a form using the View as Form command of the context menu

A graphical image of the form will appear on the screen again. If you do make adjustments to the text, they will be reflected in the appearance of the form.

The shape description file (DFM file) is needed only at the design stage. When the application is built, the form description from the DFM file is placed in a special data area of ​​the executable file (resource area). When a form is created while the application is running, its description is retrieved from the resources area and used to initialize the form and its components. As a result, the form is displayed on the screen as you specified during design.

7.1.3. Program module files

Each form in the project has its own program module (unit), containing all form-related declarations and event processing methods written in Delphi. Software modules are located in separate files with the PAS extension. Their number may exceed the number of forms. Why? Because in some cases, program modules may not belong to forms, but contain auxiliary procedures, functions, classes, etc. Our ideal weight problem is very simple, so there is only one software module related to shape. Take the time to study it carefully:

We will give the necessary comments to the text of the program module. At the very beginning after the keyword unit the module name is written

The Delphi environment generates a list of modules without your participation and automatically updates it when you add new components to the form. However, the list of connected modules can be changed directly in the code editor (manually).

type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Edit1: TEdit; Edit2: TEdit; Label1: TLabel; Label2: TLabel; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private ( Private declarations ) public ( Public declarations ) end;

Components placed on a form are represented by form fields. We have six components on the form, so there are also six fields in the class description. The field names match the component names specified in the properties window.

After the fields are the headers of the event handling methods. The Delphi environment generates the name of each such method automatically based on the name of the component and the name of the event generated by it. For example, for a button Button1 event handling method OnClick called Button1Click.

Note that fields representing form components, as well as event handling methods, receive a visibility attribute published(it is the default for all heirs TForm). Thanks to this, you can work with them on a visual level, for example, see their names in the properties window. Since the Delphi environment itself manages the contents of the section published, never modify this section manually (in the code editor), use visual tools: the component palette and the properties window. Remember:

  • when you place components on a form, the Delphi environment itself adds the corresponding fields to the form's class description, and when you remove components from the form, the environment removes the fields from the class description;
  • When you define event handlers in a form or components, Delphi itself defines the corresponding methods in the class, and when you remove all code from the event handlers, Delphi removes the methods themselves.

For your convenience, empty sections are pre-declared in the form class private And public, in which you can place any auxiliary fields, methods and properties. The Delphi environment does not “see” them, so you can work with them only at the program code level. You can put in the section private attributes that are needed only by the form itself, and in the section public- attributes that are also needed by other forms and modules.

After the class description comes the declaration of the form object itself:

Please do not think that this directive includes all files with the DFM extension. Only one DFM file is connected, which describes the form of this module. The name of the DFM file is obtained by replacing the asterisk with the name of the module in which the directive is written.

Attention! If you want to remove an event handling method and remove references to it, simply make the method empty - remove all code you wrote, including comments, and local variable declarations. When saving or compiling a project, the Delphi environment itself will remove empty methods from the text.

Upon careful study of the source code of the module, one question remains unclear: how methods are called Button1Click And Button2Click when clicking on the button form, because there is not even a hint of this in the module text. Everything is very simple. Take a look at the DFM file. In addition to setting property values, you will also find setting event handlers.

object Button1: TButton ... OnClick = Button1Click end object Button2: TButton ... OnClick = Button2Click end

This description ensures that form methods are bound to appropriate events. By the way, the meaning of the given fragment of the description becomes more transparent if we remember that events in the Delphi language are actually properties, but their values ​​are pointers to methods. Thus, setting events is not much different from setting form properties, because by nature they are one and the same.

We delved quite deeply into the internal structure of form description files and program module files and, we admit, we did this on purpose in order to give you a complete understanding of the issue, without forcing you to take anything far from obvious on faith. Now it’s time to go up to the project level and see what unites all these files.

7.1.4. Main project file

In order for the compiler to know which specific files are included in the project, some kind of organizing principle is necessary. And it really is. This is a so-called project file with a DPR extension (short for Delphi Project). It is a main Delphi program file that connects using the operator uses all module files included in the project. There is only one DPR file for each project.

When you're on command File/New/Application When you start developing a new application, the Delphi environment automatically creates a project file. As new forms are created, the contents of this file are modified automatically. When you are finished and ready to compile the project, the DPR file will contain a list of program modules that will be supplied as input to the compiler. To see the contents of the DPR file of our application that calculates ideal weight, select the command from the Delphi environment menu Project / View Source. A new page will appear in the code editor with the following text:

program Project1; uses Forms, Unit1 in "Unit1.pas" (Form1); ($R *.res) begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end.

Let us comment a little on this text. Module connection Forms required for all programs because it contains the definition of the object Application. This object underlies any graphical application and is available throughout his work.

The module Unit1 that is connected next contains the definition of the form. The name of the form is given in curly brackets. Directive in indicates that the module is a necessary part of the project and exists as source code in the Delphi language.

The ($R *.res) directive connects so-called resources to the resulting executable file, in this case the application icon. This icon will be visible on the Taskbar.

Next comes the main program block, containing calls to three methods of the object Application. Calling a Method Initialize prepares the application for operation, method CreateForm loads and initializes the form Form1, and the method Run activates the form and starts executing the application. Actual running time of the method Run- this is the running time of the application. Exit method Run occurs when the user closes the main application form; As a result, the application terminates.

Attention! Never edit a DPR file manually. Leave this work to the Delphi environment. Adding and removing modules, as well as managing the creation of forms, is carried out using commands and dialog boxes in the environment.

7.1.5. Other project files

Above we looked at the main project files. In addition to them, there are a number of additional files:

  • A file with the DOF extension (short for Delphi Options File), which stores the compilation and assembly parameters of the project specified by the programmer;
  • A file with the DSK extension (short for Desktop), which stores the Delphi environment settings for of this project. To have Delphi save its settings in a DSK file, select the command from the menu and in the dialog box Environment Options on the tab Preferences in Group Autosave options check the box Project Desktop.
  • A file with the CFG extension (short for Configuration), where settings for console version compiler.
  • A file with the DCI extension (short for Delphi CodeInsight), where the Delphi environment stores the settings you made for the software “prompter” (CodeInsight).
  • A file with the DCT extension (short for Delphi Component Templates), where your homemade components are stored.
  • A file with the DMT extension (short for Delphi Menu Templates), where your homemade menu templates are stored.
  • A file with the DRO extension where settings and your additions to the component repository are stored.
  • A file with the TODO extension is a notebook for storing programming tasks and short notes.
  • A file with the DDP extension (short for Delphi Diagram Portfolio), where graphic diagrams, clearly explaining the relationships between components.
  • Resource file with the RES extension (short for RESource). For example, it stores the application icon that appears on the Taskbar. We'll talk about how to set an application icon when discussing project management issues.

The project may also include logically autonomous elements: bitmaps (BMP files), icons (ICO files), help files (HLP files), etc., but they are controlled by the programmer himself.

Now you can clarify the drawing reflecting the composition of the project (Figure 7.4):


Figure 7.4. Project composition in the Delphi environment

So, the composition of the project is clear. Now we need to figure out how to manage it - create and save a project, add and remove modules, set compilation options, build and run the application. This is what we will do now.

7.2. Project management

7.2.1. Create, save and open a project

When you start the Delphi environment, a new project is automatically created. This is for your convenience. If you need to create a new project without overloading the Delphi environment, simply run the menu command File/New/Application. As a result, the old project will be closed and a new one will be created in its place. Delphi always places a blank form in a new project.

During the application development process, you add components to the form, write event handlers, add child forms to the project, in general, design the application. When something is already done, it makes sense to save the project. To do this, execute the main menu command File/Save All. The environment will first ask for a name for the program module with the form, and then a name for the project (by the way, you already saved the file in the first chapter). If a file with the entered name already exists on disk, Delphi will tell you this and ask you to confirm whether to overwrite the existing file or write it under a different name.

If you suddenly need to replace the project name with another name, use the menu command File / Save Project As.... If you need to replace the module name, use the command File / Save As.... These operations are elementary and do not require further explanation.

To open a project previously saved on disk in the Delphi environment, simply execute the main menu command File/Open.... A dialog box will appear on the screen (Figure 7.5), where you must indicate or select from the list provided the directory and name of the downloaded project.


Figure 7.5. Project selection window

With an open project, you can continue working: fix it, compile it, execute it, and don’t forget to save it.

7.2.2. Project management window

When creating a more or less complex application, a programmer should always know what stage of development he is at, have an idea of ​​the composition of the project, and be able to quickly activate required file, add a new file to the project or delete an unnecessary one, set compilation parameters, etc. For this purpose, in the Delphi environment there is a project management window - window Project Manager. In fact, it is a visual tool for editing the main project file. The project management window is called from the main menu with the command View/Project Manager. After selecting this command, a window will appear on the screen in which the project is presented in the form of a tree (Figure 7.6).


Figure 7.6. Project management window

The Project1 element in bold is our project. Its name coincides with the name of the executable file, which is obtained as a result of compiling and assembling all project modules. The list of modules is displayed as subordinate elements. If an element is a form, then it, in turn, consists of two subordinate elements: a form program module (PAS file) and a form description (DFM file).

Project management is performed using the context menu, which is called by right-clicking on the Project1 element (Figure 7.7).


Figure 7.7. Project context menu

Team Description
Add… Adds an existing file (module) to the project.
Remove File... Removes a file (module) from the project.
Save Saves the project to disk.
Options… Calls up the project settings dialog ( Project Options).
Activate Makes the project active (when working with a group of projects, see paragraph 7.2.3).
Close Closes the project.
Remove Project Removes the project from the group (see paragraph 7.2.3).
Build Sooner Moves the project up in the list that determines the order in which projects are built. Used when working with a group of projects (see paragraph 7.2.3).
Build Later Moves the project down the list that determines the order in which projects are built. Used when working with a group of projects (see paragraph 7.2.3).
Compile All From Here Compiles modified projects in order, starting with the selected project. Used when working with a group of projects (see paragraph 7.2.3).
Build All From Here Compiles all projects in order, starting with the selected project. Used when working with a group of projects (see paragraph 7.2.3).

An individual module is controlled using the context menu, which is called up by right-clicking on the corresponding element, for example Unit1 (Figure 7.8).


Figure 7.8. Module context menu in the project management window

Purpose main commands The context menu is briefly described in the following table:

Now you can always find out what files a particular project consists of, and managing it will not be difficult for you.

7.2.3. Project groups

In practice, several projects can be logically related to each other, for example, a dynamic link library project is related to an application project that uses this library. The Delphi environment allows you to combine such projects into a group. It is for this purpose that the project management window has a root element ProjectGroup1, the subordinate elements of which are logically related projects. The order of the elements determines the order in which projects are built. You can change the order using the commands Build Sooner And Build Later, which are in the context menu called by right-clicking on the corresponding project (Figure 7.7).

On this moment There is only one project in the group - Project1. To add other projects to the group, use the context menu, which is called up by right-clicking on the ProjectGroup1 element (Figure 7.9).


Figure 7.9. Project group context menu

The purpose of the context menu commands is briefly described in the following table:

When several projects are combined into a group, the Delphi environment creates a special text file describing this group. The file has a BPG extension (from Borland Project Group), and its name is requested from the user. For groups consisting of one single project, a BPG file is not created.

7.2.4. Setting Project Settings

A project has many different settings that you can use to control the process of compiling and building your application. You can set project parameters in the window Project Options. To do this, select the command in the main menu Project/Options... or in the project management window, call the context menu of the Project1 element and select the command Options.... A dialog box will appear on the screen; All you have to do is set the required parameter values ​​in it.

The Project Options dialog box consists of several tabs. There are a lot of parameters, so we will consider only those that are used most often.

On the tab Forms(Figure 7.10) you can set the main form of the application ( Main form) and in the list Auto-create forms specify the forms that will be created simultaneously with the main form.


Figure 7.10. Project parameters window. Forms tab

On the tab Application(Figure 7.11) you can set the name ( Title) of your program. In the Delphi environment, you can additionally specify a help file ( Help file) and icon ( Icon).


Figure 7.11. Application tab in the project settings window

On the tab Compiler(Figure 7.12) compiler parameters are configured. The most interesting of them are the switches Optimization(includes optimization of the generated code) and Use Debug DCUs(allows you to debug source system libraries). Both of these switches are useful when debugging a program: the first one should be turned off, and the second one should be turned on.


Figure 7.12. Compiler tab in the Project Options window

On the tab Compiler Messages(Figure 7.13) the compiler's sensitivity to suspicious code is adjusted. Turning on the switches Show hints And Show warnings, you will receive very useful hints and warnings from the compiler, not just error messages.


Figure 7.13. Compiler Messages tab in the Project Options window

On the tab Linker(Figure 7.14) the project build parameters are configured. Owners of the Delphi environment should pay attention to the group Memory sizes, especially on two parameters: Min stack size And Max stack size. They set the minimum and maximum sizes of the application program stack, respectively. You may need to increase the values ​​of these parameters when writing an application that makes extensive use of recursive routines.


Figure 7.14. Linker tab in the Project Options window

On the tab Directories/Conditions(Figure 7.15) you can specify directories for various files. The most important of them: Output directory- the directory in which the executable file is placed; Unit output directory- directory in which intermediate object modules (DCU files) are placed; Search path- a list of directories in which software modules are searched.


Figure 7.15. Directories/Conditionals tab in the project parameters window

On the tab Version Info(Figure 7.16) information about the application version is displayed. In order for this information to be placed in the executable file, you need to enable the switch Include version information in project. The version number is specified as four numbers: Major version- the major version number of the program (it is usually increased when conceptually new features are introduced into the program); Minor version- minor version number of the program (it is usually increased with minor expansion functionality programs); Release- release number of the program that is debugged and suitable for use by the customer; Build- project build sequence number (it is automatically increased by one when compiling the project if the switch is enabled Auto-increment build number). All these parameters are for informational purposes only and do not affect the operation of the program itself. However, version information can be used by special user program installers to ensure that newer versions of libraries are not replaced by older ones.


Figure 7.16. Version Info tab in the project parameters window

On the tab Packages(Figure 7.17) you can manage the list of packages used in your project. Packages are external component libraries and are covered in Chapter 13. Note the switch Build with runtime packages, which allows you to significantly reduce the size of the executable file by using external component libraries instead of embedding their code directly into the executable file. This mode is beneficial when creating several programs based on large quantity common components.


Figure 7.17. Packages tab in the project settings window

When all project parameters are set, you can start compiling it.

7.2.5. Compiling and building the project

Compilation and assembly of the project can be performed at any stage of project development. Compilation means obtaining object modules (DCU files) from the source texts of program modules (PAS files). Assembly means obtaining an executable file from object modules. In the Delphi environment, compilation and assembly of the project are combined.

To compile, just run the menu command Project/Compile<Имя проекта> or press the key combination Ctrl+F9. This compiles all source modules whose contents have changed since the last compilation. As a result, a file with the DCU extension (short for Delphi Compiled Unit) is created for each software module. Then the Delphi environment compiles the main project file and assembles (sometimes say links) from the DCU modules an executable file whose name matches the name of the project. For the information of professionals, we note that the smart compiler of the Delphi environment throws out all unused program code from the executable file, so you do not need to worry about unnecessary objects and subroutines that may be present in connected modules.

There is a special type of compilation and assembly - a complete forced compilation of all program modules of the project for which the source texts are available, followed by assembly of the executable file. It does not matter whether changes were made to them after the previous compilation or not. Complete compilation of the project is performed using the main menu command Project/Build<Имя проекта> . As a result, an executable file is also created, but it takes a little more time.

7.2.6. Launching a finished application

When, after numerous compilations, you correct all the errors and get an executable file, you can look at the result of your dedicated work. To do this, you need to execute the created application using the menu command Run/Run or F9 keys. Before execution, the compilation process will be automatically repeated (if changes were made to the project) and after its successful completion, the application will be launched for execution. As a result, you will see its main form on the screen.

That's basically all we wanted to tell you about the project. In general, you understand what a project is and know how to manage it. It's time to tackle the components of the project and at the same time the main elements of any application in the Delphi environment - forms.

7.3. Form

7.3.1. Concept of form

From the first chapter you already have a general idea of ​​the form, now it is time to study it more closely. In fact, the form is the main component of the application, which, like smaller components, has properties. The most important form properties: title, height, width, location, background color, etc. When creating a new form, the Delphi environment itself sets the initial values ​​for the form properties, but you can change them as you see fit. This can be done at form design time (in the properties window) or at application runtime (using Delphi language statements).

The form has many properties, and at first it is easy to get confused. Practice shows that confusion arises due to the alphabetical order of properties in the window Object Inspector: properties that are similar in meaning are scattered across the list cells. To give you an idea of ​​the capabilities of a form, let's look at the main properties of a form in order of importance. For this we need a new application.

Select a command from the menu File/New/Application. The Delphi environment will automatically create a clean form in the new project and place its source text in the code editor (Figure 7.18).


Figure 7.18. The form on the screen and its description in the code editor

Save the project and its shape immediately to give them meaningful names. Select a command from the menu File/Save All and give the module a name Main.pas, and the project name FormTest.dpr. The testing ground for studying the form is ready, you can study its properties.

7.3.2. Form name and title

The main property where you start customizing the form is the property Name- Name. It contains an identifier used to access the form from the program (Figure 7.19). The first form of a new project is automatically assigned a name Form1. We recommend that you always change this so that the name of the form reflects its role in the application. For example, the main form of an application might be called MainForm(if nothing better comes to mind).


Figure 7.19. Programmatic form identifier

For the future, note that the property Name exists in any component, and it can be edited in the properties window.

Each application form should have a clear title that tells the user what its purpose is. The title is set in the property Caption. Our form is a training form, so we'll give it a title Main, indicating that this is just the main form (Figure 7.20).


Figure 7.20. Form title

7.3.3. Form style

When setting up the form, you need to take into account what kind of user interface your application will have: an MDI (Multiple Document Interface) or a regular SDI (Single Document Interface). The form property is responsible for this FormStyle

  • fsMDIChild - child window of an MDI application;
  • fsMDIForm - main window of the MDI application;
  • fsNormal - normal window (default value);
  • fsStayOnTop - a window that is always located on top of other windows on the screen.

Many applications you work with have an MDI user interface. They consist of a main window that includes one or more internal windows. Internal windows are limited to the area of ​​the main window and cannot extend beyond its boundaries. For the main form corresponding to the main window of the MDI application, the property value FormStyle must be equal to fsMDIForm. For all secondary forms corresponding to internal windows, the property value is FormStyle equals fsMDIChild. For dialog windows running in exclusive mode, the property FormStyle equal to the fsNormal value, which makes it possible to move them outside the main form.

If the program has an SDI user interface, then each form exists as a separate independent window. One of the windows is the main window, but it does not contain any other windows. In an SDI application, the property value FormStyle equals fsNormal for both the main form and secondary forms. In some cases, you can set the fsStayOnTop value to ensure that the form is always displayed on top of other forms.

Obviously, our simple ideal weight calculator is an SDI application and therefore the property FormStyle has a default value of fsNormal.

7.3.4. Dimensions and location of the form on the screen

Now let's decide on the size of the form and its location on the screen. The easiest way to set the size and position of a shape is during design using the mouse. Another way is to go to the properties window and set the dimensions of the form using the properties Width And Height, and the location using properties Left And Top(values ​​are given in pixels). The meaning of the properties is explained in Figure 7.21.


Figure 7.21. Dimensions and location of the form on the screen

In addition, using the property Position You can arrange for the form to be automatically placed on the screen by selecting one of the following possible values:

  • poDefault - the size and position of the form are selected automatically based on the screen size.
  • poDefaultPosOnly - the position of the form is selected automatically, and the width and height are determined by the property values Width And Height respectively.
  • poDefaultSizeOnly - form sizes are set automatically, and location is determined by property values Left And Top.
  • poDesigned - the dimensions and position of the form are determined by the property values Left, Top, Width, Height.
  • poDesktopCenter - the form is placed in the center of the desktop (i.e. the screen from which the taskbar is excluded). Form dimensions are determined by property values Width And Height.
  • poMainFormCenter - the form is centered relative to the main form. Form dimensions are determined by property values Width And Height.
  • poOwnerFormCenter - the form is centered relative to the owner form. Form dimensions are determined by property values Width And Height.
  • poScreenCenter - the form is placed in the center of the screen. Form dimensions are determined by property values Width And Height.

Sometimes the size of a form is calculated based on the size of its internal working area (client area), on which components are placed. As you know, the work area does not include a frame and a title. The dimensions of the work area are stored in properties ClientWidth And ClientHeight. When setting them, property values Width And Height are automatically recalculated (and vice versa).

It happens that when choosing form sizes, screen sizes are taken into account. Since modern video adapters support many modes with different resolutions, the question arises: how to ensure the same proportion between form and screen, regardless of the display resolution. For this case, the form provides a property Scaled. If set to True, the form will automatically scale based on the display resolution.

When moving around the screen, the form may stick slightly to the edges of the screen if the edges of the form are in close proximity to them. This happens if the property ScreenSnap contains the value True. The distance of the form to the edges of the screen at which the form sticks is set in the property SnapBuffer and is measured in pixels.

While working with the application, the user can collapse the form or expand it to the entire working area of ​​the screen using the corresponding frame buttons. The state of the form (collapsed or expanded) is determined by the property WindowState, which takes the following values:

  • wsNormal - the form is in normal state (neither minimized nor expanded to full screen);
  • wsMinimized - the form is minimized;
  • wsMaximized - the form is maximized to fill the entire screen.

If during design you change the value of a property WindowState to wsMinimized or wsMaximized, you will get a form that, when it first appears, will be automatically either minimized to the taskbar or expanded to full screen.

On a computer with two or more monitors, you can select the monitor for a form to display it on. To do this, set the property DefaultMonitor to one of the following values:

  • dmDesktop - the form is displayed on the current monitor; no attempt is made to fit the form on any particular monitor;
  • dmPrimary - the form is displayed on the first monitor in the list Monitors object Screen(see paragraph 7.7.2);
  • dmMainForm - the form is displayed on the monitor on which the main form is located;
  • dmActiveForm - the form is displayed on the monitor on which the currently active form is located.

Property DefaultMonitor is taken into account only if the main form exists in the program.

7.3.5. Form work area color

Everything is clear with the size of the form and now anyone can change the standard color of its background using the property Color. To do this, go to the properties window. Go to property Color, open the drop-down list and select any color from the set of basic colors. Basic colors are represented in the list by named constants. You can also choose a color from all color palette by double-clicking the property value field. A standard color selection dialog box will appear on the screen (Figure 7.22).


Figure 7.22. Standard color selection dialog box

When you set the color in the properties window, the change is immediately reflected on the form. You can work with a variety of colors, but it is considered good practice to use a standard color scheme. Therefore the best value for the property is Color- clBtnFace (the color is the same as the buttons).

7.3.6. Shape frame

In the appearance of the form, a very important role is played by the frame and the “Collapse”, “Expand”, “Close” buttons located on it (Figure 7.23). The frame style is set using the property BorderStyle, which can take the following values:

  • bsNone - the window has no frame or title at all;
  • bsDialog - a non-resizable frame typical of dialog boxes;
  • bsSingle - a non-resizable frame for a regular window;
  • bsSizeable - resizable frame for a regular window;
  • bsToolWindow - similar to the bsSingle value, but the window has a slightly smaller title, which indicates its service purpose;
  • bsSizeToolWin - similar to bsSizeable, but the window has a slightly smaller title bar, indicating its utility purpose.

Typically a property BorderStyle has the value bsSizeable. In this case, the form has a standard resizable frame (as in design), a header, a control menu, Collapse, Maximize, Close buttons and, sometimes, Help. To specify which of these elements to display, use the property BorderIcons. The list of its possible values ​​is as follows:

  • biSystemMenu - the form frame contains a control menu, which is called up by right-clicking on the form title;
  • biMinimize - the form frame has a "Minimize" button;
  • biMaximize - the form frame has a "Maximize" button;
  • biHelp - the form frame has a "Help" button. When you click the Help button, the mouse cursor turns into an arrow with a question mark. By selecting the desired form element with such a cursor, the user receives help on it in a pop-up window.


Figure 7.23. Form frame and its context menu

The control menu commands do not need comments.

7.3.7. Shape icon

If you are developing a commercial application rather than a test case, you should make sure that the form has a distinctive icon in its upper left corner. There are many tools for developing icons, which we will not dwell on. When the icon is ready and saved in a file, you just need to set it as a property value Icon(Figure 7.24).


Figure 7.24. Setting the form icon

To do this, open a window Picture Editor by pressing the button with the ellipsis (Figure 7.25). Click the button Load... and select a file from the standard icon collection (usually the C:\Program Files\Common Files\Borland Shared\Images directory). After that, close the dialog box using the button OK.


Figure 7.25. Form icon selection window

The Delphi environment will immediately insert the selected icon into the upper left corner of the form (Figure 7.26).


Figure 7.26. New form icon

7.3.8. Invisible form

The problem solving scenario may require that at some point the form becomes invisible, i.e. disappeared from the screen. The Boolean property is responsible for the “visibility” of the form Visible. Setting it to False will hide the form, while setting it to True will show it.

7.3.9. Transparent form

Some parts of the form can be made transparent (Figure 7.27). Moreover, clicking the mouse on the transparent area of ​​the form will lead to the activation of windows (forms) located behind the form. This is achieved by setting the property TransparentColor to True and selecting the transparency color in the property TransparentColorValue. All pixels of shape with color TransparentColorValue will be transparent.


Figure 7.27. Transparent form

For example, Figure 7.27 was obtained as follows. We put a component on the form Shape, turned it into an ellipse ( Shape= stEllipse), stretched to the size of the form ( Align= alClient), set the property in the form TransparentColor to True and equalized the property value in the form TransparentColorValue with property Brush.Color component Shape. After assembling and launching the program, we received a “leaky” form.

7.3.10. Translucent form

The form can be translucent (Figure 7.28). The properties responsible for the translucency of the form are: AlphaBlend And AlphaBlendValue. The first property turns the translucency effect on and off, and the second determines the strength of the transparency.


Figure 7.28. Translucent form

For example, Figure 7.28 was obtained as follows. We put a component on the form Image, loaded a picture into it (property Picture), then set the property in the form AlphaBlend to the value True and the property AlphaBlendValue- to a value of 150. After assembling and launching the program, we received a translucent effect.

7.3.11. Inaccessible form

Sometimes you just need to deny access to a form without removing it from the screen. Another Boolean property does this: Enabled. This is usually set to True, but set it to the opposite value and you won't be able to make the form active once the application starts.

As you understand, all the properties described above are available not only in the properties window, but also in the code editor, i.e. in the program text. When you work with a form at the source level, you also have access to some additional properties that are not visible in the properties window. They will be considered as needed.

7.3.12. Form Events

So, we’ve figured out the properties and it’s time to say a few words about the events that arise when working with the form. With your permission, we will omit those form events that occur in all components visible on the screen (we will talk about them later, when we look at components). We list only the characteristic events of the forms:

  • OnCreate- occurs immediately after the form is created. The handler of this event can set initial values ​​for the properties of the form and its components, request the necessary resources from the operating system, create service objects, and also perform other actions before the user begins working with the form. Paired to the OnCreate event is the OnDestroy event.
  • OnDestroy- occurs immediately before the form is destroyed. The handler for this event can release resources, destroy service objects, and perform other actions before the form object is destroyed.
  • OnShow- occurs immediately before the form is displayed on the screen. The counterpart to the OnShow event is the OnHide event.
  • OnHide- occurs immediately before the form disappears from the screen. The OnHide event is paired with the OnShow event.
  • OnActivate- occurs when the user switches to the form, i.e. the form becomes active. The counterpart to the OnActivate event is the OnDeactivate event.
  • OnDeactivate- occurs when the user switches to another form, i.e. the current form becomes inactive. The counterpart to the OnDeactivate event is OnActivate.
  • OnCloseQuery- occurs when trying to close the form. A request to close a form can come from a user who clicked the “Close” button on the form frame, or from a program that called the Close method on the form. The OnCloseQuery event handler is passed by reference a Boolean parameter CanClose that allows or disables the actual closing of the form.
  • OnClose- occurs after the OnCloseQuery event, just before the form is closed.
  • OnContextPopup- occurs when calling the form's context menu.
  • OnMouseDown- Occurs when the user presses a mouse button while the mouse pointer is hovering over the form. After releasing the mouse button, an event occurs in the component OnMouseUp. When you move the mouse pointer over a form, an event occurs periodically OnMouseMove
  • OnMouseWheelUp- occurs when the mouse wheel rotates forward (away from you).
  • OnMouseWheelDown- occurs when the mouse wheel rotates backwards (towards itself).
  • OnMouseWheel- occurs when the mouse wheel is rotated in either direction.
  • OnStartDock
  • OnGetSiteInfo- Occurs when a docking component requests a docking location.
  • OnDockOver- Occurs periodically when the mating component is towed over the mold.
  • OnDockDrop- occurs when a component is mated (see Chapter 10).
  • OnEndDock
  • OnUnDock- occurs when the user tries to undock a component.
  • OnDragDrop- Occurs when the user drops a towable object into the mold.
  • OnDragOver- occurs periodically when towing an object over a form.
  • OnCanResize- occurs when trying to resize a form. A request to resize can come from the user. The OnCanResize event handler is passed a Boolean parameter by reference Resize, which allows or disables actual resizing of the form.
  • OnResize- occurs when the size of the form changes.
  • OnConstrainedResize- occurs when changing the size of the form and allows you to change the minimum and maximum dimensions forms.
  • OnShortCut- occurs when the user presses a key on the keyboard (before the event OnKeyDown, see paragraph 7.5.5). Allows you to intercept keystrokes before they reach the standard form handler.

7.4. Several forms in the application

Often one form is not enough to solve a problem. Therefore, now we will look at how to add a new form to the project, select the main form of the application, and switch between forms. Then we will tell you how the application stage solves the issue of displaying forms on the screen.

7.4.1. Adding a new form to a project

Adding a new form to a project is extremely simple: select the menu command File/New/Form and a second form will immediately appear on the screen. In this case, a program module corresponding to the new form will automatically appear in the code editor window. Give the newly created form a name SecondaryForm(property Name) and title Secondary(property Caption) - Figure 7.29.


Figure 7.29. Two forms in the project

Save the module with a new form under the name Second.pas- We still need the form.

7.4.2. Adding a new form from the Object Storage

There is a second, more productive way to create forms. It is based on the use of ready-made forms that exist in the Delphi Object Repository. Object Storage ( Object Repository) contains templates of forms, program modules and entire projects that you can either simply copy into your project, or inherit, or even use directly. To take a new form from the Object Store, select the command from the menu File / New / Other.... The Delphi environment will open the window shown in Figure 7.30:


Figure 7.30. Window for creating a new form or other project element

If on the tab New dialog box select icon with caption Form, then a regular empty form will be added to the project, as if by a menu command File / New Form. If you are interested in forms with “filling”, please refer to the tabs Forms And Dialogs(Figure 7.31).


Figure 7.31. Quickly create a form with “filling”

On tabs Forms And Dialogs there is a switch indicating what needs to be done with the blank form: copy ( Copy), inherit ( Inherit) or use ( Use). The difference between them is as follows:

  • Copy- means that a complete copy of the blank form is placed in the project.
  • Inherit- means that the form added to the project is created by inheriting from a blank form located in the Object Storage;
  • Use- means that the blank form itself is added to the project; changing a form in a project means changing a blank form in the Object Storage.

Which mode to use depends on the conditions of the problem. Mode Copy It’s good simply because it doesn’t start developing a new form from scratch. Mode Inherit useful when there are several forms in a project that have the same parts. In this case, all forms that are similar to each other are generated from a single form that realizes a part common to all heirs. Mode Use allows you to adjust the blank form directly in the Object Storage.

For our tutorial example, two forms are enough, so let's return to the already created forms by clicking the Cancel button.

7.4.3. Switch between shapes during design

Sometimes the shapes become difficult to follow. The window helps to put things in order View Form, which can be called using the menu command View/Forms...(Figure 7.32).


Figure 7.32. Window for switching to another form

Select the form you are going to work with in this window and click on the button OK. The selected form will immediately become active.

7.4.4. Selecting the main form of the application

When there are several forms in a project, the question arises: which one is the main one. Let's not rack our brains, but turn to the dialog box you know Project Options and look at the tab Forms which form is selected in the dropdown list Main form(Figure 7.33).


Figure 7.33. The main form in the project is written in the Main form field

You will find that the form is selected MainForm, which was added to the project first (the Delphi environment creates it automatically when creating a new project). You can select another form - and then it will be displayed when the application starts. In this case, this is not necessary, since the main form is already installed correctly.

7.4.5. Calling a form from a program

When working with multiple forms, you must take into account that once the application is loaded, only the main form is displayed. The remaining forms, although they are created automatically after it, are not immediately shown on the screen, but wait until they are called. The form can be called to work in two different ways:

  • call for work in normal mode using the method Show. In this mode, the user can work simultaneously with several forms, switching between them;
  • call to work in exclusive mode using the method ShowModal. In this mode, the user cannot switch to another form until he finishes working with this form;

Let's show how these methods are implemented using the example of a form call SecondaryForm from the form MainForm.

To form SecondaryForm was available for use form MainForm, you need to connect the form module SecondaryForm to the form module MainForm. This is done very simply.

1. Activate the form MainForm and select the command from the main menu File/Use Unit.... In the dialog box that appears, select the module Second(this is the name of the form module SecondaryForm) and click OK (Figure 7.34).


Figure 7.34. Window for selecting a plug-in module

There will be no visible changes on the screen, but in the section implementation the Main program module will add the line

uses Second;

Now let's call the form SecondaryForm from the form MainForm. In most cases, forms are called when some button is clicked. Let's add this button to the form MainForm.

2. Find in the component palette on the tab Standard hint icon Button, click on it and then click on the form MainForm. A button will be created on the form Button1 and the properties window will display a list of its properties. Go to property Caption and replace the text Button1 to text Secondary(Figure 7.35).


Figure 7.35. The text on the button is written in the Caption property

To display a form when a button is clicked SecondaryForm, you need to define an event handler for this button OnClick. This is done very simply.

3. Activate the tab in the properties window Events and double click on the event field OnClick. The Delphi environment will define an event handler for the button by placing a program template in the source code of the module Main. Insert the operator into the body of the handler SecondaryForm.Show, as a result, the event processing method will take the following form:

procedure TMainForm.Button1Click(Sender: TObject); begin SecondaryForm.Show; end;

4. Compile and run the application. When the form appears on the screen MainForm, press the button Secondary. Another form will appear on the screen - SecondaryForm. You can arbitrarily activate any of the two forms (Figure 7.36).


Figure 7.36. Application with two forms on the screen

Thus, when using the method Show the user can work with several forms simultaneously, switching between them.

Attention! Switching between forms MainForm And SecondaryForm, you can assume that they are equal. However, in reality this is not the case. Form MainForm is the main one, and the form SecondaryForm- secondary. How is it shown? Yes, at least in that if you close the form MainForm, then the form SecondaryForm will also close and the application will end. If you close the form SecondaryForm, then the form MainForm will remain on the screen.

A situation where the user is offered several available forms to work with at once is rare. Therefore, to display the form, the method is mainly used ShowModal. It allows the form to run in exclusive mode, not returning control until the user closes the form.

5. Let's see what happens if we replace the method call in the previous example Show on ShowModal.

procedure TMainForm.Button1Click(Sender: TObject); begin SecondaryForm.ShowModal; end;

6. After compiling and running the application, click on the form MainForm button Secondary. After the form appears SecondaryForm try activating the form MainForm. You won't succeed because this time the form SecondaryForm displayed in exclusive mode (Figure 7.37).


Figure 7.37. The second form works in exclusive mode

Just closing the form SecondaryForm, you will be returned to the form MainForm. Now the purpose of the method is clear ShowModal. With its help, a step-by-step (dialogue) mode of interaction with the user is organized, which is discussed in detail in Chapter 9.

With this we will finish talking about forms and move on to their contents - components.

7.5. Components

7.5.1. Component concept

The concept of a component is fundamental to the Delphi environment. Without components, all the benefits of visual application development disappear and there is nothing to talk about. Therefore, gather all your strength and carefully read this paragraph, trying to understand not only the technique of using the components, but also their very essence.

There are two ways of looking at components.

  • A look from the outside, or more precisely from the visual application development environment. In this view, components are self-contained building blocks that you take from the component palette and transfer to a form to create the actual application. You know examples of components: buttons, lists, labels, etc.
  • There is also a view from the inside, i.e. view from a Delphi program. From this point of view, components are classes derived directly or indirectly from a class TComponent and having published-properties. Component instances are objects of these classes that exist as form fields. Among the published properties of components there must be a name ( Name), under which the component instance is presented in the properties window.

Combining these two perspectives gives a holistic view of what components are. When working with components from a visual application development environment, you always see their front side. However, once you start writing event handlers and managing components programmatically, you come into contact with the programming side of components, the essence of which is objects. Thus, the Delphi environment provides a symbiosis of visual and object-oriented programming.

When analyzing the structure of the component, it is discovered that its nature is ternary and is best described by the formula:

Component = state (properties) + behavior (methods) + feedback (events).

  • The state of a component is determined by its properties. Properties can be mutable (read-write) or immutable (read-only). In addition, properties can receive values ​​either at the design stage (design-time) or only during program execution (run-time). The first ones are set for each component in the properties window and determine the initial state of the component. While the application is running, these properties can be changed programmatically, changing the appearance and behavior of the component accordingly. The second group is properties that are not visible in the properties window and can only be controlled programmatically. From the point of view of the Delphi language, the difference between these groups of properties is that the first ones are declared in the section published, and the second - in the section public.
  • The behavior of a component is described using its procedures and functions (methods). Calls to component methods are placed in the program's source code and occur only during application execution. The methods have no visual basis.
  • The feedback of a component is its events. Events allow you, for example, to associate a button click with a form method call. Events are implemented using properties containing pointers to methods (see Chapter 3).

7.5.2. Visual and non-visual components

All components are divided into two groups: visual and non-visual components (Figure 7.38).

  • Visual components are visible elements of the user interface: buttons, labels, list boxes, etc. They look the same both at the design stage and while the application is running.
  • Non-visual components are, so to speak, the soldiers of the invisible front; They work, but they themselves are not visible on the screen. Non-visual components include a timer, database access components, etc. During the design process, such components are represented on the form with a small icon. Their properties are set in the properties window you already know. Some components, although non-visual, can display something on the screen. For example, a non-visual component MainMenu displays the main menu bar on the form, and the component OpenDialog- standard file selection dialog box.


Figure 7.38. Visual and non-visual components

Non-visual components may have captions (Figure 7.38). Display of signatures is ensured by setting the switch Show component captions in the window Environment Options on the tab Designer. The window is called up by menu command Tools/Environment Options…

7.5.3. "Window" and "graphics" components

Visual components are divided into components drawn by the Windows windowing system and components drawn by the VCL graphics library (Figure 7.39). In programming jargon, the former are called "window" components, and the latter are called "graphics" components.

  • Windowed controls are specialized windows within a form window. Their most important quality is the ability to receive input focus. Window components include, for example, components Button, RadioButton, CheckBox, GroupBox, etc. Some window components ( GroupBox, TabControl, PageControl) are capable of containing other visual components and are called container controls. The display of window components is provided by the Windows operating system. For those professionals who have dealt with the Windows API, note that window components have the property Handle. It associates a Delphi environment component with a corresponding operating system object.
  • Graphical controls are not windows, so they cannot receive input focus or contain other visual components. Graphics components are not based on Windows operating system objects; their display is handled entirely by the VCL library. Graphic components include, for example, components SpeedButton, Image, Bevel etc.


Figure 7.39. Components drawn by the Windows windowing system and the Delphi graphics library

The general classification of components has been compiled, so let's move on to discussing their properties and events. Obviously, each component has a specific set of properties and events and, it would seem, they should be studied in the context of studying the component. This is what we will do in the future when considering the distinctive properties of components. However, for now it makes sense to look at the properties and events that are common to most components.

Non-visual components have virtually no properties or events in common; the only properties they have in common are Name(no comments required) and Tag(an integer value that does not carry any semantic meaning - you can use it at your discretion). But visual components have many common properties and events, which we will now consider.

7.5.4. General Properties of Visual Components

Visual components have a number of common properties:

  • Left And Top- the location of the visual component inside the form (or inside the owner component).
  • Width And Height- horizontal and vertical dimensions of the component, respectively.
  • Anchors- allows you to bind the boundaries of the component to the boundaries of the form. The anchored border of a component will follow the corresponding border of the form as the form is resized. Experiment with the values ​​of this property and you will quickly understand the logic of how it works.
  • BiDiMode- allows you to make the text read from right to left (used when working with oriental languages). The component either uses its own property value or copies it from the owning component if the auxiliary property ParentBiDiMode equals True.
  • Caption- component label. The text string set in the property can contain the special character ‘&’ (ampersand). If an ampersand appears in a line, the character following it is displayed underlined (the ampersand is not displayed). Pressing the corresponding symbol key on the keyboard in combination with Alt key activates the component.
  • Constraints- restrictions on the size of the component. Nested properties MinWidth And MinHeight define the minimum width and height, and the nested properties MaxWidth And MaxHeight- maximum width and height, respectively.
  • Color- component color. The component either uses its own color or copies the color of the containing component. This is determined by the property value ParentColor. If the ParentColor property is True, then changing the color of the containing component (for example, a form) automatically causes the color of the nested component (for example, a button) to change. However, if you change the value of the Color property, the ParentColor property will automatically be set to False and the component will receive its own color.
  • Cursor- determines what form the mouse pointer takes when the user hovers it over a component. Each pointer option has its own integer constant (for example, the constant crArrow corresponds to a regular arrow pointer). Full list values ​​with descriptions can be found in help system Delphi environment.
  • DragCursor- the view of the mouse pointer when the user drags an object over a component. This type of cursor is only set if the object can be accepted (see Chapter 10).
  • DragKind- defines the behavior of the component when towing: just towing ( dkDrag) or docking ( dkDock). Depending on the value of this property, one or another chain of events occurs: a chain of towing events or a chain of docking events.
  • DragMode- determines the mode of towing the component across the screen. If the property is set to dmManual (the default), then towing must be initiated programmatically. If the property is set to dmAutomatic, then the component is already ready for towing; the user just needs to hover the mouse pointer over the component, press the mouse button and, while holding it, tow the component to the desired location.
  • Enabled- determines whether the component is available to the user. If the property is True, then the component is available, and if the property is False, then it is not available. An inaccessible component usually has a faded appearance.
  • Font- font of the inscription on the component. Font parameters are set using nested properties CharSet, Color, Name, Size, Style, Height, Pitch, Weight. The component either uses its own font or copies the font of the containing component. This is determined by the property value ParentFont. If the ParentFont property is True, then changing the font of the containing component (for example, a form) automatically changes the font of the nested component (for example, a button). However, if you change the value of the Font property, the ParentFont property will automatically be set to False and the component will receive its own font for the label.
  • HelpType- defines how the help file will be searched for a topic that matches a component. When a component has input focus, the user can press F1 to get online help. The search for the corresponding topic is carried out either by the number specified in the property HelpContext, or by keyword specified in the property HelpKeyword. In the first case, the HelpType property must have the value htContext, and in the second - htKeyword.
  • HelpContext- contains the number of the corresponding topic in the help file. Used when the property HelpType has the meaning htContext. If the HelpContext property has a value of 0, then the topic number is taken from a similar property of the owning component (usually a form).
  • HelpKeyword- contains a keyword to search for a topic in the help file. Used when the property HelpType has the meaning htKeyword. If the HelpKeyword property has an empty value, then the search is performed using a keyword that is taken from a similar property of the owning component (usually a form).
  • Hint- a tooltip that appears over a component when the user temporarily hovers the mouse pointer over it. The appearance of a tooltip can be enabled or disabled using the property ShowHint. The ShowHint property value can be copied from the containing component depending on the property value ParentShowHint. If the ParentShowHint property is set to True, then disabling the tooltip for the containing component (for example, a form) automatically disables the tooltip for the nested component (for example, a button). However, if you change the value of the ShowHint property, the ParentShowHint property will automatically be set to False, and control of whether the tooltip will be disabled will pass to the component.
  • PopupMenu- used to bind the context menu to the component. This menu is accessed by right-clicking on a component. Menus are discussed in detail in Chapter 8.
  • TabOrder- contains the sequence number of the component within its owning component. This is the queue number in which the component receives input focus when the Tab key is pressed on the keyboard. The TabOrder property is only present on windowed components.
  • TabStop- determines whether the component can receive input focus. If the property is True, then the component is in the input focus queue, and if the property is False, then it is not. The TabStop property is only present in windowed components.
  • Visible- determines the visibility of the component on the screen. If the property is True, then the component is visible, and if the property is False, then it is not visible.

7.5.5. Common Visual Component Events

Visual components have a number of common events:

  • OnClick- occurs as a result of a mouse click on a component.
  • OnContextPopup- occurs when calling the component's context menu.
  • OnDblClick- occurs as a result double click mouse by component.
  • OnEnter- occurs when the component receives input focus. When a component loses input focus, the event occurs OnExit. The OnEnter and OnExit events do not occur when switching between forms and applications.
  • OnKeyDown- occurs when the user presses any key (if the component has input focus). When the pressed key is released, the event occurs OnKeyUp. If the user presses a symbol key, then the event occurs after the OnKeyDown event and before the OnKeyUp event OnKeyPress. Keypress events typically go to the active component that has the input focus. However, using the form property KeyPreview You can make the form intercept keyboard events before the active component receives them. To do this, the KeyPreview property is set to True.
  • OnMouseDown- Occurs when the user presses a mouse button while the mouse pointer is hovering over a component. After releasing the mouse button, an event occurs in the component OnMouseUp. When you move the mouse pointer over a component, an event periodically occurs in the latter OnMouseMove, which allows you to track the position of the pointer.

To organize towing and docking, in visual components There are several more events:

  • OnStartDrag- occurs when the user starts towing something.
  • OnDragOver- occurs periodically when the user drags something over a component.
  • OnDragDrop- Occurs when the user drops a towed object onto a component.
  • OnEndDrag- occurs upon completion of towing of the object.
  • OnStartDock- Occurs when the user begins to tow the docking component.
  • OnEndDock- occurs upon completion of the component docking.

Towing and docking events are discussed in detail in Chapter 10.

7.6. Component management during design

7.6.1. Placing components on a form and removing them

To place the desired component from the component palette on the form, run the following actions:

  1. Hover your mouse over the icon of the desired component in the palette and left-click.
  2. Hover your mouse over the desired location on the form and left-click again.

The selected component will appear on the form and will be ready to be configured in the properties window.

You often want to place multiple components of the same type on a form, such as buttons. In this case, proceed as follows:

  1. Hover your mouse over the icon of the desired component in the palette. Press and hold the Shift key and left-click. Release the Shift key.
  2. Hover your mouse over the place on the form where the first component will be located and left-click.
  3. Hover your mouse over the place on the form where the next component will be placed and left-click again. Repeat this step as many times as you need components;
  4. Once you've placed the last component, hover your mouse over the arrow button (located on the left side of the Components palette) and left-click. This will be a signal that the placement of similar components is complete.

If for some reason you decide to remove an extra component from the form, simply select it with the mouse and press the Del key.

7.6.2. Selecting components on a form

At the design stage, any component can be highlighted on the form. The properties of the selected component are visible in the properties window and are available for editing. To select a component, simply hover over it and click the mouse button. So-called “sizing handles” will immediately appear around the component to resize the component in width and height (Figure 7.40).


Figure 7.40. Component stretch points

When designing complex forms, you will encounter a situation where you need to set some property to the same value in several components at once. For example, in several buttons set the property Enabled to False. The fastest way to do this is to select several components, then go to the properties window and change the desired property. When several components are selected on a form, only their common properties are visible in the properties window.

There are two ways to select multiple components:

  • Press the Shift key and, while holding it, mark all the required components with mouse clicks, then release the Shift key. Small square markers will appear in the corners of each selected component.
  • Press the mouse button when the cursor is outside the components. Then, while holding the button pressed, drag the cursor over the selected components, including them in the dotted rectangle. When all the required components fall into the dotted rectangle, release the mouse button. (If the selected components are inside a Panel or GroupBox component, then this operation must be performed with the Ctrl key pressed.) As a result of the above actions, small square markers will appear in the corners of all components that are at least partially within the dotted rectangle, indicating that components are highlighted.

You can combine both methods to highlight only the components you need.

When several components are selected on a form, the properties window displays only their general properties. Activate the desired property and set it to the desired value. You will see that this setting will be reflected in all selected components (Figure 7.41).


Figure 7.41. Setting a property for a group of components

When several components are selected on a form, some properties may seem to have no meaning in the window Object Inspector(the value field is empty). This suggests that the properties have different meanings in the selected components.

7.6.3. Move and resize a component

Once all the components are placed on the mold, you need to evaluate it from an aesthetic point of view. Step away from the computer and look at the form from the outside. As a rule, there will be some flaws in the landscape. Surely you will want to reduce something, increase something, or move something to another place. Doing all this is as easy as shelling pears.

To move a component to another location:

  1. Place the cursor over the component you want to move and click (the component will immediately come into focus). Without releasing the mouse button, drag the component to a new location.
  2. When the component is where you want it, release the mouse button.

Resizing a component is also easy:

  1. Click to activate the component you want to resize (it will immediately be in focus);
  2. Hover your mouse over a vertical or horizontal stretch point and the pointer will change to a double-headed arrow. Click and hold the mouse button and move the pointer to decrease or increase the size of the component;
  3. Having achieved the desired size, release the mouse button and move the pointer away from the stretch point (the pointer will return to normal). The component with the new dimensions is ready for use.

To make it easier for you to position and resize components, the form displays a grid during design. Components automatically align at the intersection of imaginary grid lines when you drag them from the Components palette onto the form. Initially, the spacing between the horizontal and vertical grid lines is 8, but this can be easily changed. To do this, execute the menu command Tools/Environment Options.... A dialog box will appear on the screen Environment Options. Find the fields Grid size X And Grid size Y on the tab Designer and set the grid parameters that you need (Figure 7.42). By the way, you can use the adjacent switches to tell the Delphi environment whether to show the grid at all ( Display grid) and whether components should be aligned to it ( Snap to grid).


Figure 7.42. Distance between grid nodes horizontally and vertically

Sometimes, after roughly placing a component on the mesh, it is necessary to adjust its position and dimensions to an accurate point on the screen. This does not require turning off the grid or changing its pitch. Simply select the component on the form and proceed as follows:

  • Press and hold the Ctrl key and use the arrow keys to move the component the desired number of points on the screen. Release the Ctrl key.
  • Press and hold the Shift key and use the arrow keys to stretch or shrink the component as many points on the screen as you want. Release the Shift key.

7.6.4. Aligning components on a form

When there are many components on a form, manual alignment becomes a very tedious task. For this case, the Delphi environment provides tools for automated alignment of components. The alignment algorithm is as follows:

1. Select the components you are going to align. Small square markers will appear in all four corners of each selected component;

2. Go to the main menu and call up the window Alignment(Figure 7.43) using the menu command Edit/Align... .


Figure 7.43. Dialog box for choosing how to align a group of components on a form

3. Select what you need from the list and click the button OK. The window will close and all components will be aligned according to your instructions.

If there are a lot of components on the form and you have a lot of work to do to align them, open the window Align(using the menu command View/Alignment Palette) and use it in the second step of the above algorithm (Figure 7.44).


Figure 7.44. An auxiliary button bar for aligning a group of components on a form

7.6.5. Using the Clipboard

When working with multiple forms, sometimes the task of copying and moving components from one form to another arises. Regular tugging doesn't help here. The problem is easily solved using the Clipboard:

  1. Using well-known techniques, select the components on the first form that you want to copy or move to the second form.
  2. If you are going to copy components, select the command from the menu Edit/Copy. If you are going to move components, select the command from the menu Edit/Cut. The components will appear in the Clipboard.
  3. Activate the second form and select the command from the menu Edit / Paste. With this command, the Delphi environment will extract components from the Clipboard and place them on the active form.

Let's add that the Clipboard commands are used not only to copy and move components from one form to another, but also to copy and move components within the same form between different owning components, for example, to move buttons from one panel to another. The need to use the Clipboard in this case is due to the fact that the components firmly know their owner (for example, the buttons know the panel on which they are located), so the usual dragging operation does not lead to anything.

So, you already know a lot about components, and there is no point in going deeper into them. Starting from the next chapter, we will begin to introduce you to the elements of the user interface: menus, toolbars, status bars, dialog boxes, etc. - that's where we'll talk about the details. Now let’s say a few words about those objects that work hard “behind the scenes” of the application and provide it with access to various computer resources, such as the screen, printer, Clipboard, etc.

7.7. Behind-the-scenes application objects

7.7.1. Application - the main object that controls the application

What we talked about above is the external side of the application. What's going on inside? This is how it is. Above all forms and components there is an object Application(class TApplication), representing the application as a whole. This is the main "actor" that is created at the beginning of the execution of any application. An object Application holds all the threads of control in his hands: creates the main and secondary forms, destroys them, serves exceptional situations. By the way, you have already encountered it in the project file:

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

An object Application is not in the component palette, so its properties can only be changed from within the program. Let's briefly look at the most important properties of this object:

  • Active- equals True if the application is active.
  • AutoDragDocking- mode of automatic or manual joining of forms and components. IN automatic mode(value True) docking occurs after towing is completed when the mouse button is released. IN manual mode(value False) To dock, you must hold down the Ctrl key while releasing the mouse button.
  • BiDiKeyboard- keyboard layout when working with oriental languages.
  • BiDiMode- allows you to make the inscriptions read from right to left (used when working with oriental languages).
  • CurrentHelpFile- name of the help file of the active form of the program (each form can have its own help file). If the active form does not have its own help file, then in the property CurrentHelpFile the property value is simply duplicated HelpFile.
  • HintColor- background color of the tooltip.
  • HintHidePause- the time during which the tooltip remains on the screen.
  • HintPause- delay before the tooltip appears.
  • HintShortCuts- determines whether the name of the key combination is included in the tooltip text.
  • HintShortPause- the time after which a tooltip appears if another tooltip is currently displayed on the screen.
  • MainForm- indicates the main form of the application. By default, the first form created is considered the main one.
  • NonBiDiKeyboard- keyboard layout.
  • ExeName- contains full name(including the route) of the program's executable file. The name of the executable file is the same as the name of the main project file. If a project name was not specified, the executable file is named Project1 by default.
  • Title- contains the name of the application, which is displayed on the Taskbar during operation. By default, the property value is the name of the main project file.
  • HelpFile- contains the name of the reference file, which is used by the program to display online help information on forms and components.
  • HelpSystem- interface to the help system.
  • Icon- contains an icon displayed on the Taskbar while the program is running. The icon appears to the left of the name (see Title).
  • UpdateFormatSettings- includes automatic update format strings in the program following changes in these parameters in the operating system. Format strings control the display of date, time, currency, etc.
  • UpdateMetricSettings- enables automatic updating of the font and background of system inscriptions (tooltips and icon labels) when changing screen settings in the operating system.
  • Terminated- True value indicates that the program is in the process of ending.

If you want to set the title (property Title), icon(property Icon) and directory file name (property HelpFile) for the application, do not edit the main program file, but rather refer to the dialog box Project Options(Figure 7.45), which appears by menu command Project/Options... .


Figure 7.45. Project Options Window

An object Application has several useful events. The most important of them: OnActivate, OnDeactivate, OnException.

  • OnActionExecute- occurs when executing any command in the component ActionList(see chapter 10).
  • OnActionUpdate- occurs when the program is idle to update the state of commands in the component ActionList(see chapter 10).
  • OnActivate- occurs when the program receives input focus, i.e. when the user switches to it from another program.
  • OnDeactivate- occurs when the program loses input focus, i.e. when the user switches from it to another program.
  • OnException- Occurs when an unhandled exception occurs in a program. The standard handler for this event calls the method ShowException to display a message box explaining the reason for the error. You can change the response to the OnException event by rewriting its handler.
  • OnHelp- occurs when the user calls help.
  • OnHint- occurs when the mouse cursor hovers over a component containing a tooltip.
  • OnIdle- Occurs periodically when the program is idle.
  • OnMessage- occurs when the program receives a message from the Windows operating system.
  • OnMinimize- occurs when the user minimizes the program.
  • OnModalBegin- occurs when an exclusive form is displayed on the screen.
  • OnModalEnd- occurs when closing an exclusive form.
  • OnRestore- occurs when the user restores a minimized program.
  • OnSettingChange- Occurs when operating system settings are changed, such as display settings or regional settings.
  • OnShortCut- occurs when a key is pressed on the keyboard (even before the event occurs in the form OnKeyDown).
  • OnShowHint- occurs immediately before any tooltip appears.

Of all the object's methods Application we will mention only one - Terminate. He performs normal completion applications. Remember, the method Terminate does not cause immediate termination of the application, allowing all forms to close themselves gracefully. When closing forms, the property Terminated is True.

If necessary, assist the facility Application less significant "persons" are in a hurry: objects Screen, Printer And Clipboard. They are also global and are created automatically when the application starts (if, of course, standard modules are connected where they are located).

7.7.2. Screen - object that controls the screen

Every program displays something on the screen, otherwise it is simply useless. In Delphi the screen is treated as a global object Screen class TScreen, which has a set of properties. Many of them are strictly related to the physical characteristics of the screen (hardware), and therefore in most cases are not available for recording. Let us indicate the most important properties:

  • Width And Height- screen width and height in pixels.
  • ActiveForm- active form (the one that is currently in input focus).
  • ActiveControl- indicates the component that has input focus in the active form.
  • Cursor- manages appearance mouse pointer for all application forms.
  • Cursors- list of available mouse pointers.
  • DataModuleCount- number of data modules created by the application. A data module is something like an invisible form in which only non-visual components can be placed. Moving non-visual components from the form to the data module can, in some cases, improve the structuring of the program by separating the subject program logic from the user interface program logic.
  • DataModules- a list of all data modules created by the application.
  • DesktopWidth And DesktopHeight- width and height of the virtual screen (used when several monitors are connected to the computer).
  • DesktopLeft And DesktopTop- position of the virtual screen on the monitor screen.
  • DesktopRect- coordinates of the virtual screen.
  • Fonts- list of all supported fonts.
  • FormCount- number of forms created by the application.
  • Forms- a list of all forms created by the application.
  • HintFont- tooltip font.
  • IconFont- font for icon captions.
  • MenuFont- text font in the menu.
  • MonitorCount- number of monitors connected to the computer.
  • Monitors- a list of all monitors connected to the computer.
  • PixelsPerInch- the number of pixels in one inch of the monitor screen.
  • WorkAreaWidth And WorkAreaHeight- width and height of the screen working area (does not include the taskbar). If several monitors are connected to the computer, then the width and height of the work area on the main monitor is calculated.
  • WorkAreaLeft And WorkAreaTop- position of the working area on the monitor screen.
  • WorkAreaRect- dimensions and position of the working area on the monitor screen.

As an example of using the object Screen Here is a fragment that sets the mouse pointer to look like an hourglass while performing some lengthy operation:

7.7.3. Mouse - an object representing a mouse

It is hardly possible to find computers today without a miniature “tailed” device called a mouse. To work with it in the Delphi environment there is a special object Mouse: TMouse, automatically added to the program when the module is connected Controls. We list the most important properties of this object:

  • Capture- contains a handle to the window that has captured the mouse for exclusive use (this is an object of the Windows operating system).
  • CursorPos- position of the mouse pointer.
  • DragImmediate- determines when towing starts: True value - immediately, False value - after the mouse pointer moves to DragThreshold positions while holding down the mouse button.
  • DragThreshold- the number of pixels by which the pointer must be moved while the mouse button is pressed for towing to begin.
  • IsDragging- checks whether the towing process is currently in progress.
  • MousePresent- checks whether the mouse is connected to the computer.
  • WheelPresent- checks whether the mouse has a wheel.
  • WheelScrollLines- the number of logical lines by which the page is shifted when the mouse wheel is scrolled one step.

7.7.4. Printer - an object that controls the printer

Most programs output some text or graphics to the printing device. For this useful task, there is a special object in the Delphi environment Printer. It becomes available after connecting the Printers module. If you include this module in your project, an object will be generated immediately after start Printer class TPrinter. Its properties and methods give you very good opportunities for printing from the application to all types of printers. However, this topic deserves a separate chapter (see Chapter 10).

7.7.5. Clipboard - object that manages the Clipboard

Anyone who has worked with text knows what a great thing it is - the Clipboard. Let's remember that this is a buffer where you can put something (for example, text or a drawing), and then take it from there. A global object is responsible for operations with the Clipboard in the Delphi environment. Clipboard class TClipboard. It is located in the Clipbrd module. About how an object Clipboard used practically, described in detail in Chap. 8.

7.8. Results

Time well spent! You have learned everything about the project:

  • what it is and what parts it consists of (form description files, program module files, main project file, etc.);
  • how to open, save, run and manage a project using the window Project Manager;
  • what a form is, how to change its style, size, location, color, how to switch from the main form to a secondary form and vice versa, etc.
  • what a component is, where to get it from, where to place it, how to bring order to a group of components;
  • who manages the application from the inside (Application object) and who helps him in this (Screen, Printer, Clipboard objects).

Yes, it was a little difficult to understand all this, but it was necessary. It's hard to learn, but it's easy to fight. Having consoled ourselves with this truth, let's move on to studying the most important elements of the user interface - the menu, toolbar and status bar.

Compiling and Running a Delphi Project

The process of compiling a project creates a ready-to-use file, which can be an application (EXE) or a dynamically loaded library (DLL). In what follows, we will consider only the application file. The name of the application resulting from compilation is the same as the name of the project file, and the application itself is self-contained and does not require additional files to operate Delphi.

The compilation process is started using the command Project | Compile (Project | Compile<проект1>) or by pressing a key combination + . This command contains the name of the project currently under development, initially Projectl. When saving a project under a different name, the project name in the menu command should change accordingly.

Compiling a project to produce an application can be done at any stage of project development. This is convenient for checking the appearance and correct functioning of individual form components, as well as for checking individual fragments of the generated code.

When compiling a project, the following actions are performed:

1) files of all modules are compiled, the contents of which have changed since the last compilation. As a result, for each file with the source text of the module, a file with the extension is created DCU. If for some reason the source code of a module is not available to the compiler, it is not recompiled;

2) if changes have been made to a module, then not only this module is recompiled, but also those using it using the directive uses modules;

3) module recompilation also occurs when the object file or included file changes ( INC) used by this module;

4) after compiling all the project modules, the project file is compiled and an executable application file is created with the name of the project file.

In addition to compilation, the project can be built. When building, all files included in the project are compiled, regardless of whether changes have been made to them or not. To build the project, use the menu command Project | Build (Project | Collect<проект1>).

You can run a project from the environment Delphi and from the Windows environment. Executing a project from the environment Delphi carried out by the team Run | Run(Run | Run) or by pressing a key . At the same time, the created application begins its work. If changes have been made to the project files, the project is first compiled. A running application works the same as one launched outside the environment Delphi however there are some features:

· you cannot launch a second copy of the application;

· You can continue developing the project only after the application is completed;

When an application loops (freezes), it must be terminated using Delphi using the command Run | Program Reset(Run | Stop program) or key combinations + .

You can use debugger tools to debug applications in Delphi.

From the Windows environment, the created application can be launched like any other application, for example, using Explorer.

Control questions

1. Basic stages of physical design software product: programming stage, processing by a translator, layout.

2. Stages of the translation process: lexical analysis - the essence, tasks of lexical analysis, organization of classes of lexical analyzer tables; parsing; code generation.

3. Linking stage: definition of the Linker, the essence of the linking process, the result of linking, definition of the executable file.

4. The concept of a library, working with libraries.

5. Types of binding: the concept of passage, actions performed on each passage; resolution process external links, static and dynamic binding, types of static binding.

7. Functions of the main tools of the integrated development environment: text editor, debugger, purpose and operation of the link editor, loader (OS tool), profiler, main types of libraries.

8. Definition of the translator, input and output data of the translator.

9. Compilers: definition; input and output data; compilation definition; the concepts of “object file”, “target computing system"; compilation steps; general scheme of the compiler, compilation phases; concept of pass, multi-pass and single-pass compilers; principles of operation of modern compilers.

10. Interpreters: definition; input and output data; difference from the compiler; algorithm for a simple interpreter; advantages and disadvantages of interpreters.

11. Macrogeneration: definitions of the concepts “Macrocommand”, “Macrogeneration”, “Macroexpansion”, “Macrosubstitution”, “Macrodefinition”; principle of macro command execution; macroprocessor, the result of its work; application of macrolanguages ​​in high-level languages.

12. Preprocessor: definition; principle of operation.

13. Compiling and executing the Delphi project.


Lecture 12

SUBJECT: Software version management.

Literature: 1. Ben Collins-Sussman, Brian W. Fitzpatrick, C. Michael Pilato Version Control in Subversion. URL: http://svnbook.red-bean.com/nightly/ru/index.html.

2. Natalia Elmanova. Borland StarTeam 6.0// ComputerPress 6"2004.

3. IBM Rational ClearCase. Manage file versions and builds. URL: http://cmcons.com/tech_rational/IBM_Rational_ClearCase.

Change management in projects is carried out at all stages of application creation. This is one of the most important parts of the project, because changes can occur in requirements, code, and models created during the business modeling and design stages. Without tracking changes and timely notification of those project participants who are affected by the changes made, it is difficult to manage a project: the project manager must be aware of what exactly is happening on the project. at this stage and what has already been implemented in the project, otherwise he risks never completing the project at all.

If you want to know who and when changed this or that line in the source code file of a program or website, make the work of two or more programmers on one project transparent, simple and easily controlled, or want to be able to undo changes in the code made a week ago , and also be able to access the sources of the old version of the program, without worrying about timely creation of backup copies, then you need to use a version control system.



Modern version control systems must track changes not only to application source code, but also to models, documents, datasets, and more.

Currently, various version control systems are offered, such as Microsoft Visual SourceSafe, StarBase’sStarTeam, RationalClearCase, Subversion, etc.

Microsoft Visual SourceSafe (VSS) supports client-server mode of operation and is intended for small development teams; it allows you to store files shared by several users in a common storage; a version history is stored for each file. VSS was included with Microsoft Visual Studio 2005 and was integrated with its products. Available only for Windows platforms. The Unix version is supported by MainSoft. Today, a new Microsoft product is being offered to replace SourceSafe - Team Foundation Server.

Borland StarTeam- scalable configuration management tool software, storing all the necessary data in a centralized repository and facilitating the interaction of employees responsible for performing various tasks. This product provides a team of project participants with a variety of tools for publishing requirements, managing tasks, planning, working, discussing changes, version control, and organizing document flow. Supports different client interfaces (Windows, UNIX, Linux and Web interfaces), integrates with Microsoft Project.

ClearCase– Rational Software's configuration management tool. It handles version, workspace, build, and process management. ClearCase is a scalable client/server tool and is responsible for storing and tracking all project artifacts. Developers and managers can monitor the progress of changes in real time, receiving the necessary versions for editing and viewing without additional synchronization with the database, which greatly increases the “agility” of large systems, allowing you to quickly move from version to version, from project to project. ClearCase allows you to simply return to the previous version, while getting full set project artifacts (versions source files, documentation, requirements, models, test scripts), corresponding to the previous version, and build a new version on its basis.

Typically, ClearCase is chosen by companies with a large number of employees and with a large number of projects that need to be supported simultaneously. This choice is completely justified, since at the moment CC is the only tool that allows you to work with any subset of projects, both locally and distributed (union of geographically remote development teams).

Subversion is a free and open source version control system. It allows you to manage files and directories over time and is a system general purpose, which can be used to control any a set of files. For you, this will be the source code of your programs, and for someone else it will be a list of products or a mixed digital video. The file tree is placed in a central repository, which is similar to a regular file server except that it remembers every change made to a file or directory. This allows you to restore earlier versions of data and explore the history of data changes. Thanks to this, many people consider the version control system to be a kind of “time machine”. Subversion is designed specifically to replace CVS, the most common open system version control. It has all the basic functions of CVS (although it performs some of them in other ways) and does not have a number of its shortcomings. Subversion is often called "svn", after the name of the client program included in its distribution.

A typical application developed in Delphi consists of different types of files, each with a different name extension.

    Dpr. Delphi project file. Delphi creates a project file automatically the first time you save a new application. Typically there is no need to modify the project file. You should not delete the .dpr file.

    Pas. Module files. Contains source code for programs in Delphi. Typically, a Delphi application has one .pas file for each display module, although you can include your own non-display code in these files. You should not delete the .pas file.

    Dfm. Form description files. They contain information about the properties of the screen form and the objects included in it, as well as about events and procedures for processing them. You should not delete the .dfm file.

    Dcu. Files containing compiled code derived from information in the corresponding .pas and .dfm files.

    Res. Resource file. Contains binary resources (icons and other bitmaps) used in the project.

    Exe. The executable file resulting from the .dcu files as a result of linking.

    Dof. Project options file. Contains project settings modified in the dialog box that opens using the Project/Options command.

    Dll. Library files. Contains code for the dynamic link library.

    .~*. Copy files of modified and saved files. For example, Main.~dp contains the previous save version of the Main.dpr file.

Project file structure

Any Delphi program consists of a project file (.dpr) and one or more modules (.pas). The project file is automatically created by Delphi and is intended to be processed by the compiler. To view the project file, use the Project/ViewSource menu item. Delphi will display a code window with a Project1 tab containing the following text:

program Project1;

Unit1 in "Unit1.pas" (Form1);

Application.Initialize;

Application.CreateForm(TForm1, Form1);

Application.Run;

Keyword program followed by the program name and a semicolon form the program title. The title is followed by a description section, in which after the keyword uses it is indicated that in addition to the project file, the program must use modules Forms And Unit1. Module Forms is standard (that is, already known to Delphi), and the module Unit1- new, previously unknown, therefore Delphi also indicates the name of the file with the text of the module (in"Unit1.pas") and the name of the form description file associated with the module (Form1).

Line ($R *.RES)- a compiler directive indicating the need to connect a resource file to the program.

The body of the program begins with a keyword begin and ends with a keyword end with a dot. The body consists of several language statements Delphi. Each of them implements a call to one of the object’s methods Application. In the object Application collected data and routines necessary for normal functioning Windows-programs as a whole. Delphi automatically creates a program object Application for every new project.

Using the method Initialize the program carries out a number of auxiliary actions necessary to work under the operating system Windows.

Method CreateForm creates and displays the main form window.

Method Run implements an endless cycle of receiving and processing incoming messages from Windows messages about user actions. When the user clicks the button Close,Windows will send a special message to the program that will force the program to stop running and release the system resources assigned to it.

Module structure

Modules- These are program units designed to contain program code. Implement the behavioral side of the application.

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

TForm1 = class(TForm)

(Private declarations)

(Public declarations)

The file name must match the module name.

The interface part (begins with the keyword interface) contains declarations of all global objects of the module (types, constants, variables and subroutines) that should become available to the main program and/or other modules.

The uses section specifies a list of connected modules.

The type section describes the TForm1 class, derived from the standard TForm class - the base Delphi class used to generate software objects such as screen forms. Members of a class can be both data and methods for processing it. Some class members can have private or public status. Private members are only accessible within a class module, but public members can be accessed from other modules.

The var section defines the Form1 variable as an object of the TForm1 class for which memory space is allocated.

The executable part begins with the implementation keyword. Line ($R *.DFM) is a compiler directive indicating the need to connect to the program a form description file with a name that matches the name of the module. The executable part contains descriptions of subroutines declared in the interface part. Details of the implementation of declared procedures, functions, classes are hidden in the implementation section and are not available to other modules.







2024 gtavrl.ru.