我有分层窗口(WS_EX_LAYERED
),我正在尝试使用UpdateLayeredWindow
在窗口上显示位图。
位图是不规则形状,背景色为RGB(30, 30, 30)
。我想使用ULW_COLORKEY
选项使背景透明。
当我致电UpdateLayeredWindow
时,图像会在屏幕上呈现,但背景不透明。
// Draw background rectangle
COLORREF transparent_colour = RGB(30, 30, 30);
HBRUSH hTranparentBrush = CreateSolidBrush(transparent_colour);
HBRUSH hOldBrush = SelectObject(hSrcDC, hTransparentBrush);
// the rectangle is the size of the splash screen / layered window
hresult = Rectangle(hSrcDC, 0, 0, szSplash.cx, szSplash.cy);
//error checking has been removed
// Paint rotated bitmap over background
hresult = SetGraphicsMode(hSrcDC, GM_ADVANCED);
float s = math::sin(degree * pi / 180);
float c = math::cos(degree * pi / 180);
XFORM x;
x.em11 = c;
x.em12 = s;
x.em21 = -s;
x.em22 = c;
x.eDx = (1-c)*szImg.cx + s*szImg.cy;
x.eDy = (1-c)*szImg.cy - s*szImg.cx;
hresult = SetWorldTransform(hSrcDC, x);
// the BitBlt below succeeds. hSrcDC contains a bitmap with a solid
// background and the rotated image over it.
hresult = BitBlt(hSrcDC, 100, 100, szImg.cx, szImg.cy, hImgDC, 0, 0, SRCCOPY);
// Print to screen
POINT ptZero = {0, 0};
SIZE szBmp = szSplash; //size of the splash screen
BLENDFUNCTION blend;
blend.BlendOp = ARC_SRC_OVER;
blend.BlendFlags = 0;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
//this call succeeds - the bitmap is printed to the screen, but the
//background is still visible?
hresult = UpdateLayeredWindow(hwndSplash,
hSplashDC,
&ptZero,
&szBmp,
hSrcDC,
&ptZero,
transparent_colour,
&blend,
ULW_COLORKEY);
如果我将ULW_COLORKEY
替换为ULW_ALPHA
,则图像的背景和其他部分都会变得透明,看起来很糟糕。我希望背景透明 - 有什么我想念的吗?