我有一个VCL表单,上面有一个TBitBtn和一个包含2个位图的TImageList。在运行时,我运行以下代码行,将其中一张位图放在我的TBitBtn上:
ImageList1->GetBitmap(1, BitBtn1->Glyph);
这成功地将位图放在TBitBtn上。然后,我运行下面的代码行来更改位图,但没有任何反应:
ImageList1->GetBitmap(0, BitBtn1->Glyph);
这两个位图都出现在图像列表中(0和1)。我可以交换代码行,并证明imagelist没错。这是一篇旧文章,似乎有人在Delphi中解决了这个问题。我在想我必须先以某种方式清除字形,但我不知道在C ++中如何使用。
这是使用它的一种方法的示例,使用临时方法TBitmap
从中检索图像TImageList
并将其在运行时放入字形中。在此示例中,它是在TBitBtn->OnClick
事件处理程序上执行的。
void __fastcall TForm1::btn1Click(TObject *Sender)
{
// FOdd is a bool variable defined in the form's private section.
// It's just being used here as a toggle to flip between the images
this->FOdd = !this->FOdd;
TBitmap *bmp = new Graphics::TBitmap();
try {
bmp->SetSize(this->ImageList1->Width, this->ImageList1->Height);
this->ImageList1->GetBitmap(int(FOdd), bmp);
this->BitBtn1->Glyph->Assign(bmp);
}
__finally
{
delete bmp;
}
}
@ relayman357为try ..__ finally块提供了正确的代码,以使此答案更合适。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句