C++如何通过一个响应事件接受多个控件消息

时间:2021-01-26 16:02:35
在空的Form里加个Button,写入
void __fastcall TForm1::Button1Click(TObject *Sender)
{
for (long k=0; k<5; k++)
{
TLabel * label = new TLabel (Application);
label->Left = 10;
label->Top = k*30;
label->Parent = this;
label->Caption = "AAAA";
label->OnClick = LabelClick_All;
}
} 然后写:
void __fastcall TForm1::LabelClick_All(TObject *Sender)
{
TLabel * label = (TLabel*)Sender;
label->Caption = "Clicked!";
} Form的头文件里手动写:
void __fastcall LabelClick_All(TObject *Sender); 运行,点击Button1会看到5个Label,然后点哪个,哪个就变Caption
OnClick是函数指针,可以程序修改。其他消息响应函数同。