偶然在使用DevExpress 的組件時,發現大部分的Unit 檔中,都有一個{$I cxVer.inc}或{$I cxGridVer.inc} 等類似的編譯指
令。
由於較少涉及Delphi 的編譯指令方面的內容,所以一時未明其實際用途。 事後查找Delphi 幫助文檔,Include file 索
引下有一段文字(詳細就不寫了)描述了其用法,大意是:在有{$I} 指令的地方,編譯該指令後面指定的Inc 文本
內容。
稍為琢磨了一下其用法,現貼出如下:
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls;
- type
- TForm1 = class(TForm)
- Button1: TButton;
- procedure Button1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.Button1Click(Sender: TObject);
- const
- str1 = 'TEST INC FILE';
- {$I CyDefine.inc} // 相當於在此處插入CyDefine.inc 文件中的代碼並編譯
- (*
- // 如果注釋上一句 {$I CyDefine.inc},實際等同於在此處插入Inc文件中的代碼
- var
- abc : string ;
- i,j : integer;
- *)
- begin
- {$I CyTest.inc} //相當於在此處插入CyTest.inc 文件中的代碼並編譯
- abc := 'declare in inc file'; //注意abc 變量是在CyDefine.inc文件中聲明的
- ShowMessage(abc);
- end;
- end.
通過上述例子不難明白,其實{$I incFile} 就相當於插入代碼塊。至於這樣做的好處,嘿嘿,就看各人所理解了。
Inc 文件的代碼內容,可以用記事本編輯,也可以在delphi IDE中打開編輯。注意記事本編輯時保存為ANSI 格式,不可以用Unicode.
編寫好的Inc 文件,最好放在可執行程式同一目錄,否則必需指定路徑。
代碼在Delphi7環境下測試過,其它Delphi 版本沒試過 :)
附上
CyTest.Inc 的文件內容如下:
Showmessage('Test Include Inc File');
Messagebox(handle,pchar('Message Hint'),pchar('Hint'),MB_OK);
CyDefine.inc 的文件內容如下:
//定義變量或常量,或其它代碼塊
var
abc : string ;
i :integer;