我的目的是:有加减两个按钮,点加(btn1)的时候实现加的功能,点减(btn2)的时候实现两个数相减的功能(不要改方法,我就想用回调函数试一下)unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
Tfunctiontype=function(a,b:Integer):Integer;type
TForm1 = class(TForm)
edt1: TEdit;
edt2: TEdit;
lbl1: TLabel;
lbl2: TLabel;
lbl3: TLabel;
edt3: TEdit;
btn1: TButton;
btn2: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function ADD(ParaA,ParaB:Integer):Integer;stdcall;
function SUB(ParaA,ParaB:integer):Integer;stdcall;
function Cal(a, b: Integer; fun:Tfunctiontype ): Integer;//stdcall;
end;var
Form1: TForm1;implementation
{$R *.dfm}{ TForm1 }function TForm1.ADD(ParaA, ParaB: Integer): Integer;
begin
Result:=ParaA+ParaB;
end;function TForm1.SUB(ParaA, ParaB: integer): Integer;
begin
result:=ParaA-ParaB;
end;function TForm1.Cal(a, b: Integer; fun:Tfunctiontype ): Integer;
begin
result:=fun(a,b);
end;procedure TForm1.btn1Click(Sender: TObject);
begin
edt3.Text:=IntToStr(Cal(StrToInt(edt1.Text),StrToInt(edt2.Text),ADD);
end;procedure TForm1.btn2Click(Sender: TObject);
begin
edt3.Text:=IntToStr(Cal(StrToInt(edt1.Text),StrToInt(edt2.Text),SUB);
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
Tfunctiontype=function(a,b:Integer):Integer;type
TForm1 = class(TForm)
edt1: TEdit;
edt2: TEdit;
lbl1: TLabel;
lbl2: TLabel;
lbl3: TLabel;
edt3: TEdit;
btn1: TButton;
btn2: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function ADD(ParaA,ParaB:Integer):Integer;stdcall;
function SUB(ParaA,ParaB:integer):Integer;stdcall;
function Cal(a, b: Integer; fun:Tfunctiontype ): Integer;//stdcall;
end;var
Form1: TForm1;implementation
{$R *.dfm}{ TForm1 }function TForm1.ADD(ParaA, ParaB: Integer): Integer;
begin
Result:=ParaA+ParaB;
end;function TForm1.SUB(ParaA, ParaB: integer): Integer;
begin
result:=ParaA-ParaB;
end;function TForm1.Cal(a, b: Integer; fun:Tfunctiontype ): Integer;
begin
result:=fun(a,b);
end;procedure TForm1.btn1Click(Sender: TObject);
begin
edt3.Text:=IntToStr(Cal(StrToInt(edt1.Text),StrToInt(edt2.Text),ADD);
end;procedure TForm1.btn2Click(Sender: TObject);
begin
edt3.Text:=IntToStr(Cal(StrToInt(edt1.Text),StrToInt(edt2.Text),SUB);
end;end.
解决方案 »
- delphi简单字符替换
- delphi8.0的error问题
- 怎么将ACCESS数据库中的记录ID取最大值?
- 用此CreateOleObject('Excel.Application')后不能感知代码
- 關于QRBand的Expression屬性的一個問題﹐請大俠指教﹐急
- 请问:如何将带有‘的字符串内容写入到相关表中!如:将I DON'T KNOW写入到相关表的字符串字段内!!
- 问有关数据库的问题~~(我是新手,望各高手们帮忙)
- 用lineto()的时候超出image大小,怎么才能让它自动出现滚动条???
- 大家好,探讨一下CTTS中文语音的问题, 我下载了一个,但是在DELPHI 中不能用,请问谁有好的,能给一份吗?[email protected]
- 我想按我设计的表格去打印每一条纪录,如何做报表
- 查询数据有遗漏
- WIN7 Hook inet_addr 无效求解决办法
Tfunctiontype=function(a,b:Integer):Integer of object;
还有,stdcall也和定义的函数不匹配,要么都有,要么都没有
2.public下面的函数删掉
3.ADD,SUB,Cal前面的TForm1删掉
4.调用:Cal(1,2,@ADD) 或者 Cal(1,2,@SUB); 前面2个参数自己定
5.ADD和SUB后面加上stdcall;另外也可以用of object这种对象形式,那么这样改:
1.声明:type Tfunctiontype=function(a,b:Integer):Integer of object stdcall;
2.public下面的函数ADD和SUB用了stdcall,后面实现的代码也要加上
3.最后调用少了个括号