tags:

views:

325

answers:

4

I am running this following code,

HDC hdc;
HDC hdcMem;
HBITMAP bitmap;
RECT c;
GetClientRect(viewHandle, &c);
// instead of BeginPaint use GetDC or GetWindowDC
hdc = GetDC(viewHandle); 
hdcMem = CreateCompatibleDC(hdc); 
// always create the bitmap for the memdc from the window dc
bitmap = CreateCompatibleBitmap(hdc,c.right-c.left,200);

SelectObject(hdcMem, bitmap);

// only execute the code up to this point one time
// that is, you only need to create the back buffer once
// you can reuse it over and over again after that

// draw on hdcMem
// for example  ...
Rectangle(hdcMem, 126, 0, 624, 400);

// when finished drawing blit the hdcMem to the hdc
BitBlt(hdc, 0, 0, c.right-c.left,200, hdcMem, 0, 0, SRCCOPY);

// note, height is not spelled i before e

// Clean up - only need to do this one time as well
DeleteDC(hdcMem);
DeleteObject(bitmap);
ReleaseDC(viewHandle, hdc);

The code is just fine. But I am seeing black color around this rectangle. Why is that? Here is an example image.

A: 

Per MSDN http://msdn.microsoft.com/en-us/library/dd162898.aspx:

The rectangle is outlined by using the current pen and filled by using the current brush.

Consider calling FillRect instead, or select an appropriate pen prior to calling Rectangle'.

binarycoder
The same thing happens even if draw a line. I think the problem is with BitBlt
Vadi
+4  A: 

The bitmap is most likely initialized to be all black. You are then drawing a white rectangle that between x-coordinates 126 and 624. Hence, everything to the left of x=126 and to the right of x=624 stays black.

Edit: The documentation for CreateCompatibleBitmap doesn't state how the bitmap will be initialized, so you should explicitly initialize the bitmap with a specific colour, as Goz suggests, using FillRect:

RECT rc;

rc.left=0;
rc.top=0;
rc.right=c.right-c.left;
rc.bottom=200;

FillRect(hdcMem, &rc, (HBRUSH)GetStockObject(GRAY_BRUSH));

This example fills the bitmap in gray -- you may need to CreateSolidBrush your own brush if you need a different colour. (Don't forget to call DeleteObject when you're done.)

As a side note, I find it a bit strange that your bitmap is being set to a constant height of 200 -- the normal thing would be to make the height of the bitmap equal to the height of the window (as is done for the width).

Martin B
how to initialize the bitmap with a specified color?
Vadi
Edited the answer to show how to initialize the bitmap.
Martin B
Thanks Martin, It works. Is this is the right way? I want to understand the reason why we need to do FillRect. Can you please explain in your answer.
Vadi
This is necessary simply because you don't know what the initial contents of the bitmap created by `CreateCompatibleBitmap` will be; at least the MSDN documentation doesn't specify what the initial contents of the bitmap will be, so you should initialize it to have defined behaviour.
Martin B
+1  A: 

Might it be because you haven't initialised the memory bitmap area to a given colour? Try FillRect'ing the background to a different colour then draw your white rectangle over it and see what happens.

Goz
A: 

I used:

 // Fill the background
 hdcMem->FillSolidRect(c, hdcMem->GetBkColor());

Just as a note.

Matt Clarkson