简体   繁体   English

WinAPI 工具栏文本颜色

[英]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.我目前正在编写一个程序,该程序利用 WinAPI 工具栏在 Window 的顶部显示菜单。 Using the DarkMode_Explorer style使用 DarkMode_Explorer 风格

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?如何将其颜色更改为红色、绿色或白色?

这是 ToolBar 当前状态的图片

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.我根本找不到这样做的方法,阅读文档,什么都没有。

I figured it out.我想到了。 You should be able to handle it in WM_NOTIFY->NM_CUSTOMDRAW.您应该能够在 WM_NOTIFY->NM_CUSTOMDRAW 中处理它。 Set the LPNMTBCUSTOMDRAW clrText (text color) to the desired color.将 LPNMTBCUSTOMDRAW clrText(文本颜色)设置为所需的颜色。 You can specify font there as well.您也可以在那里指定字体。

颜色工具栏

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.确保将工具栏的主题设置为删除视觉 styles,否则将无法正常工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM