Sign in to follow this  
Noods

Bitblt-ing a background color...

Recommended Posts

I have implemented a tear-free bitblt operation using double buffering. However, my background color controls aren't working. I have attempted to change the background color during the pre-bitblt buffer as I have seen others implement it. However, FillRect() isn't working. It isn't updating the background at all. Can someone take a look at my code and tell me why this isn't working properly? Thanks!
case WM_PAINT:
	BITMAP bm;                                  //Bitmap object for drawing
	HBITMAP bitmap;
	hdc          = BeginPaint(hWnd, &psDraw);   //Get the window device context
	hdcBufferOne = CreateCompatibleDC(hdc);     //Create a memory device context compatible with the window
	hdcMem       = CreateCompatibleDC(hdc);     //Create a memory device context compatible with the window
	bitmap       = CreateCompatibleBitmap(hdc,Image.GetImageWidth(),Image.GetImageHeight());
	hOld         = SelectObject(hdcBufferOne, bitmap);   //Move the mask to the memory device context and store the information from hdcMem

	FillRect(hdcBufferOne, &rcWholeScreen, CreateSolidBrush(RGB(fScrollPosition[SCROLLRED], fScrollPosition[SCROLLGREEN], fScrollPosition[SCROLLBLUE])));
	GetObject(Image.GetBitmapHandle(), sizeof(bm), &bm);   //Move the stored image to the bitmap object
	BitBlt(hdcMem, STARTOFDRAWINGFIELD, TOOLBAROFFSET, bm.bmWidth, bm.bmHeight, hdcBufferOne, iHorizontalScroll, iVerticalScroll, SRCAND);   //Copy the memory device context (mask) to the window

	SelectObject(hdcBufferOne, Image.GetBitmapHandle());   //Move the image to the memory device context
	BitBlt(hdcMem, STARTOFDRAWINGFIELD, TOOLBAROFFSET, bm.bmWidth, bm.bmHeight, hdcBufferOne, iHorizontalScroll, iVerticalScroll, SRCPAINT);   //Copy the memory device context (image) to the window for the whole image
	BitBlt(hdc, STARTOFDRAWINGFIELD, TOOLBAROFFSET, bm.bmWidth, bm.bmHeight, hdcBufferOne, iHorizontalScroll, iVerticalScroll, SRCCOPY);   //Copy the memory device context (image) to the window for the whole image

	SelectObject(hdcBufferOne, hOld);   //Move the mask to the memory device context
	DeleteDC(hdcBufferOne);             //Delete the memory device context
	DeleteDC(hdcMem);             //Delete the memory device context
	EndPaint(hWnd, &psDraw);      //Mark the end of painting
	break;

Share this post


Link to post
Share on other sites
I added checks to both of the return values. They both test properly.


HBRUSH test = CreateSolidBrush(RGB(fScrollPosition[SCROLLRED], fScrollPosition[SCROLLGREEN], fScrollPosition[SCROLLBLUE]));
if(!test)
{
MessageBox(NULL, "Oh snap!", "The program has encountered a problem", MB_ICONERROR );
}

int test2 = FillRect(hdcBufferOne, &rcWholeScreen, test);

if(!test2)
{
MessageBox(NULL, "Oh snap again!", "The program has encountered a problem", MB_ICONERROR );
}

Share this post


Link to post
Share on other sites
Doesn't the last BitBlt uses the wrong source DC? Seems like it should be hdcMem rather than hdcBufferOne, because that just straight copies the image. You'll definitely also need to select something into hdcMem before the first BltBlt or it's contents will stay all black. The 'bitmap' variable is also leaked.

Share this post


Link to post
Share on other sites
A little off topic - that's horribly inefficient; you shouldn't be creating and destroying two device contexts, a bitmap and a brush for every WM_PAINT, you should create them at startup and destroy them at cleanup.
You're also not storing and restoring the handle returned by SelectObject(), which is another resource leak.

As for the actual problem, you seem to be writing into hdcMem in the first two BitBlt calls, but you don't use that result, and just delete the DC. The only writes to the DC shown on the window is in the last BitBlt line; a straight copy from hdcBufferOne.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this