如何为动态生成的对象写事件函数?(50分)

  • 主题发起人 主题发起人 radio_kang
  • 开始时间 开始时间
R

radio_kang

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何为动态生成的对象写事件函数?
代码:
我动态生成一个TImage数组,如何为它们写一个处理OnMouseDown的函数,谢谢!
 
自己写一个和其参数相同的函数,动态生成时赋值就可以了。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
img: TImage;
procedure MyImageMouseMove(Sender: TObject;
Shift: TShiftState;
X,Y: Integer);
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
img := TImage.Create(self);
img.Parent := Form1;
img.Left := 10;
img.Top := 10;
img.OnMouseMove := MyImageMouseMove;
end;

procedure TForm1.MyImageMouseMove(Sender: TObject;
Shift: TShiftState;
X,
Y: Integer);
begin
ShowMessage('move');
end;

end.
 
zw84611的答案完成正確!
 
不好意思,我是说在C++Builder中如何用,谢谢
 
先在头文件中定义函数void __fastcall CompentMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y);
在cpp中写相应的代码.
TImage image;
image=new TImage(this);
image->Parent=this;
image->OnMouseDown=CompentMouseDown;
 
好象不行啊
出错信息是:
[C++ Error] Unit1.cpp(26): E2034 Cannot convert 'void (_fastcall *)(TObject *,TMouseButton,TShiftState,int,int)' to 'void (_fastcall * (_closure )(TObject *,TMouseButton,TShiftState,int,int))(TObject *,TMouseButton,TShiftState,int,int)'
 
奇怪,c++builder论坛怎么出现了delphi的代码,给楼主一段c++builder的:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TImage **image = new TImage*[3];
int j = 0;
for(int i =0;i<=2;i++)
{
j = j + 160;
image = new TImage(Application);
image->Parent = this;
image->Width = 120;
image->Height = 120;
image->Left = j;
image->Picture->LoadFromFile("c://贝壳800.bmp");
image->OnMouseDown = proc;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::proc(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
ShowMessage("调用OnMouseDown事件成功");
}
//---------------------------------------------------------------------------
顺便告诉各位,这里的人气好差啊,唉!!!!!!!!!!!!!!!!!!!!
 
接受答案了.
 
后退
顶部