码迷,mamicode.com
首页 > Windows程序 > 详细

delphi回调函数

时间:2016-09-05 23:31:28      阅读:570      评论:0      收藏:0      [点我收藏+]

标签:

delphi回调函数

技术分享

 

文章来源: 
http://anony3721.blog.163.com/blog/static/5119742010866050589/

 

一.主单元

unit UnMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Imaging.jpeg,
  Vcl.ExtCtrls;

type
  TFrmMain = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure Test(str:string); {定义一个用于回调的过程}
  public
    { Public declarations }
  end;

var
  FrmMain: TFrmMain;

implementation
{引用子窗体单元}
uses UnChild;
{$R *.dfm}

procedure TFrmMain.Button1Click(Sender: TObject);
begin
  {调用Unit2的接口方法}
  CallUnit2(test);
end;

{回调过程的实现部分}
procedure TFrmMain.test(str: string);
begin
  {将str值副给Edit1}
  Edit1.Text:=str;
end;

end.

 

二.子单元

unit UnChild;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
   TFuncCallBack = procedure(str:string) of object;
  TFrmChild = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
    {定义一个回调函数类型的变量}
    aFuncCallBack:TFuncCallBack;
  end;
   {提供给Unit1调用的接口方法,注意里面的参数的类型}
    procedure CallUnit2(FuncCallBack:TFuncCallBack);
var
  FrmChild: TFrmChild;

implementation

{$R *.dfm}
{接口方法的实现部分}
procedure CallUnit2(FuncCallBack:TFuncCallBack);
begin
  Application.CreateForm(TFrmChild,FrmChild);
  {将参数赋值给FuncCallBack}
  FrmChild.aFuncCallBack:=FuncCallBack;
  FrmChild.ShowModal;
end;

procedure TFrmChild.Button1Click(Sender: TObject);
begin
  {当点击Form2的按钮时将Form2中的Edit的值传递给了Form1中的Edit}
  {是不是很神奇?我并没有uses Unit1,但却改变了Form1中Edit的Text属性}
  aFuncCallBack(Edit1.Text);
  ModalResult:=mrOk;
end;

end.

 

http://www.cnblogs.com/FKdelphi/p/5843104.html

 

delphi回调函数

标签:

原文地址:http://www.cnblogs.com/findumars/p/5844055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!