Winapi issue setting background color of button

I’m having an issue changing the color of a button in my winapi app. This is my winproc:

LRESULT CALLBACK WndProc(
    HWND hWnd,
    UINT message,
    WPARAM wParam,
    LPARAM lParam
)
{
    HWND A = NULL;
    switch (message) {
    case WM_CREATE:
        A = CreateWindow(
            L"BUTTON",
            L"A",      // Button text 
            WS_TABSTOP | WS_VISIBLE | WS_CHILD,
            0,
            0,
            (maxX) / 2,
            (maxY) / 2,
            hWnd,
            NULL,
            (HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE),
            NULL
        );
        break;
    case WM_DESTROY:
        DestroyWindow(hWnd);
        break;
    case WM_CTLCOLORBTN:
        SetBkColor(GetDC(A), RGB(255, 0, 0));
        return (INT_PTR)CreateSolidBrush(RGB(255, 0, 0));
        break;
    default:
        return DefWindowProc(
            hWnd,
            message,
            wParam,
            lParam
        );
    }
    return 0;
}

I create the button and that works fine, it shows up on the app when I test it in debug mode. The problem is it doesn’t seem to want to change color.

I saw that in the docs it says:

By default, the DefWindowProc function selects the default system colors for the button. Buttons with the BS_PUSHBUTTON, BS_DEFPUSHBUTTON, or BS_PUSHLIKE styles do not use the returned brush. Buttons with these styles are always drawn with the default system colors. Drawing push buttons requires several different brushes-face, highlight, and shadow-but the WM_CTLCOLORBTN message allows only one brush to be returned. To provide a custom appearance for push buttons, use an owner-drawn button. For more information, see Creating Owner-Drawn Controls.

so I removed the BS_DEFPUSHBUTTON from the button but it still doesn’t work.

Would someone be able to help me make the button a different color? Thanks!!

Leave a Comment