如何动态生成多个图片控件
需要动态生成几百个图片,我用TImage控件。
TImage *a = new TImage(this);倒是可以new出来,但我需要添加图片的点击,鼠标按下事件。不知道怎么添加事件了。
难道非要手动添加几百个图片控件?
[解决办法]
void __fastcall TForm1::ImageClick(TObject *Sender){TOpenPictureDialog *Open = new TOpenPictureDialog(this);Open->Filter = GraphicFilter(__classid(Graphics::TGraphic));//"照片文件(*.jpg)|*.jpg";if(Open->Execute ()){((TImage *)Sender)->Picture->LoadFromFile(Open->FileName);}delete Open;}void __fastcall TForm1::FormCreate(TObject *Sender){ImageList = new TList();for (int i =0; i < 10; i++) { TImage *a = new TImage(this); a->Parent = this; a->Top = 10; a->Left = 20*i*10; a->Width = 50; a->Height = 50; a->OnClick = ImageClick; ImageList->Add(a);}}//---------------------------------------void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action){for (int i = 0; i < ImageList->Count; i++) { delete (TImage *)ImageList->Items[i];}ImageList->Clear();delete ImageList;}//---------------------------------------
[解决办法]
循环里面类似这样设置, Image1Click是单击生成的事件, 然后根据Sender可以判断是哪个被点击了
a->OnClick = Image1Click;