C++ 生成器透明位图覆盖 canvas

C++ builder transparent bitmaps cover the canvas

TFormOnPaint 事件中,我想绘制不覆盖背景或其他绘制对象的位图,因为它们具有透明部分。

如果我在图像上绘制图像,它就可以工作。

但是当我在窗体的 Canvas 上绘制时,它不起作用:图像的白色部分,本应是透明的,用白色覆盖了 Canvas 的其他对象正方形颜色。

Canvas->CopyMode = cmMergePaint ;
Graphics::TBitmap * Image=new Graphics::TBitmap();
Image->Transparent = true;
MainForm->Images->GetBitmap(14, Image);

Canvas->Draw(10,10,Image;
MainForm->Images->GetBitmap(0, Image);
Canvas->Draw(15,15,Image);

更新

当我使用 MainForm->Images->Draw(Image->Canvas...) 在图像上绘制时,我得到一个透明的正方形,里面什么也没有,我可以移动到其他组件上。

当我使用 MainForm->Images->GetBitmap(ImgIndex[HisType]+Rotation, Image) 绘图时,我在表单上得到了正确的拉伸图像,但没有透明胶片,即它的白色部分覆盖了其他组件。

虽然 MainForm->Images->Draw(Canvas, X, Y, ImgIndex[HisType]+Rotation, dsTransparent, itImage); 完成了这项工作,但我需要根据 Size 变量为该组件拉伸它。

TRect DstRect(X,Y, X+32 + ( 1 - Rotation ) * 32 * Size, Y+32 + Rotation * 32 * Size);
Graphics::TBitmap * Image=new Graphics::TBitmap();
Image->Transparent=true;
//MainForm->Images->GetBitmap(ImgIndex[HisType]+Rotation, Image);
MainForm->Images->Draw(Image->Canvas, 0, 0, ImgIndex[HisType]+Rotation, dsTransparent, itImage);
Canvas->StretchDraw(DstRect, Image);
delete Image;

//MainForm->Images->Draw(Canvas, X, Y, ImgIndex[HisType]+Rotation, dsTransparent, itImage);

改用Images->Draw(),让TImageList为您处理绘图:

MainForm->Images->Draw(Canvas, 10, 10, 14, dsTransparent, itImage);
MainForm->Images->Draw(Canvas, 15, 15, 0, dsTransparent, itImage);

找到解决方案,感谢 Remy。我们必须先用一种颜色填充新创建的位图,不要让它为空,这样透明度才能起作用...

Size=1; //debug
TRect DstRect(X,Y, X+32 + ( 1 - Rotation ) * 32 * Size, Y+32 + Rotation * 32 * Size);
Graphics::TBitmap * Image=new Graphics::TBitmap();
Image->Width = 32;
Image->Height = 32;
Image->Canvas->FillRect(Rect(0,0,32,32));
MainForm->Images->GetBitmap(ImgIndex[HisType]+Rotation, Image);
//MainForm->Images->Draw(Image->Canvas, 0, 0, ImgIndex[HisType]+Rotation, dsTransparent, itImage);
Image->Canvas->Pen->Color = clRed;
Image->Canvas->MoveTo( 3, 3 );
Image->Canvas->LineTo( 29, 29 );
Image->Transparent=true;
Canvas->StretchDraw(DstRect, Image);
delete Image;