0

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:

The broken application

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)

TopchetoEU
  • 697
  • 5
  • 9

1 Answers1

0

So after looking into the question a little bit further, it seems like I was doing the backbuffer HBITMAP wrong. It should be created as compatible with the main HDC:

...
HDC device = GetDC(handle);
HDC backBufferDevice = CreateCompatibleDC(device);
RECT boundRect;

GetClientRect(handle, &boundRect);

HBITMAP backbufferBmp = CreateCompatibleBitmap(
    device, // previously was backBufferDevice
    boundRect.right - boundRect.left,
    boundRect.bottom - boundRect.top
);
...

Seems like no matter what, the second HDC is always monochrome, although it should be compatible with the first HDC, as stated here. Also, the dimension retrieval of the HDC didn't work, because the HDC's source isn't from a window. It seems like they should be retrieved in the following way:

// hdc definition

...
int width = GetDeviceCaps(hdc, HORZRES);
int height = GetDeviceCaps(hdc, VERTRES);
...

I'm sorry for asking a question, instead of looking a little bit further and not wasting someone's time.

TopchetoEU
  • 697
  • 5
  • 9