3

Doing a project in win32 in c++, attempting to double buffer the image being drawn, but I'm getting a black screen with the correct bitmaps drawn over it. This is also causing my WM_MOUSEMOVE condition, which drags a bitmap along with your cursor to not draw the bitmap. Code for paint is below: paint() is called in wndproc under WM_PAINT, scroll is the position of the scroll bar, unused so far.

int paint(HWND hWnd, HINSTANCE hInst, RECT clientRect, std::vector<Measure> *measures, int scroll)
{
int x = 90;
hdc = BeginPaint(hWnd, &ps);
hdcmem = CreateCompatibleDC(hdc);
HBITMAP hbmScreen = CreateCompatibleBitmap(hdc, clientRect.right, clientRect.bottom);
SelectObject(hdcmem,hbmScreen); 
/*these functions just create the bitmaps into hdcmem*/
drawStaff(hWnd, hInst, clientRect, x, 0);
drawKey(hWnd, hInst, clientRect, x, (*measures)[0], 0);
drawTime(hWnd, hInst, clientRect, x, (*measures)[0], 0);
drawNotes(hWnd, hInst, clientRect, measures, x);
    BitBlt(hdc, 0, 0, clientRect.right, clientRect.bottom, hdcmem, 0, 0, SRCCOPY);
ReleaseDC(hWnd, hdcmem);
return 0;
}
Aaron K
  • 437
  • 4
  • 10
  • 3
    you shouldn't remake the backbuffer ever time you paint, just make it once on `WM_CREATE` (and `WM_SIZE`) and attach it to the `HWND` with `SetWindowLongPtr(GWLP_USERDATA)`. – Necrolis Jan 16 '12 at 07:23
  • 3
    You have a resource leak - you are forgetting to call DeleteObject on the hdcmem before returning from this function. – selbie Jan 16 '12 at 08:23
  • 3
    You are also failing to call EndPaint. – selbie Jan 16 '12 at 08:26

1 Answers1

3

You need to fill the bitmap with whatever your background color is first before drawing your other graphics. If memory serves me correctly, bitmaps are filled with black by default when they are created.

hypercode
  • 488
  • 2
  • 10
  • I tried adding in the following code after selecting the object into hdcmem: SetBkColor(hdcmem, COLORREF RGB(0, 0, 0)); which is believe is white, but I also tried it with 255 instead of 0, and then various other numbers, none of which changed the image I was getting. Is this what you mean by changing the background of the bitmap, or does is that different from changing the background of the control for the bitmap? – Aaron K Jan 16 '12 at 03:29
  • 7
    You actually have to fill with the color you want, say via `FillRect`. `SetBkColor` sets the background color for text, but you don't appear to draw any text so the call has no effect. – Raymond Chen Jan 16 '12 at 03:42
  • 2
    @AaronK - RGB(0,0,0) is solid black, not white! – selbie Jan 16 '12 at 08:25