Big question on the assignment of event property of VCL object in BCB(50分)

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

RoboCop

Unregistered / Unconfirmed
GUEST, unregistred user!
I created a TImage object dynamiclly,and I wanted to response to the mouse click on it,so I wrote a handler function just like this:

void __fastcall MyOnClick(TObject *Sender)

Then I should assign it (the function pointer MyOnClick) to OnClick event of my TImage object,so I wrote the following code:

pImg->OnClick=(TNotifyEvent)MyOnClick;

But I got the following compiler error:

Cannot cast from 'void (_fastcall *)(System::TObject *)' to 'void (_fastcall * (_closure )(System::TObject *))(System::TObject *)'

I was completely confused,and would you topguns like to help me?

Thank you very much!
 
void __fastcall MyOnClick(TObject *Sender)
应该是Form中的成员函数,
例子:
Unit1.h:
....

class TForm1 : public TForm
{
__published: // IDE-managed Components
void __fastcall FormCreate(TObject *Sender);
private: // User declarations
public: // User declarations
void __fastcall MyClick(TObject *Sender);
__fastcall TForm1(TComponent* Owner);
};
...
Unit1.cpp
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TImage *Image2;
Image2 = new TImage(this);
Image2->Parent = this ;
Image2->Left = 5 ;
Image2->Top = 5 ;
Image2->AutoSize = True;
Image2->OnClick = MyClick;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::MyClick(TObject *Sender)
{
ShowMessage("OK!");
}
 
接受答案了.
 

Similar threads

I
回复
0
查看
742
import
I
I
回复
0
查看
3K
import
I
I
回复
0
查看
3K
import
I
I
回复
0
查看
3K
import
I
I
回复
0
查看
1K
import
I
后退
顶部