Search code examples
c++winapibitblt

Win32 C++ BitBlt Transparency


UPDATED LOOK AT BOTTOM OF THIS POST What I am doing is trying to use one Black and White bitmap, to lay a background bitmap on the white, and the tile overlay on the black. The problem I am having is adding the overlay.

This is all the parts

and this is my BitBlt() code, this code produces #5.

hOldBitmap = (HBITMAP)SelectObject(hdcMem, bitmap.hbmBackground); // #2
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCCOPY); 

hOldBitmap = (HBITMAP)SelectObject(hdcMem, bitmap.hbmMap); // #1
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCAND);

hOldBitmap = (HBITMAP)SelectObject(hdcMem, bitmap.hbmMapOverlay); // #4
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCAND);   

I am unsure about using the same "hOldBitmp", but it seems to do the same thing either way.

The transparent blt function would not fully suffice here, either.

Thanks.

NEW

I have been having trouble combining and rastoring. I can somewhat handle DC's and bitmaps, but this is one thing I cant figure out how to do... Creating memory dcs, and dcs to hold a bitmap, dc for another bitmap, then bitblt to the mem. I think...

Heres my redundant code I have at the moment. Really I am needing help with pseudo code, how to combine the bitmaps... how many DC's are necessary, etc..

buffer.getBufferDC() is the main DC that displays on the screen.

HDC hdc = GetDC(hWnd);
HDC hdcMem = CreateCompatibleDC(hdc);
HDC hdcMem2 = CreateCompatibleDC(hdc);
HDC hdcMem3 = CreateCompatibleDC(hdc);
HDC hdcMem4 = CreateCompatibleDC(hdc);
HBITMAP hbmMem3 = CreateCompatibleBitmap(hdc, WINDOW_WIDTH, WINDOW_HEIGHT);
HBITMAP hbmMem4 = CreateCompatibleBitmap(hdc, WINDOW_WIDTH, WINDOW_HEIGHT);
ReleaseDC(hWnd, hdc);

// Copy the map and clean the hdcMem
HBITMAP hbmOld;
hbmOld = (HBITMAP)SelectObject(hdcMem, bitmap.hbmMap);
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCCOPY);    
SelectObject(hdcMem, hbmOld);
hbmOld = (HBITMAP)SelectObject(hdcMem2, bitmap.hbmBackground);
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem2, 0, 0, SRCAND);    
SelectObject(hdcMem2, hbmOld);     

hbmOld = (HBITMAP)SelectObject(hdcMem3, bitmap.hbmMapOverlay);
hbmOld = (HBITMAP)SelectObject(hdcMem4, bitmap.hbmMap);
BitBlt(hdcMem3, 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem4, 0, 0, SRCINVERT);

BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem3, 0, 0, SRCPAINT);     
//hbmOld = (HBITMAP)SelectObject(hdcMem, bitmap.hbmMap);
//BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCPAINT);    
//SelectObject(hdcMem, hbmOld);


//hbmOld = (HBITMAP)SelectObject(hdcMem, bitmap.hbmMapOverlay);
//BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCAND);    
//SelectObject(hdcMem, hbmOld);    

DeleteDC(hdcMem);
DeleteDC(hdcMem2);
DeleteDC(hdcMem3);
DeleteDC(hdcMem4);

Solution

  • Combine hbmpBackground with hbmMap using SRCAND, as you've done in 3.

    Combine hbmMapOverlay with an inverted hbmMap (SRCINVERT should do it).

    Combine those two results using OR (SRCPAINT)