I have read an article about backbuffering, which I have recreated in the following way:
void paint(HWND handle)
HDC device = GetDC(handle);
HDC backBufferDevice = CreateCompatibleDC(device);
RECT boundRect;
GetClientRect(handle, &boundRect);
HBITMAP backbufferBmp = CreateCompatibleBitmap(
backBufferDevice,
boundRect.right - boundRect.left,
boundRect.bottom - boundRect.top
);
SelectObject(backBufferDevice, backbufferBmp);
// This is in a separate function void clear(HDC device)
RECT rect{ 0, 0, 300, 300 };
GetClientRect(WindowFromDC(backBufferDevice), &rect);
FillRect(hdc, &rect, br);
BitBlt(
device,
boundRect.left, boundRect.top,
boundRect.right - boundRect.left, boundRect.bottom - boundRect.top,
backBufferDevice,
0, 0,
SRCCOPY
);
DeleteObject(backbufferBmp);
DeleteDC(backBufferDevice);
ReleaseDC(handle, device);
}
LRESULT CALLBACK ProcessMessage(
HWND handle,
unsigned int message,
WPARAM wParam,
LPARAM lParam
) {
switch (message) {
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
case WM_MOUSEMOVE:
paint(handle);
return 0;
case WM_ERASEBKGND:
return 1;
default:
return DefWindowProc(handle, message, wParam, lParam);
}
}
void main() {
// ... window initialiation
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
But when I start the application, the following happens:
What should happen is that the window should be cleared in RGB(20, 20, 20). It seems like it is in black and white mode and the WindowFromDC() failed. The second is unsurprising, but why is the device in black and white and how should I fix it?
(Sorry for bad English)