在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls; type TForm1 = class(TForm) {主窗体中放一个Edit和一个Button} Edit1: TEdit; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } {定义一个用于回调的过程} procedure test(str:string); public { Public declarations } end; var Form1: TForm1; implementation {引用unit2} uses unit2; {$R *.dfm} {回调过程的实现部分} procedure TForm1.test(str: string); begin {将str值副给Edit1} Edit1.Text:=str; end; procedure TForm1.Button1Click(Sender: TObject); begin {调用Unit2的接口方法} CallUnit2(test); end; end. unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type {定义一个回调函数类型} TFuncCallBack=procedure(str:string) of object; TForm2 = class(TForm) {Form2中也有一个Edit和一个Button} Edit1: TEdit; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } {定义一个回调函数类型的变量} aFuncCallBack:TFuncCallBack; public { Public declarations } end; {提供给Unit1调用的接口方法,注意里面的参数的类型} procedure CallUnit2(FuncCallBack:TFuncCallBack); var Form2: TForm2; implementation {$R *.dfm} {接口方法的实现部分} procedure CallUnit2(FuncCallBack:TFuncCallBack); begin Application.CreateForm(TForm2,Form2); {将参数赋值给FuncCallBack} Form2.aFuncCallBack:=FuncCallBack; Form2.ShowModal; end; procedure TForm2.Button1Click(Sender: TObject); begin {当点击Form2的按钮时将Form2中的Edit的值传递给了Form1中的Edit} {是不是很神奇?我并没有uses Unit1,但却改变了Form1中Edit的Text属性} aFuncCallBack(Edit1.Text); ModalResult:=mrOk; end; end. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论