內容源自Delphi XE5 UPDATE 2官方幫助《Delphi Reference》,本人水平有限,歡迎各位高人修正相關錯誤! 也歡迎各位加入到Delphi學習資料漢化中來,有興趣者可QQ:[email protected] 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 重新顯示。