wx小部件绘制透明度问题



下面是相关的代码行。从本质上讲,我所做的是创建一个更大的位图,绘制到这个,然后从那里创建一个较小的图像并将其绘制到屏幕上(wxDC mdc)。为了使其透明,我首先使用wxMemoryDC和wxGCDC,因为这是我能弄清楚的唯一方法。

问题是,除非剪掉的sub_bmp没有吸引任何东西,否则它才能完美地工作,然后它只是绘制黑色背景而不是透明背景。

有什么想法吗?

*bmp = wxBitmap(bwidth, bheight, 32);       
bmp->UseAlpha();
wxColor colour;
colour.Set("#800020");
penWidth = 4;
mdc->SetPen(wxPen(colour, penWidth));
wxMemoryDC memDC (*bmp);
wxGCDC dc(memDC);
dc.SetBackground(*wxTRANSPARENT_BRUSH);
dc.Clear();
dc.SetBrush(*wxRED_BRUSH);
dc.SetPen(wxPen(colour, penWidth));
...
b1.x = pix_offset_x - (cpix.x - b1.x);                  
b1.y = pix_offset_y - (cpix.y - b1.y);
b2.x = pix_offset_x - (cpix.x - b2.x);
b2.y = pix_offset_y - (cpix.y - b2.y);
dc.DrawLine(b1, b2);
memDC.SelectObject(wxNullBitmap);           //releases the bitmap from memDC
wxRect subSize(xloc,yloc , vp->pix_width*scaleFactor, vp->pix_height*scaleFactor);
wxBitmap sub_bmp = bmp->GetSubBitmap(subSize);
wxImage tmpimg = sub_bmp.ConvertToImage();
const wxBitmap tbmp(tmpimg.Scale(t_width, t_height),32);
mdc->DrawBitmap(tbmp, 0, 0, true);

假设您使用的是最新版本的 wxWidgets,您可以直接绘制到 wxImage,但首先您必须设置 alpha 通道。

然后,可以像当前一样缩放图像并将其复制到提供的 mdc。

// Setup the alpha channel.
unsigned char* alphaData = new unsigned char[bwidth * bheight];
memset (alphaData, wxIMAGE_ALPHA_TRANSPARENT, bwidth * bheight);
// Create an image with alpha.
wxImage image (wxSize(bwidth, bheight));
image.SetAlpha (alphaData);
wxGraphicsContext* gc = wxGraphicsContext::Create (image);
gc->SetPen (wxPen(colour, penWidth));
gc->SetBrush (wxTRANSPARENT_BRUSH);
// Do drawing here ....
// Release the graphics context.
delete gc;
// Scale the image and convert to a bitmap.
wxBitmap outBmp (image.Scale(t_width, t_height), 32);
// Blit it to the provide mdc.
mdc->DrawBitmap (outBmp, wxPoint(x,y)), true);

最新更新