简体   繁体   中英

OnClick event for dynamic TImage

I am trying to build a memory game with 16 pairs of cards.

I do not know exactly how to implement an OnClick event. I am new at using C++Builder, so please help.

The images are in an array, I allocate them dynamically like this:

for(int i=0;i<4;i++)
{
    for(int j = 0; j < 8 ; j++)
    {
        VectorOfImages[i*8+j]=new Card(9+i*112,9+j*112,pan, 0);
        VectorOfImages[i*8+j]->image->Picture>LoadFromFile("...OOP\\c\\images\\0.bmp");
        VectorOfImages[i*8+j]->image->Tag=i*8+j;
        VectorOfImages[i*8+j]->image->Enabled=false;
    }
}

OnClick is a property of TImage , you can assign it like you would any other property, eg:

for(int i = 0; i < 4; ++i)
{
    for(int j = 0; j < 8; ++j)
    {
        int idx = (i*8) + j;
        VectorOfImages[idx] = new Card(9+i*112, 9+j*112, pan, 0);
        VectorOfImages[idx]->image->Picture->LoadFromFile("...OOP\\c\\images\\0.bmp");
        VectorOfImages[idx]->image->Tag = idx;
        VectorOfImages[idx]->image->Enabled = false;
        VectorOfImages[idx]->OnClick = &ImageClicked; // <-- here
    }
}

Then, add ImageClicked() to your Form:

private:
    void __fastcall ImageClicked(TObject *Sender);

...

void __fastcall TMyForm::ImageClicked(TObject *Sender)
{
    // Sender points at the TImage that was clicked...
    TImage *Image = static_cast<TImage*>(Sender);
    // use Image as needed...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM