• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

转:Delphi回调函数及例子

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

http://anony3721.blog.163.com/blog/static/5119742010866050589/

{
  http://anony3721.blog.163.com/blog/static/5119742010866050589/ 例子出处
}
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    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 Unit1
object Form1: TForm1
  Left = 216
  Top = 98
  Width = 347
  Height = 308
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Edit1: TEdit
    Left = 32
    Top = 48
    Width = 121
    Height = 21
    ImeName = '中文 (简体) - 搜狗拼音输入法'
    TabOrder = 0
    Text = 'Edit1'
  end
  object Button1: TButton
    Left = 128
    Top = 152
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
end
object Form1: TForm1

 

 

unit Unit2;

interface

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

type
  {定义一个回调函数类型}
  TFuncCallBack=procedure(str:string) of object;
  TForm2 = class(TForm)
    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.
unit Unit2
object Form2: TForm2
  Left = 0
  Top = 0
  Width = 279
  Height = 191
  Caption = 'Form2'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Edit1: TEdit
    Left = 72
    Top = 24
    Width = 121
    Height = 21
    ImeName = '中文 (简体) - 搜狗拼音输入法'
    TabOrder = 0
    Text = 'Edit1'
  end
  object Button1: TButton
    Left = 80
    Top = 72
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
end
object Form2: TForm2

 

总结:回调 可以当做一个数据类型 使用 

procedure CallUnit2(FuncCallBack:TFuncCallBack);


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi中CxGrid控件使用总结发布时间:2022-07-18
下一篇:
delphitcp/ipIdTCPServer1实例一发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap