转:Delphi 回调函数及例子

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

时间: 2024-07-29 09:55:38

转:Delphi 回调函数及例子的相关文章

delphi回调函数

疯狂delphi delphiXE7.XE8.XE10公开课A 群号:58592705 delphi回调函数 文章来源: http://anony3721.blog.163.com/blog/static/5119742010866050589/ 一.主单元 unit UnMain; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graph

Delphi回调函数及其使用

Delphi回调函数及其使用 1 回调函数的概述 回调函数是这样一种机制:调用者在初始化一个对象(这里的对象是泛指,包括OOP中的对象.全局函数等)时,将一些参数传递给对象,同时将一个调用者可以访问的函数地址传递给该对象.这个函数就是调用者和被调用者之间的一种通知约定,当约定的事件发生时,被调用者(一般会包含一个工作线程)就会按照回调函数地址调用该函数.  这种方式,调用者在一个线程,被调用者在另一个线程. 消息: 消息也可以看作是某种形式的回调,因为消息也是在初始化时由调用者向被调用者传递一个

Delphi 回调函数及property的使用

要弄清楚Delphi中的回调,首先要弄清楚: delphi中经常见到的以下两种定义 Type TMouseProc = procedure (X,Y:integer); TMouseEvent = procedure (X,Y:integer) of Object; 两者样子差不多但实际意义却不一样, TMouseProc只是单一的函数指针类型; TMouseEvent是对象的函数指针,也就是对象/类的函数/方法 区别在于类方法存在一个隐藏参数self,也就是说两者形参不一样,所以不能相互转换.

js的回调函数 一些例子

这边用bootstrap 3.0的  上传控件做例子 下面是上传控件的一段完整的 js 操作 代码. <!-- 上传缩略图控件配置 --><script> // 定义这四个全局变量是 为了保存4张图片的uuid 在回调函数中每次保存 都覆盖前一张的名字 实现 用户可以重复添加取最终的结果保存入库 window.Picture1=""; //轮播图1的全局变量 window.Picture2=""; //轮播图2的全局变量 window.Pic

基于委托的C#异步编程的一个小例子 带有回调函数的例子

我创建的是一个winform测试项目:界面如下: 设置: 下面是代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; usin

java回调函数的例子

1. 定义一个回调接口 public interface CallBack { //执行回调操作的方法 void execute(); } 2. 定义一个类的方法,使上面定义的回调接口接口作为它的参数 . public class Test { /** * 测试函数,通过定义CallBack接口的execute方法 * @param callBack */ public void test(CallBack callBack) { System.out.println("回调前..."

c回调函数(真不好理解)

什么是回调函数(callback) 模块A有一个函数foo,它向模块B传递foo的地址,然后在B里面发生某种事件(event)时,通过从A里面传递过来的foo的地址调用foo,通知A发生了什么事情,让A作出相应反应. 那么我们就把foo称为回调函数. 例子: 回调函数是一个很有用,也很重要的概念.当发生某种事件时,系统或其他函数将会自动调用你定义的一段函数.回调函数在windows编程使用的场合很多, 比如Hook回调函数:MouseProc,GetMsgProc以及EnumWindows,Dr

C++回调函数(callback)的使用

什么是回调函数(callback)    模块A有一个函数foo,他向模块B传递foo的地址,然后在B里面发生某种事件(event)时,通过从A里面传递过来的foo的地址调用foo,通知A发生了什么事情,让A作出相应反应. 那么我们就把foo称为回调函数.   例子:      回调函数是个很有用,也很重要的概念.当发生某种事件时,系统或其他函数将会自动调用您定义的一段函数.回调函数在windows编程使用的场合很多, 比如Hook回调函数:MouseProc,GetMsgProc连同EnumW

JavaScript中的回调函数

在学习JavaScript的过程中遇到了很多,使用到回调函数的例子,出现了许多疑问,就由一个栗子开始吧: 在JavaScript中接触的第一个回调函数是在setInterval()和setTimeout()中出现的: 1 var num = 10; 2 3 var interValId = setInterval(function (){ 4 console.log(num); 5 num--; 6 if(num==0){ 7 clearInterval(interValId); 8 } 9 }