写了一个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的时候 根据按键信息 直接调用相关的函数。
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中学号列 更新到表2的学号列
- Copyfile函数怎么调用?
- Delphi DBGrid为什么不能及时反应数据库的变化?
- 短信接收问题
- word问题。大家看看好吗?
- 初学数据库,请问用AdoConnection,AdoDataSet,DataSource够用吗?实现增加删除更新查询即可。
- 急...在线等 老问题 Access数据库日期型怎么查询
- 100分求助!关于realAudio控件的问题!
- 可怜可怜我吧!那位能具体解释一下SQL中的外连接的具体含义!!!
- 看过李维书的请进来看看
- ★★★★★饭桶牌农历控件包3.0 Beta版(带源码)发布啦★★★★★
- truncate这是个什么函数?
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;
//传递回调函数的入口地址,最重要啦!