Part 5 compile file (page 12)
Part 5 Compile Document (Page 12) Our next example is a program written in a VCL (visual component library) in the IDE. This program automatically forms a frame window and resource file, so you cannot compile from a single source file. But he explained an important feature of the Delphi language. In addition to multi-cell, you can use classes and objects.
This program includes a project file, and 2 new unit files. First, the project documents are as follows:
PROGRAM GREETING;
Uses form, unit1, unit2;
{$ R * .res} // This Directive Links The Project's Resource File.
// This is the project's resource file command line
Begin
// Calls to global Application Instance
// Call the 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 three unit files.
Forms is part of the VCL; UnitL is a main window combined with an application; Unit2 is another window being combined.
This program calls a series of objects called Applications, which is an instance of the TApplication class defined in the Forms Unit unit. (Each project automatically produces an Application object.) Two methods of calling from Tapplication of Createform. The first call CREATEFORM creates FORM1, is an instance of the TForm1 class defined in Unit1. The second call CREATEFORM, create FORM2, define an instance of the TFROM2 class in Unit2.
10
Unit1 Looks Like THIS:
Unit1 looks like the following:
Unit unit1;
Interface
Uses Sysutils, Types, Classes, Graphics, Controls, Forms, Dialogs;
Type
TFORM1 = Class (TFORM)
Button1: tbutton;
Procedure Button1Click (Sender: TOBJECT);
END;
VAR
FORM1: TFORM1;
IMPLEMENTATION
Uses unit2;
{$ R * .dfm}
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
Begin
Form2.showmodal;
END;
End.
Unti1 Create a class named TForm1 (from TForm derived) and an instance of this class, form1.tform1 contains a ButtonButton1 button, an instance of the TButton class and a process named Button1Click, when the user is called when the user clicks on Button1. Button1Click Hide Form1 Displays FORM2 (by calling form2.showmodal).
FORM2 is defined in Unit2
Unit unit2;
Interface
Uses Sysutils, Types, Classes, Graphics, Controls, Forms, Dialogs;
Type
TFORM2 = Class (TFORM)
Label1: TLABEL;
CancelButton: tbutton;
Procedure CancelButtonClick (Sender: TOBJECT); END;
VAR
Form2: TFORM2;
IMPLEMENTATION
11
Uses unit1;
{$ R * .dfm}
Procedure TForm2.cancelButtonClick (Sender: TOBJECT);
Begin
Form2.close;
END;
End.
Unit2 creates a class named TFORM2 and an instance of this class, Form2. TFORM2 contains a button (an instance of CancelButton, TButton) and a text box (an instance of Label1, TLabel). You can't see this form in the source code, except for the Label1 display headline read Hello World!
TFORM2 declaration and define a method CancelButtonClick, is called when the user presses CancelButton at runtime. This process (along with form1.button1click in Unit1) is called an event handle. Because events that are in response during the program run. Event handle is a special event defined in the 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 obvious at runtime, she is called the project main window.) When the user presses the button in Form1, Form2 shows Hello World! High heat Russia listening. When the user presses the CancelButton button or on the CLOSE button on the title bar, Form2 is turned off.
Posted on 2004-12-23 11:28 潇 2