已解决: 当前代码示例:
HBITMAP TwoBitmap(HDC &hdc, RECT &rc)
{
HDC hdcmem = CreateCompatibleDC(hdc);
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left, rc.bottom-rc.top);
SelectObject(hdcmem, hbitmap);
HBRUSH hBrush = GetSysColorBrush(2);
FillRect(hdcmem, &rc, hBrush);
DeleteObject((HBRUSH)hBrush);
DeleteDC(hdcmem);
return hbitmap;
}
void CreateTransparentBitmap(HBITMAP &hBitmap)
{
BITMAP TemporaryBitmap;
GetObject(hBitmap, sizeof(BITMAP), &TemporaryBitmap);
HDC MemoryDC = CreateCompatibleDC(NULL);
BITMAPINFO BitmapInfo;
ZeroMemory(&BitmapInfo,sizeof(BITMAPINFO));
BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BitmapInfo.bmiHeader.biWidth = TemporaryBitmap.bmWidth;
BitmapInfo.bmiHeader.biHeight = TemporaryBitmap.bmHeight;
BitmapInfo.bmiHeader.biPlanes = 1;
BitmapInfo.bmiHeader.biBitCount = 32;
BitmapInfo.bmiHeader.biCompression = BI_RGB;
RGBQUAD* pArgb = NULL;
HBITMAP hTemporaryBitmap = CreateDIBSection(MemoryDC, &BitmapInfo, DIB_RGB_COLORS, (void**)&pArgb, NULL, 0);
GetDIBits(MemoryDC, hBitmap, 0, BitmapInfo.bmiHeader.biHeight, pArgb, &BitmapInfo, DIB_RGB_COLORS);
for (int i = 0; i < BitmapInfo.bmiHeader.biWidth * BitmapInfo.bmiHeader.biHeight; i++)
pArgb[i].rgbReserved = 255;
DeleteDC(MemoryDC);
DeleteObject(hBitmap);
hBitmap = hTemporaryBitmap;
}
case WM_PAINT:
{
PAINTSTRUCT p;
HDC hDC = BeginPaint(hWnd, &p);
HDC hdcmem = CreateCompatibleDC(hDC);
HBITMAP hImage = TwoBitmap(hDC, p.rcPaint);
CreateTransparentBitmap(hImage);
SelectObject(hdcmem, hImage);
BLENDFUNCTION bfn;
bfn.BlendOp = AC_SRC_OVER;
bfn.BlendFlags = 0;
bfn.SourceConstantAlpha = 10;
bfn.AlphaFormat = AC_SRC_ALPHA;
AlphaBlend(hDC, 0, 0, 100, 64, hdcmem, 0, 0, 100, 64, bfn);
bfn.SourceConstantAlpha = 80;
AlphaBlend(hDC, 200, 0, 100, 64, hdcmem, 0, 0, 100, 64, bfn);
bfn.SourceConstantAlpha = 255;
AlphaBlend(hDC, 400, 0, 100, 64, hdcmem, 0, 0, 100, 64, bfn);
BitBlt(hDC, 0, 100, 100, 64, hdcmem, 0, 0, SRCCOPY);
DeleteDC(hdcmem);
DeleteObject(hImage);
EndPaint(hWnd, &p);
}
break;
对于这个例子,我有一个使用 CreateCompatibleBitmap() 创建的简单位图:
HBITMAP TwoBitmap(HDC &hdc, RECT &rc)
{
HDC hdcmem = CreateCompatibleDC(hdc);
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left,
rc.bottom-rc.top);
SelectObject(hdcmem, hbitmap);
HBRUSH hBrush = GetSysColorBrush(3);
FillRect(hdcmem, &rc, hBrush);
DeleteObject((HBRUSH)hBrush);
DeleteDC(hdcmem);
return hbitmap;
}
现在,我想要实现的是向这个位图添加 alpha 通道(如果它还没有)。使用下面的代码,我尝试创建一个 32 位位图,然后将旧位图复制到这个新位图中。最后,我使用 AlphaBlend() 和 BitBlt() 函数将新的位图打印到屏幕上,像往常一样,我得到单色 alpha 混合图像和完美的 BitBlt 图像。
所以我的问题是——我错过了什么,做错了什么?我很迷失在这里。我的代码(不要介意 GDI 泄漏):
编辑:
感谢PhoenixX_2,我能够解开这个谜团,我所需要的只是将 Alpha 位设置为 255。下面是我的代码,请记住它不是无泄漏的。
PAINTSTRUCT p;
HDC hDC = BeginPaint(hWnd, &p);
HDC BufferDC = CreateCompatibleDC(NULL);
BITMAPINFO BufferInfo;
ZeroMemory(&BufferInfo,sizeof(BITMAPINFO));
BufferInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BufferInfo.bmiHeader.biWidth = p.rcPaint.right-p.rcPaint.left;
BufferInfo.bmiHeader.biHeight = p.rcPaint.bottom-p.rcPaint.top;
BufferInfo.bmiHeader.biPlanes = 1;
BufferInfo.bmiHeader.biBitCount = 32;
BufferInfo.bmiHeader.biCompression = BI_RGB;
RGBQUAD* pArgb;
HBITMAP BufferBitmap = CreateDIBSection(BufferDC, &BufferInfo, DIB_RGB_COLORS,
(void**)&pArgb, NULL, 0);
HBITMAP hImage = TwoBitmap(hDC, p.rcPaint);
GetDIBits(BufferDC, hImage, 0, BufferInfo.bmiHeader.biHeight, pArgb,
&BufferInfo, DIB_RGB_COLORS);
//----------------------------------the part I was missing
for (int i = 0; i < BufferInfo.bmiHeader.biWidth
* BufferInfo.bmiHeader.biHeight; i++)
pArgb[i].rgbReserved = 255;
//----------------------------------
SelectObject(BufferDC, BufferBitmap);
BLENDFUNCTION bfn;
bfn.BlendOp = AC_SRC_OVER;
bfn.BlendFlags = 0;
bfn.SourceConstantAlpha = 10;
bfn.AlphaFormat = AC_SRC_ALPHA;
AlphaBlend(hDC, 0, 0, 100, 64, BufferDC, 0, 0, 100, 64, bfn);
bfn.SourceConstantAlpha = 80;
AlphaBlend(hDC, 200, 0, 100, 64, BufferDC, 0, 0, 100, 64, bfn);
bfn.SourceConstantAlpha = 255;
AlphaBlend(hDC, 400, 0, 100, 64, BufferDC, 0, 0, 100, 64, bfn);
BitBlt(hDC, 0, 100, 100, 64, BufferDC, 0, 0, SRCCOPY);
DeleteDC(BufferDC);
DeleteObject((HBITMAP)BufferBitmap);
EndPaint(hWnd, &p);