在Delphi的窗口单元中,如何调用其它窗口的单元。

http://www.cnblogs.com/champion8/p/5569930.html


在Delphi的窗口单元中,如何调用其它窗口的单元。(转载) 

在Delphi中简单的调用单元 
unit实例  
一,新建一个工程文件,默认的文件是unit1,代码如下:unit Unit1; 

interface 

uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
   Dialogs, StdCtrls; 

type 
   TForm1 = class(TForm) 
     Button1: TButton; 
     Edit1: TEdit; 
    procedure Button1Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 

var 
   Form1: TForm1; 

implementation 

uses Unit2; 

{$R *.dfm} 

procedure TForm1.Button1Click(Sender: TObject); 
var 
   nTemp:integer; 
begin 
   nTemp:=add(3,4); 
  //也可以这样nTemp:=unit2.add(3,4); 

   edit1.Text:=inttostr(nTemp); 
end; 

end. 

  

2,新建一个unit,默认名称是unit2,代码如下: 

unit Unit2; 

interface 
  uses windows,messages, SysUtils, Variants, Classes; 
  function add(a,b:integer):integer; 

implementation 

function add(a,b:integer):integer; 
begin 
   result:=a+b; 
end; 

end. 

  

从这里我们可以看到,单元的引用是非常简单的. 
我们的unit2只是一个代码单元,没有窗体.在interface节中我们相当于声明了单元的对外可见部分,在implementation中,定义了实现部分. 
在unit1当中,我们引用unit2后,直接可以调用add函数了,当然,也可以在前面加上unit2.add()这样的形式. 

在这个单元中,我们可以把一些公用函数,类等东西放进去,实现程序的模块化.便于程序结构明晰.也便于程序维护。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值