Question

I recently asked a question about this, and understood the answer, but couldn't translate it into code. After another day of messing around with stuff, and fixing the leaks. I literally cannot for the life of me figure this out, though.

This is slightly different, all I need to do is get the background bitmap, under the map bitmap.What I have

HDC hdc = GetDC(hWnd);
HDC hdcMem = CreateCompatibleDC(hdc);
HDC hdcMem2 = CreateCompatibleDC(hdc);
ReleaseDC(hWnd, hdc);

HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, bitmap.hbmBackground);
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem, 0, 0, SRCCOPY);       

HBITMAP hbmOld2 = (HBITMAP)SelectObject(hdcMem2, bitmap.hbmMap);
BitBlt(buffer.getBufferDC(), 1, 1, WINDOW_WIDTH, WINDOW_HEIGHT, hdcMem2, 0, 0, SRCPAINT); 

SelectObject(hdcMem2, hbmOld2);

My problem is combining... creating memory dcs to keep results of raster operations. I can not at all wrap my head around this, any help would be great.

Thanks.

Was it helpful?

Solution

Probably the most difficult part of this is the rectangle in the middle. The other white areas in the mask are the places the background should show through, but inside that rectangle you have white where the background should not show through.

For the moment, I'm going to assume we can start with those sections being different colors. It doesn't really matter what color we use for the transparent part (the part where the background should show) as long as it's unique. For now I'll assume it's an RGB value for 254, 254, 254--code won't mistake it for pure white (255, 255, 255), but usually not visually distinguishable.

With that, the job becomes quite simple: we can use TransparentBlt to handle the transparency, so you blit your background to a destination with BitBlt, then blit your mask to that destination using TransparentBlt. Simplifying a bit (leaving out parameters we don't care much about for the moment) the code looks something like this:

HDC mask, background, combined;

mask =       CreateCompatibleDC(screen);
background = CreateCompatibleDC(screen);
combined =   CreateCompatibleDC(screen);

HBITMAP mask_bmp = LoadBitmap(MAKEINTRESOURCE(IDB_MASK));
HBITMAP back_bmp = LoadBitmap(MAKEINTRESOURCE(IDB_BKGND));
HBITMAP result = CreateCompatibleBitmap(screen);

mask_original = Selectobject(mask, mask_bmp);
back_original = SelectObject(background, back_bmp);
combined_original = SelectObject(combined, result);

BitBlt(background, result, SRCCOPY);
TransparentBlt(mask, result, RGB(254, 254, 254));

BitBlt(result, screen, SRCCOPY);

SelectObject(mask, mask_original);
SelectObject(background, back_original);
SelectObject(combined, combined_original);

DeleteDC(mask);
DeleteDC(background);
DeleteDC(combined):
DeleteObject(result);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top