[英]Problem with OnClick event signature in C++Builder
I would like to create a function which replaces the current image with another one.我想创建一个 function 用另一个替换当前图像。 The problem is that I have 64 pictures to replace.问题是我要替换 64 张图片。 I created a function with a TImage* Sender
parameter but it works only when I set Sender
as TObject*
instead.我创建了一个带有TImage* Sender
参数的 function 但它仅在我将Sender
设置为TObject*
时才有效。
How can I change this function:如何更改此 function:
void __fastcall TForm1::Image1Click(TObject *Sender)
{
Sender->Picture->LoadFromFile("puste.bmp");
}
into this进入这个
void __fastcall TForm1::Image1Click(TImage *Sender)
{
Sender->Picture->LoadFromFile("puste.bmp");
}
I am using the VCL library.我正在使用 VCL 库。
You can't change the signature of the event handler.您不能更改事件处理程序的签名。 It has to be what the VCL expects it to be, which in this case is defined by the TNotifyEvent
type, which is what the OnClick
event is declared as:它必须是 VCL 所期望的,在这种情况下由TNotifyEvent
类型定义,这就是OnClick
事件被声明为:
typedef void __fastcall (__closure *TNotifyEvent)(System::TObject* Sender);
__property System::Classes::TNotifyEvent OnClick = {read=FOnClick, write=FOnClick, stored=IsOnClickStored};
However, you don't need to change the signature.但是,您不需要更改签名。 All VCL components derive from TObject
, and the Sender
parameter points to the control that was clicked on.所有 VCL 组件都派生自TObject
,并且Sender
参数指向被点击的控件。 So, in this case, you can simply use a type-cast to access functionality that is specific to TImage
, eg:因此,在这种情况下,您可以简单地使用类型转换来访问特定于TImage
的功能,例如:
void __fastcall TForm1::Image1Click(TObject *Sender)
{
static_cast<TImage*>(Sender)->Picture->LoadFromFile("puste.bmp");
}
You can then assign this 1 handler to all 64 of your TImage
controls.然后,您可以将此 1 处理程序分配给所有 64 个TImage
控件。
If you need to differentiate between different TImage
controls, you can use the TImage
's Name
or Tag
property for that purpose.如果您需要区分不同的TImage
控件,您可以使用TImage
的Name
或Tag
属性来达到此目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.