
没必要在命令行参数中包括 Unit1。当编译器处理Greeting.dpr 时,它手动查找Greeting 程序所依赖(引用)的单元文件。程序的执行结果和上面的例子相同,它输出信息”Hello world!”。
2.3 A VCL Application
2.3一个可视化程序
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.
我们的下一个实例程序是在 IDE 下用VCL组件生成的,它使用手动产生的字段文件和资源文件,因此,你不能仅仅使用源代码来编译它。它表明了Delphi语言 的重要特征。除包括多个单元外,这个程序还使用了类和对象。
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 的调用 } 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).
我们的程序而是叫Greeting。它引用了三个单元:一个是Forms 单元,它是VCL 的一部分;二是Unit1 单元,它和程序的主窗体(Form1)相关联;三是Unit2 单元,它和另一个窗体(Form2)相关联。
The program makes a series of calls to an object named Application, which is an instance of the Vcl.Forms.TApplication class defined in the Forms unit. (Every project has an automatically generated Application object.) Two of these calls invoke a Vcl.Forms.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.
这个程序调用 Application 对象的一系列方法。Application 是类Vcl.Forms.TApplication 的一个实例,它在Forms 单元定义(每个项目自动生成一个Application 对象)。这些调用中有两个调用了Vcl.Forms.TApplication的CreateForm 方法,第一个CreateForm 创建Form1,它是类TForm1(在Unit1 单元定义)的一个实例;第二个CreateForm 创建Form2,它是类TForm2(在Unit2 单元定义)的一个实例。
Unit1 looks like this:
Unit1 单元看起来像上面的样子:

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 Vcl.Forms.TForm) and an instance of this class Form1. The TForm1 class includes a button -- Button1, an instance of Vcl.StdCtrls.TButton -- and a procedure named Button1Click that is called when the user presses Button1. Button1Click hides Form1 and displays Form2 (the call to Form2.ShowModal).
Unit1 单元创建了类TForm1(继承自Vcl.Forms.TForm)和它的一个实例Form1。TForm1 包含一个按钮Button1,它是Vcl.StdCtrls.TButton 的一个实例;还包括一个过程TForm1.Button1Click,在运行时,当用户按下Button1 时它将被执行。TForm1.Button1Click 隐藏Form1 并显示Form2 ( 调用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.ShowModal依赖于使用手动建立的窗体。当这段例子代码完成后,使用手动建立窗体是被激活的。
Form2 is defined in Unit2:
Form2 在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 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. The TForm2 class includes a button (CancelButton, an instance of Vcl.StdCtrls.TButton) and a label (Label1, an instance of Vcl.StdCtrls.TLabel). You can not 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.
Unit2 单元创建了类TForm2 和它的一个实例Form2。TForm2 包含一个按钮(CancelButton,Vcl.StdCtrls.TButton 的一个实例)和一个标签(Label1,TLabel 的一个实例)。Label1 将显示”Hello world!” 标题,但从源代码中你不能看到这一点。标题是在Form2 的窗体文件Unit2.dfm 中定义的。
TForm2 declares and defines a method CancelButtonClick that will be invoked at run time 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.
Unit2 单元定义了一个过程。在运行时, 当用户点击CancelButton 时,TForm2.CancelButtonClick 将被调用。这个过程( 以及Unit1 单元的TForm1.Button1Click)是成为事件句柄,因为他们响应程序运行时出现的事件。事件句柄通过表单文件(Windows 下是.dfm,Linux 下是.xfm)赋给指定的事件Form1 和 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 run time. 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.
当 Greeting 程序开启时,显示Form1 而隐藏Form2(默认状况下,只有项目文件中最先创建的窗体是可见的,它称为主窗口)。当用户点击Form1 上的按钮时,Form1 消失而被Form2 取代,后者将显示”Hello world!” 信息。当用户关闭Form2(点击CancelButton 按钮或窗口标题栏上的关掉按键)后,Form1 重新显示。
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-123046-2.html
拉森号算新的了
完全物质化的叫兽
但没有想到北洋水师败得这么惨