内容源自Delphi XE5 UPDATE 2官方帮助《Delphi Reference》,本人水平有限,欢迎各位高人修正相关错误!
也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可QQ:34484690@qq.com
2 Example Programs
2实例程序
The examples that follow illustrate basic features of Delphi programming. The examples show simple applications that would not normally be compiled from the IDE; you can compile them from the command line.
下面的实例演示 Delphi编程的基本特点,该示例显示简单的应用程序,通常不会从IDE中编译,你可以在命令行编译它们。
2.1 A Simple Console Application
2.1一个简单的控制台程序
The program below is a simple console application that you can compile and run from the command prompt:
下面是一个简单的控制台程序,你可以从命令行编译并运行它。
program Greeting; {$APPTYPE CONSOLE} var MyMessage: string; begin MyMessage := 'Hello world!'; Writeln(MyMessage); end.
The first line declares a program called Greeting. The {$APPTYPE CONSOLE} directive tells the compiler that this is a console application, to be run from the command line. The next line declares a variable called MyMessage, which holds a string. (Delphi has genuine string data types.) The program then assigns the string "Hello world!" to the variable MyMessage, and sends the contents of MyMessage to the standard output using the Writeln procedure. (Writeln is defined implicitly in the System unit, which the compiler automatically includes in every application.)
第一行声明程序叫做Greeting。{$APPTYPE CONSOLE} 指示字告诉编译器,这是一个控制台程序,它要从命令行运行。接下来的一行声明了一个变量MyMessage,它存储一个字符串(Delphi包含真正的字符串类型)。程序把字符串”Hello world!” 赋给变量MyMessage,然后使用Writeln 例程把MyMessage 的内容送到标准输出设备(Writeln 在System 单元声明,编译器在每个程序中自动包含这个单元)。
You can type this program into a file called greeting.pas or greeting.dpr and compile it by entering:
你可以把这个程序(源代码)输入到一个叫Greeting.pas 或Greeting.dpr 的文件中,然后在控制台输入如下命令编译它:
dcc32 greeting
to produce a Win32 executable.
在Win32平台上执行。
The resulting executable prints the message Hello world!
执行的结果是输出信息 Hello world!
Aside from its simplicity, this example differs in several important ways from programs that you are likely to write with Embarcadero development tools.
除了简单,上面这个例子和我们在 Embarcadero开发工具下写的程序有几个重要的不同。
First, it is a console application. Embarcadero development tools are most often used to write applications with graphical interfaces; hence, you would not ordinarily call Writeln. Moreover, the entire example program (save for Writeln) is in a single file. In a typical GUI application, the program heading the first line of the example would be placed in a separate project file that would not contain any of the actual application logic, other than a few calls to routines defined in unit files.
首先,这是一个控制台程序,Embarcadero开发工具通常创建图形界面的程序,因此,你不大可能调用Writeln(GUI 程序不能调用Writeln);而且,整个程序只有一个文件。在一个典型的程序中,程序头,也就是例子中的第一行,将被放在一个单独的工程文件中。工程文件通常不包含实际的程序逻辑,而只是调用在单元文件中定义的方法。
2.2 A More Complicated Example
2.2一个稍微复杂的程序
The next example shows a program that is divided into two files: a project file and a unit file. The project file, which you can save as greeting.dpr, looks like this:
下面的实例将程序被分成两个文件:一个工程文件,一个单元文件。工程文件可以存为Greeting.dpr,它看起来这样:
program Greeting; {$APPTYPE CONSOLE} uses Unit1; begin PrintMessage('Hello World!'); end.
The first line declares a program called greeting, which, once again, is a console application. The uses Unit1; clause tells the compiler that the program greeting depends on a unit called Unit1. Finally, the program calls the PrintMessage procedure, passing to it the string Hello World! The PrintMessage procedure is defined in Unit1. Here is the source code for Unit1, which must be saved in a file called Unit1.pas:
第一行声明程序叫做Greeting,它同样是个控制台程序。uses Unit1; 子句告诉编译器,Greeting包含(引用)一个叫做Unit1 的单元。最后,程序调用PrintMessage 过程,并把字符串”Hello world!”传递给它。请注意,PrintMessage 过程是从哪里来的?它是在Unit1 单元定义的。下面是Unit1 单元的源代码,你能把它保存在一个叫Unit1.pas 的文件中:
unit Unit1; interface procedure PrintMessage(msg: string); implementation procedure PrintMessage(msg: string); begin Writeln(msg); end; end.
Unit1 defines a procedure called PrintMessage that takes a single string as an argument and sends the string to the standard output. (In Delphi, routines that do not return a value are called procedures. Routines that return a value are called functions.)
Unit1 单元定义一个叫做PrintMessage 的过程,它接收一个字符串作为参数,并把它送到标准输出设备(在Delphi中,没有返回值的例程叫过程,有返回值的例程叫函数)。
Notice that PrintMessage is declared twice in Unit1. The first declaration, under the reserved word interface, makes PrintMessage available to other modules (such as greeting) that use Unit1. The second declaration, under the reserved word implementation, actually defines PrintMessage.
请注意,PrintMessage在Unit1 中声明了两次,第一次是在关键字interface 的下面,这使得它对于引用Unit1 单元的其它模块(比如Greeting)是可用的;第二次声明是在关键字implementation 的下面,它实际定义PrintMessage 过程。
You can now compile Greeting from the command line by entering
现在你可以在控制台输入如下命令编译 Greeting。
dcc32 greeting
to produce a Win32 executable.
在Win32平台上执行。
There is no need to include Unit1 as a command-line argument. When the compiler processes greeting.dpr, it automatically looks for unit files that the greeting program depends on. The resulting executable does the same thing as our first example: it prints the message Hello world!
没必要在命令行参数中包含 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 重新显示。