Creating a VCL Forms Application


To create a new VCL Forms application, double-click the VCL Forms Application item in the Delphi Projects category on the Tool Palette window or select File ® New ® VCL Forms Application - Delphi for Win32.

When the project is created, the IDE enables all the tools that can be used to design and develop VCL Forms applications (see Figure 11-1).

image from book
Figure 11-1: VCL Forms application project

The central point of a VCL Forms application is the main form, displayed on the Designer Surface. At design time, the Designer Surface enables you to resize the main form and to add additional objects (components) to it. Since currently only the main form exists, the form is automatically selected and its properties are displayed in the Object Inspector.

Before we thoroughly examine how VCL Forms applications function, let's change the title bar text of the main form to "My First VCL Application."

This requires modifying the form's Caption property in the Object Inspector. If the Object Inspector arranges properties by category, you can find the Caption property in the Action category (see Figure 11-2).

image from book
Figure 11-2: Modifying the Caption property

Before running the application, be sure to use the File ® Save All option to save the project files to disk. We have to use the Save All option because VCL Forms applications usually consist of more than just the main project file (.dpr). VCL Forms applications contain forms that are defined by two separate files: the Delphi form file (extension .dfm) and the form's unit (extension .pas). Accept the automatically generated names and run the application (see Figure 11-3).

image from book
Figure 11-3: VCL Forms application at run time

As you can see, VCL Forms applications and console applications differ greatly. When you run a console application, the execution starts in the main block. The console application sequentially executes the statements in the main block and terminates when all statements have executed.

VCL Forms applications don't execute statements sequentially. They are idle most of the time and execute parts of the code randomly as a response to an event. VCL Forms applications are thus called event-driven applications.

Every time a user presses a key on the keyboard, moves the mouse cursor, activates another application — in general every time an event occurs — the operating system creates a message that describes exactly what happened and sends that message to the appropriate window. When developing VCL Forms applications, we see these messages as events, and all that we have to do is write code that responds to a specific event. The Events tab in the Object Inspector displays the list of events to which the main form can respond.

image from book
Figure 11-4: Events of the main form

If you run the VCL Forms application from the Delphi IDE, you won't be able to see the Object Inspector until you close the application.

The Main Form

Every VCL form is described by two separate files: the source file and the form file. The source file contains the source code of the form, that is, the class interface and the event responses. Listing 11-1 shows the source code of the main form generated by the Delphi IDE.

Listing 11-1: Source code of the main form

image from book
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.
image from book

The interface section of the unit contains the declaration of a new class: TForm1. Every form we create inherits the properties, methods, and events of the TForm class declared in the Forms unit.

TForm1 = class(TForm)

The TForm1 class declaration is somewhat different from the class declarations presented in the previous chapter because the TForm1 class declaration contains private and public sections. The reserved words private and public define the visibility of class members. Private class members can only be used in the unit where the class is declared. Public members, on the other hand, are globally visible.

The interface section of the unit also contains a variable declaration — the declaration of the Form1 object. At design time, Delphi automatically creates the Form1 object to be displayed by the Designer Surface. The Form1 variable is also used by the code in the main project file to create the Form1 object (the main form) when the application starts.

The implementation section of the unit currently contains a very important line of code:

{$R *.dfm}

The $R compiler directive is used to include a resource file into the executable at compile time. Resources are files like images, cursors, sounds, and Delphi form files.

In units, the * symbol replaces the unit name, and in the main project file, the * symbol replaces the project name. Currently, the main form is declared in image from book Unit1.pas, and thus the form file has to be image from book Unit1.dfm.

If you remove the $R compiler directive, the application will successfully compile but will not be able to run (see Figure 11-5).

image from book
Figure 11-5: Application compiled without the form file

The Delphi form file contains the list of properties and property values of the form and all objects that we placed on the form at design time. Unlike the Object Inspector that displays all properties of a selected object, the form file only contains properties that were modified at design time. Delphi doesn't have to write all object properties to the form file because all objects have default values and Delphi knows how to access these values. By not writing all object properties to the form file, Delphi reduces the amount of redundant data, thus reducing the size of both the form file and the final executable.

The Delphi IDE automatically updates both the source code and the form files when we add, remove, or modify components at design time. Although it's best to leave this job to the Delphi IDE, there are situations where you might have to modify the contents of the form file yourself. The form file can be easily modified because it is a text file that can easily be viewed inside the IDE. If you want to see the textual representation of the form, right-click anywhere on the Designer Surface and select the View as Text option from the context menu.

When you select the View as Text option, the Designer Surface is hidden and the form file is displayed in the Code Editor (see Figure 11-6). To return to the Designer Surface, right-click anywhere in the Code Editor and select the View as Form option on the context menu, or press Alt+F12.

image from book
Figure 11-6: Contents of the Delphi form file

When you compile a VCL Forms application, the form's source file and the form file are compiled into a compiled unit file (.dcu).



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net