Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə17/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   13   14   15   16   17   18   19   20   ...   294
DelphiLanguageGuide

A VCL Application
Our next example is an application built using the Visual Component Library (VCL) components in the IDE. This
program uses automatically generated form and resource files, so you won't be able to compile it from the source
code alone. But it illustrates important features of the Delphi Language. In addition to multiple units, the program
uses classes and objects
The program includes a project file and two new unit files. First, the project file:
program greeting;
                                        
        uses Forms, Unit1, Unit2;
        {$R *.res} // This directive links the project's resource file.
begin
   // Calls to global Application instance
   Application.Initialize;
   Application.CreateForm(TForm1, Form1);
   Application.CreateForm(TForm2, Form2);
   Application.Run;
end.
    
Once again, our program is called 
greeting
. It uses three units: 
Forms
, which is part of VCL; 
Unit1
, which is
associated with the application's main form (
Form1
); and 
Unit2
, which is associated with another form (
Form2
).
The program makes a series of calls to an object named 
Application
, which is an instance of the TApplication
class defined in the Forms unit. (Every project has an automatically generated 
Application
 object.) Two of these
calls invoke a TApplication method named CreateForm. The first call to CreateForm creates 
Form1
, an instance of
the 
TForm1
 class defined in 
Unit1
. The second call to CreateForm creates 
Form2
, an instance of the 
TForm2
class defined in 
Unit2
.
10


Unit1
 looks like this:
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.
Unit1
 creates a class named 
TForm1
 (derived from TForm) and an instance of this class, 
Form1

TForm1
 includes

buttonButton1
, an instance of TButton and a procedure named 
Button1Click
 that is called when the user
presses 
Button1

Button1Click
 hides 
Form1
 and it displays 
Form2
 (the call to 
Form2.ShowModal
).
Note:
In the previous example, 
Form2.ShowModal
 relies on the use of auto-created forms. While this is fine for
example code, using auto-created forms is actively discouraged.
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
 includes a button
(
CancelButton
, an instance of TButton) and a label (
Label1
, an instance of TLabel). You can't see this from the
source code, but 
Label1
 displays a caption that reads Hello world! The caption is defined in 
Form2
's form file, Unit2.
dfm.
TForm2
 declares and defines a method 
CancelButtonClick
 which will be invoked at runtime whenever the user
presses 
CancelButton
. This procedure (along with 
Unit1
's 
TForm1.Button1Click
) is called an event
handler because it responds to events that occur while the program is running. Event handlers are assigned to
specific events by the form files for 
Form1
 and 
Form2
.
When the 
greeting
 program starts, 
Form1
 is displayed and 
Form2
 is invisible. (By default, only the first form
created in the project file is visible at runtime. This is called the project's main form.) When the user presses the
button on 
Form1

Form2
, displays the Hello world! greeting. When the user presses the 
CancelButton
 or the
Close
 button on the title bar, 
Form2
 closes.
12



Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   13   14   15   16   17   18   19   20   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin