Win32 C ++ Alphablend位图部分透明

时间:2014-02-19 04:56:16

标签: c++ winapi alphablending

我用Google搜索,看到示例,其他问题,MSDN和下载示例代码。我无法弄清楚这有什么问题。

// setting up the memory DC and selecting in the bitmap
HDC hdc = GetDC(hWnd);
HDC hdcMem = CreateCompatibleDC(hdc);
ReleaseDC(hWnd, hdc);
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, bitmap.hbmLogo);

// setting up the blend function
BLENDFUNCTION bStruct; 
bStruct.BlendOp = AC_SRC_OVER;
bStruct.BlendFlags = 0;
bStruct.SourceConstantAlpha = 255;
bStruct.AlphaFormat = AC_SRC_ALPHA;

// try
BOOL check = AlphaBlend(buffer.getBufferDC(), 0, 0, bitmap.bmLogo.bmWidth, bitmap.bmLogo.bmHeight, hdcMem, 0, 0, bitmap.bmLogo.bmWidth, bitmap.bmLogo.bmHeight, bStruct);
if (check == FALSE) MessageBox(0,0,0,0);

// this is how I load the bitmap, it is a resource. 
bitmap.hbmLogo = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_LOGO_0));
if (bitmap.hbmLogo == NULL) { MessageBox(NULL, "Could not read the logo bitmap.", "Error", MB_OK); return false; }
GetObject(bitmap.hbmLogo, sizeof(bitmap.bmLogo), &bitmap.bmLogo);

我使用消息框快速检查结果。检查始终返回TRUE。位图及其尺寸是正确的。

我已尝试过不同的背景颜色,alpha值,但仍然没有,用BitBlt或TransparentBitBlt替换它,没问题,徽标显示。我使用AlphaBlend函数的所有尝试都没有改变。即使是一秒钟,屏幕上也不会出现徽标。

有什么想法吗?

感谢。

Here is the bitmap.

1 个答案:

答案 0 :(得分:1)

仔细观察一个例子后找到解决方案。

我将BLENDFUNCTION设置为全局,并在我使用的WM_CREATE消息中:

    m_bf.BlendOp = AC_SRC_OVER;
    m_bf.BlendFlags = 0;
    m_bf.SourceConstantAlpha = 100; // any 0 to 255
    m_bf.AlphaFormat = 0;
    LoadBitmapsFromResource();

现在正在运作。