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实现电脑桌面壁纸更换

Delphi 程序嵌入桌面效果的实现

Delphi 将label的caption内容竖向显示

Delphi应用程序 paramstr()带有参数

Delphi tidhttp 超时的解决方案

Delphi urlencode

Delphi把域名转换成ip

Delphi xe6 android下捕获功能键

Delphi 遍历所有edit控件

Delphi程序带参数运行

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



打赏

取消

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

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

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

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

评论

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