zoukankan      html  css  js  c++  java
  • delphi回调函数

    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

  • 相关阅读:
    Android自动化框架学习中遇到的方法
    Python中使用adb命令行
    monkeyrunner无法运行的问题解决方案总结
    TCP与UDP的区别
    KVM虚拟机的认知
    HTTP状态码分类
    FTP主动模式(Port)和被动模式(Passive)的区别
    Linux df -h 显示磁盘空间满,但实际未占用满——问题分析
    浅谈AD域
    zabbix连接Mysql提示Can’t connect to local MySQL server through socket的解决方法
  • 原文地址:https://www.cnblogs.com/findumars/p/5844055.html
Copyright © 2011-2022 走看看