帮忙解释一下回调函数!顶者有分
同上! 问题点数:50、回复次数:18Top
1 楼angelface(§<@#$&^%$>§)回复于 2003-11-04 08:38:53 得分 0
回头看一下windows程序设计吧Top
2 楼tjianliang(乡关何处)回复于 2003-11-04 08:45:14 得分 5
Wi n d o w s的消息系统是由3个部分组成的:
• 消息队列。Wi n d o w s能够为所有的应用程序维护一个消息队列。应用程序必须从消息队列中获取
消息,然后分派给某个窗口。
• 消息循环。通过这个循环机制应用程序从消息队列中检索消息,再把它分派给适当的窗口,然
后继续从消息队列中检索下一条消息,再分派给适当的窗口,依次进行。
• 窗口过程。每个窗口都有一个窗口过程来接收传递给窗口的消息,它的任务就是获取消息然后
响应它。窗口过程是一个回调函数;处理了一个消息后,它通常要返回一个值给Wi n d o w s。
注意回调函数是程序中的一种函数,它是由Windows或外部模块调用的。Top
3 楼outer2000(天外流星)回复于 2003-11-04 08:51:31 得分 35
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
ttest=function(s:string):string;
var
Form1: TForm1;
implementation
{$R *.dfm}
function test1(s:string):String;
begin
result:=s;
end;
procedure mytest(t1:ttest;tmp:integer);
begin
showmessage(t1(inttostr(tmp)));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
mytest(test1,2);
end;
end.Top
4 楼outer2000(天外流星)回复于 2003-11-04 08:53:46 得分 0
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
ttest=function(s:string):string;//先定义一个回调函数;
var
Form1: TForm1;
implementation
{$R *.dfm}
function test1(s:string):String;//这个是回调函数的实现,和你定义的保持一致;
begin
result:=s;
end;
procedure mytest(t1:ttest;tmp:integer);//这里把回调函数传递进去
begin
showmessage(t1(inttostr(tmp)));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
mytest(test1,2);//调用;
end;
end.Top
5 楼xiaoxiao197821(你的笑对我很重要)回复于 2003-11-04 08:57:03 得分 10
通俗的说:调用的函数(内部代码)需要使用自己编写的一个函数(外部代码) ,自己编写的
这个函数就是回调函数。
例子如下:(实现流动线的效果)
Timer1.Interval = 200;
var
Form1: TForm1;
Counter :Byte;
implementation
{$R *.DFM}
procedure MovingDots(X,Y: Integer; TheCanvas: TCanvas); stdcall;
begin
Counter := Counter shl 1; // Shift the bit left one
if Counter = 0 then Counter := 1; // If it shifts off left, reset it
if (Counter and 224) > 0 then // Are any of the left 3 bits set?
TheCanvas.Pixels[X,Y] :=clBtnFace // Erase the pixel
else
TheCanvas.Pixels[X,Y] :=clBlack; // Draw the pixel
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
LineDDA(30,30,200,30,@MovingDots,LongInt(Canvas));
LineDDA(200,30,200,150,@MovingDots,LongInt(Canvas));
LineDDA(200,150,30,150,@MovingDots,LongInt(Canvas));
LineDDA(30,150,30,30,@MovingDots,LongInt(Canvas));
end;
以上的LineDDA就是我们说得内部函数,而它调用的MovingDots就是自己定义的外部函数,
也就是回调函数。
Top
6 楼LKJ99(路是人走的,命是天定的)回复于 2003-11-04 09:01:29 得分 0
呵呵,其实很多DELPHI的书籍都会讲一点这个问题,而且是针对DELPHI讲,
你去找找!
我记得很多帖子也有这个问题,搜索一下!!!Top
7 楼pingo888(逸风)回复于 2003-11-04 09:03:44 得分 0
顶!
找找吧Top
8 楼fhuibo(永远深爱一个叫“莎”的好女孩儿)回复于 2003-11-04 09:07:21 得分 0
studyTop
9 楼icecsdn(问到懂为止!~)回复于 2003-11-04 09:39:35 得分 0
to : outer2000(天外流星)我在窗口放了一个EDIT 为什么我在你的procedure mytest(t1:ttest;tmp:integer)的过程里这么写会说编译出错?好想不能引用一样
begin
edit1.text := t1(inttostr(tmp));
end;Top
10 楼charles2118(第六元素)回复于 2003-11-04 10:37:16 得分 0
outer2000(天外流星)给的例子很好阿
多谢了
icecsdn(问到懂为止!~) 的问题我想是你在窗体中加了EDIT控件,但是你把流星兄的代码全部拷过来了,在TForm中EDIT没加,所以有错的吧!
Top
11 楼icecsdn(问到懂为止!~)回复于 2003-11-04 10:47:12 得分 0
不是的,我在窗口放了一个EDIT控件,然后把代码改成edit1.text := t1(inttostr(tmp));就出错了,不信你44。Top
12 楼charles2118(第六元素)回复于 2003-11-05 10:08:49 得分 0
呵呵,可以的
但是要设置成Form1.Edit1.Text:= (t1(inttostr(tmp)));
是form的作用范围问题Top
13 楼Erice(白雪公猪)回复于 2003-11-05 10:16:52 得分 0
学习,听课Top
14 楼hotdog911(昱)回复于 2003-11-05 10:19:39 得分 0
学习,upTop
15 楼icecsdn(问到懂为止!~)回复于 2003-11-05 10:26:25 得分 0
不行呀,我就写的Form1.Edit1.Text:= (t1(inttostr(tmp)));出错。
[Error] Unit1.pas(31): Undeclared identifier: 'Edit1'
假设正确的话为什么还要设置FORM的作用范围?Top
16 楼Alanwn(天痴(日本人与狗不得回答问题))回复于 2003-11-05 10:27:39 得分 0
我也正好遇到这样的问题,我的初步理解是,在函数调用的同时还有函数在其中被调用,但是这个调用的返回值有很多,返回什么值对应一个什么函数,也就是对应一个东西被反馈回来,我听高手们说windows的核心就是回调函数。
我就理解这么多,还有请高手们指点指点,我说的对不?我们共同关注!Top
17 楼icecsdn(问到懂为止!~)回复于 2003-11-05 10:28:30 得分 0
呵呵,刚才我弄错了,和你昨天说的一样。能编译OK了。
但是为什么还要设置FORM的作用范围?Top
18 楼charles2118(第六元素)回复于 2003-11-05 11:14:57 得分 0
to icecsdn(问到懂为止!~)
^_^
type
TForm1 = class(TForm)
Button1: TButton;
edit1:TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
ttest=function(s:string):string;
仔细看看这里的
你在函数中要用form中的东东,总要先去跟它说一声的阿!
揭帖了!
多谢各位了!Top




