Search code examples
c++winapivisual-c++toolbar

WinAPI ToolBar Text Color


I am currently writing a program that makes use of the WinAPI ToolBar to display a menu at the top of the Window. Using the DarkMode_Explorer style

SetWindowTheme(Script, L"DarkMode_Explorer", nullptr);

I am making the ToolBar's background turn grey for dark mode. However, the ToolBar button text still remains black. How can I change the color of this to red, or green, or white?

Here is a picture of the ToolBar in its current state

As you can see, it is all black. I want the text to be a different color (red, green, white, purple, should all be possibilities). I cannot find a way to do this at all, read the docs, nothing.


Solution

  • I figured it out. You should be able to handle it in WM_NOTIFY->NM_CUSTOMDRAW. Set the LPNMTBCUSTOMDRAW clrText (text color) to the desired color. You can specify font there as well.

    Color toolbar

    case WM_NOTIFY:
    {
        switch (lpnm->code)
        {
            case NM_CUSTOMDRAW:
            {
                LPNMTBCUSTOMDRAW data_ptr = (LPNMTBCUSTOMDRAW)lParam;
                if (data_ptr->nmcd.hdr.hwndFrom == ToolBar) {
                    switch (data_ptr->nmcd.dwDrawStage)
                    {
                        case CDDS_ITEMPREPAINT: {
                            SelectObject(data_ptr->nmcd.hdc, GetFont(L"Microsoft Sans Serif", 15));
                            FillRect(data_ptr->nmcd.hdc, &data_ptr->nmcd.rc, CreateSolidBrush(RGB(44, 44, 44)));
                            data_ptr->clrText = RGB(228, 228, 228);
                            return CDRF_NEWFONT;
                        }
                        case CDDS_PREPAINT:
                        {
                            return CDRF_NOTIFYITEMDRAW;
                        }
                    }
                }
            }
        }
    }
    

    Make sure to set your Toolbar's theme to remove visual styles, or it won't work.

    SetWindowTheme(ToolBar, L"", L"");