0

I try to change the default color of the button in my UI.

Here is my CallBacl function

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
    case WM_COMMAND:
        switch (wp)
        {
        case FILE_MENU_NEW:
            MessageBeep(MB_ICONINFORMATION);
            break;
        }
        break;
    case WM_CREATE:
        AddMenus(hWnd);
        AddControls(hWnd);
        break;

    case WM_CTLCOLOREDIT:
    {
        if (hEdit == (HWND)lp)
        {
            HDC hdcEdit = (HDC)wp;
            SetTextColor(hdcEdit, RGB(255, 255, 255));
            SetBkColor(hdcEdit, RGB(48, 56, 66));
            return (INT_PTR)hBrush1;
        }
    }

    case WM_CTLCOLORBTN:
    {
        if (hButton_ok == (HWND)lp)
        {
            HDC hdcButton_ok = (HDC)wp;
            SetTextColor(hdcButton_ok, RGB(255, 255, 255));
            SetBkColor(hdcButton_ok, RGB(87, 102, 110));
            return (INT_PTR)hBrush;
        }
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProcW(hWnd, msg, wp, lp);
    }
}

Now case WM_CTLCOLOREDIT changes the background color of my text input dialog correctly, Why is then WM_CTLCOLORBTN fails?

Joseph Willcoxson
  • 5,853
  • 1
  • 15
  • 29
  • 3
    What is style of button. Documentation 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. – Joseph Willcoxson May 12 '21 at 16:48
  • Thanks! The style of button... Hmm. Here is the function creating it: ``` hButton_ok = CreateWindowW(L"Button", L"Ok", WS_VISIBLE | WS_CHILD, 20, 235, 80, 27, hWnd, (HMENU)CHANGE_TITLE, NULL, NULL); ``` That is the function creating the button. Do you by chance now where I can find some minimal short example with the code creating owner-drawn button? – Yaroslav Rodionov May 12 '21 at 20:37
  • If your button is using visual styles it won't send `WM_CTLCOLORBTN`, since the rendering is handled entirely by the style. – Jonathan Potter May 12 '21 at 21:09
  • Ok, I changed WS_VISIBLE | WS_CHILD | to WS_VISIBLE | WS_CHILD | BS_OWNERDRAW . It changed the color of the button but the text disappeared. As far as I understand it will send only WM_DRAWITEM ? But nope. Just checked. It responds to WM_CTLCOLORBTN. But the text on the button disappeared! – Yaroslav Rodionov May 12 '21 at 21:16
  • Is there a way to change the background color and text color without resorting to the ultimate evel of BS_OWNERDRAW style? – Yaroslav Rodionov May 12 '21 at 22:03
  • 1
    Maybe you can refer to [How can I change the background color of a button WinAPI C++](https://stackoverflow.com/questions/18745447/how-can-i-change-the-background-color-of-a-button-winapi-c) – Zeus May 13 '21 at 06:16

0 Answers0