C++ Win32 改变静态颜色

标签 c++ windows winapi

<分区>

我想创建标签并更改其背景颜色和文本颜色

我用这个

HWND hwnder = CreateWindow(TEXT("static"), TEXT(text), WS_VISIBLE | WS_CHILD | SS_LEFT, 10, 10, 50, 60, hwnd, (HMENU) NULL, NULL, NULL);
HDC hdcStatic = GetDC(hwnder);
SetTextColor(hdcStatic, RGB(12,34,210));
SetBkColor(hdcStatic, RGB(12,34,210));

标签已创建但其颜色未更改,有什么办法可以解决这个问题?

最佳答案

处理 WM_CTLCOLORSTATIC WndProc 函数中的消息:

case WM_CTLCOLORSTATIC:
{
    HDC hdcStatic = (HDC)wParam; // or obtain the static handle in some other way
    SetTextColor(hdcStatic, RGB(255, 0, 0)); // text color
    SetBkColor(hdcStatic, RGB(12,34,210));
    return (LRESULT)GetStockObject(NULL_BRUSH);
}
break;

如果您只想更改特定组件的颜色,则使用简单的 if 语句获取目标的句柄:

case WM_CTLCOLORSTATIC:
{
    if (GetDlgCtrlID((HWND)lParam) == IDC_STATIC1) // Target the specific component
    {
        // same as above
    }
}
break;

关于C++ Win32 改变静态颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45653034/

相关文章:

c++ - 从 end() 函数返回的迭代器获取 BST 的最后一个节点?

windows - 警告 - 运行 wevtutil 时无法访问提供程序资源

c# - 我最快什么时候可以关闭窗口?

c - 使用 golang 的 Windows Hook

c - 如何在 Windows 下以编程方式确定驱动器是否为 DVD-RW/CD-RW?

c++ - 无法让 Windows 覆盖图标在 TListView 中工作

c++ - 使用 union 对 IP 地址进行多种解释?

c++ - 自定义对象的常量表达式

c++ - C++14 中的 "constexpr"

Windows Azure : how to measure the execution time of a code