delphi 使用 {$INCLUDE} 或 {$I} 指令管理和调用自定义函数


本文整理自网络,侵删。

 使用 {$INCLUDE} 或 {$I} 指令管理和调用自定义函数
这是一个简单、方便而又实用的小技巧. 譬如这段代码中有四个定义函数: MyAdd、MyDec、MyMul、MyDiv
--------------------------------------------------------------------------------

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.
--------------------------------------------------------------------------------
我们可以把其中的自定义函数(也可以是其他代码)剪切保存在一个文本文件中(譬如是: C:\DelphiFun\MyFun.inc);

然后在原来代码的位置用 {$INCLUDE C:\DelphiFun\MyFun.inc} 或 {$I 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}

{$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.
--------------------------------------------------------------------------------
另外: 引入 C 语言的 obj 文件是用 {$L 路径} 指令完成的.

相关阅读 >>

Delphi datamodule1 fdconnection1数据库连接

Delphi利用系统时间产生随机数的函数

Delphi xe2 将域名转ip

Delphi 在rxrichedit中插入图片的完美解决方法

Delphi 中拖动无边框窗口的5种方法

Delphi format函数的用法

Delphi tlog 日志

Delphi 文件名变小写的函数

Delphi enumwindows回调函数获取qq2009窗体句柄

Delphi编程如何判断图片文件的真实类型?

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...