Part 5 Compilation File (Page 12)
Our next example is a program written in VCL (Visual Component Library) in the IDE. This program automatically forms frame windows and resource files, so you cannot compile from a single source file. But he illustrates an important feature of the delphi language. In addition to multi-unit, classes and objects can be used.
This program includes a project file, and 2 new unit files. First, the project file is as follows: PRogram greeting;uses Forms, Unit1, Unit2;{$R *.res} // This directive links the project's resource file. // This is the project's resource file instruction line begin// Calls to global application instance//Call global Application instance Application.Initialize;Application.CreateForm(TForm1, Form1);Application.CreateForm(TForm2, Form2);Application.Run;end.In one, our program is named greeting. He used 3 unit files. Forms is part of vcl; Unitl is the main window of the application being fed together; Unit2 is another window of the fed together.
This program calls a series of objects called Application, which is an instance of the Tapplication class defined in the form Unit unit. (Each project automatically generates an Application object.) Two methods named Createform call from Tapplication. The first call CreateForm to create Form1 is an instance of the Tform1 class defined in Unit1. The second call CreateForm, creates Form2, and defines an instance of the Tfrom2 class in Unit2. 10Unit1 looks like this:Unit1 looks like this:unit Unit1;interfaceuses SysUtils, Types, Classes, Graphics, Controls, Forms, Dialogs;typeTForm1 = class(TForm)Button1: TButton;procedure Button1Click(Sender: TObject);end; varForm1: TForm1; implementationuses Unit2;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); beginForm2.ShowModal;end;end.Unti1 Create a class named Tform1 (derived from Tform) and one of this class Instance, Form1.Tform1 contains a buttonButton1 button, an instance of the TButton class and a process named Button1Click, which is called when the user clicks Button1. Button1Click hides Form1 to show Form2 (by calling Form2.ShowModal).
Form2 is a defined unit Unit2 in Unit2; interfaceuses SysUtils, Types, Classes, Graphics, Controls, Forms, Dialogs; typeTForm2 = class(TForm)Label1: TLabel;CancelButton: TButton;procedure CancelButtonClick(Sender: TObject);end; varForm2: TForm2; implementation11uses Unit1;{$R *.dfm}procedure TForm2.CancelButtonClick(Sender: TObject); beginForm2.Close;end;end.Unit2 creates a class named Tform2 and an instance of this class, Form2. Tform2 contains a button (an instance of CancelButton, an instance of Tbutton) and a text box (an instance of Label1, an instance of Tlabel). You can't see this form in the source code, except that Label1 shows the title read hello world!
Tform2 declares and defines a method CancelButtonClick, which is called when the user presses CancelButton at runtime. This process (along with Form1.Button1Click in Unit1) is called an event handler. Because it is an event that is responded to when the program is running. Event handles are special events defined in Form1 and Form2 form files. When the greeting program starts running, form1 is displayed and form2 is hidden. (By default, only the first window created in the project file is visible at runtime, and it is called the project main window.) When the user presses the button in form1, form2 shows hello world! Listen to the high heat. When the user presses the CancelButton button or the Close button on the title bar, Form2 is closed.