这是一个简单、方便而又实用的小技巧. 譬如这段代码中有四个定义函数: MyAdd、MyDec、MyMul、MyDiv
我们可以把其中的自定义函数(也可以是其他代码)剪切保存在一个文本文件中(譬如是: C:\DelphiFun\MyFun.inc);unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} //譬如下面四个自定义函数 ***************************** function MyAdd(const a,b: Integer): Integer; begin Result := a + b; end; function MyDec(const a,b: Integer): Integer; begin Result := a - b; end; function MyMul(const a,b: Integer): Integer; begin Result := a * b; end; function MyDiv(const a,b: Integer): Integer; begin Result := a div b; end; //**************************************************** //调用测试 procedure TForm1.FormCreate(Sender: TObject); const x = 8; y = 2; begin ShowMessageFmt('%d,%d,%d,%d',[MyAdd(x,y), MyDec(x,y), MyMul(x,y), MyDiv(x,y)]); {显示结果: 10,6,16,4} end; end. 然后在原来代码的位置用 {$INCLUDE C:\DelphiFun\MyFun.inc} 或 {$I C:\DelphiFun\MyFun.inc} 再引入即可(可以使用相对路径). 下面是使用后的代码: 另外: 引入 C 语言的 obj 文件是用 {$L 路径} 指令完成的.unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} {$I C:\DelphiFun\MyFun.inc} //调用测试 procedure TForm1.FormCreate(Sender: TObject); const x = 8; y = 2; begin ShowMessageFmt('%d,%d,%d,%d',[MyAdd(x,y), MyDec(x,y), MyMul(x,y), MyDiv(x,y)]); {显示结果: 10,6,16,4} end; end. |
|