Bittt blackness
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 fine. But I can see black color around this rectangle. Why is this? Here's a sample image.
source to share
The bitmap is most likely initialized to black. Then you draw a white rectangle between x-coordinates 126 and 624. Therefore, everything to the left of x = 126 and to the right of x = 624 remains black.
Edit: The documentation forCreateCompatibleBitmap
does not specify how the bitmap will be initialized, so you must explicitly initialize the bitmap with a specific color 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 a bitmap with a gray color - you may need CreateSolidBrush
your own brush if you need a different color. (Remember to call DeleteObject
when you're done.)
As a side note, I found it a little odd that your bitmap is set to a constant height of 200 - the normal thing would be to have the bitmap's height equal to the window's height (since that's for width).
source to share
On MSDN http://msdn.microsoft.com/en-us/library/dd162898.aspx :
The rectangle is indicated with the current pen and filled with the current brush.
Instead, invoke FillRect
or select an appropriate stylus before invoking Rectangle
'.
source to share