写了一个DLL。处理主程序的按键。我想实现按键后 直接调用定义好的函数。现在的方法是写了一个类。虽然没必要type
  Tfunact=(act1,act2,act3);//每个函数功能不一样。但是参数都是一样的。
  TDhotkeys=record
    Fhotkey:dword;
     act:Tfunact;
   end;
 var hk:array [0..10]  of tdhotkeys; Procedure func1(par1,par2:dword);//参数就是按键的信息像键值什么的
 Procedure func2(par1,par2:dword);
 Procedure func3(par1,par2:dword);
 Procedure processhotkey(dhotkey:dword);
。现在的调用方法是:在onkeydown的时候 。调用 processhotkey 并传入按键信息。
Procedure processhotkey(dhotkey:dword);
var i:integer;
begin
 for I:=0 to 10 do
   begin
    if hk[i].fhotkey=dhotkey then
    begin
      case hk[i].act of
        act1:func1(....) ;
        act2:func2(....) ;
        act3:func3(....) ;
       end;
    end;
  end;
end;
这样的话 除了以后不好修改。而且如果函数多了改起来很非常的麻烦。有没有方法把那些 函数 绑定到 类里面。在DLL处理 onkeydown的时候  根据按键信息 直接调用相关的函数。

解决方案 »

  1.   

    你应该在processhotkey方法中传入Tfunact类型的事件参数,在调用端申明一个,然后传递给dll,以便dll中执行调用端的方法
      

  2.   

    自学的DELPHI很多都不会。能否写个简单的例子参考下。
      

  3.   

    别人的了:
    interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        ListBox1: TListBox;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
      type PFCALLBACK = function(Param1:integer;Param2:integer):integer;stdcall;
     // 定义回调函数的类型
    var
      Form1: TForm1;
      gCallBack:PFCALLBACK;
      function CBFunc(Param1:integer;Param2:integer):integer;stdcall;
    implementation
    //回调函数需要定义为全局
    {$R *.dfm}
    ///实现回调函数的功能
    function CBFunc(Param1:integer;Param2:integer):integer;
    var i:integer;
    begin
    //messagebox(application.Handle,'回调函数','提示信息',mb_ok);
    for i:=0 to 100 do
    begin
    sleep(100);
    self.ListBox1.Items.Add('回调函数');
    end;
    end;
    ///
    function MyThreadFunc(P:pointer):Longint;stdcall;
    begin
    gCallBack(0,1);//简单传个参数
    end;
    procedure testpro ;
    var i:integer;
       hThread:Thandle;//定义一个句柄
      ThreadID:DWord;
    begin
    for i:=0 to 4 do
    begin
    messagebox(application.Handle,'123','提示信息',mb_ok);
    if (i=2) then
    begin
    hthread:=CreateThread(nil,0,@MyThreadfunc,nil,0,ThreadID);//利用这种线程怎么说呢,肯定方便啦,但是
    //肯定功能上受到好多限制,所以啊,自己写,下次贴个上来
    end;
    end;
    end;
    ///
    function TestCallBack( Func:PFCALLBACK ):integer;
    begin
    gCallBack:=Func;
    testpro;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    //testpro;
    TestCallBack(@CBFunc);
    end;
    procedure TForm1.Button2Click(Sender: TObject);
    begin
    self.ListBox1.Clear;
    end;
    end.
    使用回调函数需要注意的地方:
    type PFCALLBACK = function(Param1:integer;Param2:integer):integer;stdcall;
     // 定义回调函数的类型
     function CBFunc(Param1:integer;Param2:integer):integer;stdcall;
    //全局函数定义,指向函数的函数,指针!!!名字可以随便取,但参数之类的需要与定义
    //的函数类型一致。
     function CBFunc(Param1:integer;Param2:integer):integer;
    //写该函数体就没什么好说拉
    function TestCallBack( Func:PFCALLBACK ):integer;
    //传递回调函数的入口地址,最重要啦!