如何以编程方式设置静态控件背景颜色

时间:2015-10-03 14:29:54

标签: winapi visual-c++

我想在一个函数中更改标签背景颜色,我尝试了这段代码,但在调用changecolor函数后没有任何改变

HWND hWndLabel;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
        hWndLabel = CreateWindowEx(WS_EX_TRANSPARENT,
            L"STATIC", L"", WS_CHILD | WS_VISIBLE | SS_LEFT | WS_SYSMENU,
            75, 75, 70, 70, hWnd, (HMENU)labelId, hInst, NULL);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    case WM_COMMAND:  // all events are handled here
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }
    return 0;
}

DWORD WINAPI changecolor(){
    HDC hdc = GetDC(hWndLabel); // get context
    SetBkColor(hdc, RGB(0, 0, 230)); // Code Copied from the above answer by cpx.
    return 0;
}

我读到静态控件在绘制自己之前就向父节点发送WM_CTLCOLORSTATIC消息。代码是在CALLBACK函数中实现的,但是这个代码被调用(改变颜色)?,如何在函数中调用SetTextColor

代码示例:

case WM_CTLCOLORSTATIC:
  if (the_button_was_clicked) {
    HDC hdc = reinterpret_cast<HDC>(wParam);
    SetTextColor(hdc, COLORREF(0xFF, 0x00, 0x00));
  }
  return ::GetSysColorBrush(COLOR_WINDOW);  // example color, adjust for your circumstance

1 个答案:

答案 0 :(得分:2)

尝试更像这样的事情:

HWND hWndLabel;
HBRUSH hBrushLabel;
COLORREF clrLabelText;
COLORREF clrLabelBkGnd;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_CREATE:
        hWndLabel = CreateWindowEx(0, L"STATIC", L"", WS_CHILD | WS_VISIBLE | SS_LEFT,
            75, 75, 70, 70, hWnd, (HMENU)labelId, hInst, NULL);
        hBrushLabel = NULL;
        clrLabelText = GetSysColor(COLOR_WINDOWTEXT);
        clrLabelBkGnd = GetSysColor(COLOR_WINDOW);
        break;

    case WM_DESTROY:
        if (hBrushLabel) DeleteObject(hBrushLabel);
        PostQuitMessage(0);
        break;

    case WM_CTLCOLORSTATIC: {
        HDC hdc = reinterpret_cast<HDC>(wParam);
        SetTextColor(hdc, clrLabelText);
        SetBkColor(hdc, clrLabelBkGnd);
        if (!hBrushLabel) hBrushLabel = CreateSolidBrush(clrLabelBkGnd);
        return reinterpret_cast<LRESULT>(hBrushLabel);
    }

    case WM_COMMAND:  // all events are handled here
        break;

    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }
    return 0;
}

DWORD WINAPI changecolor()
{
    if (hBrushLabel) {
        DeleteObject(hBrushLabel);
        hBrushLabel = NULL;
    }
    clrLabelText = RGB(0xFF, 0x00, 0x00);
    clrLabelBkGnd = RGB(0, 0, 230);
    InvalidateRect(hWndLabel, NULL, TRUE);
    return 0;
}

WM_CTLCOLORSTATIC documentation中有一个类似的例子。

  

以下C ++示例显示如何设置静态控件的文本前景色和背景色以响应WM_CTLCOLORSTATIC消息。 hbrBkgnd变量是一个静态HBRUSH变量,初始化为NULL,并在对WM_CTLCOLORSTATIC的调用之间存储背景画笔。必须通过在不再需要时调用DeleteObject函数来销毁画笔,通常是在关联的对话框被销毁时。

case WM_CTLCOLORSTATIC:
    {
    HDC hdcStatic = (HDC) wParam;
    SetTextColor(hdcStatic, RGB(255,255,255));
    SetBkColor(hdcStatic, RGB(0,0,0));

    if (hbrBkgnd == NULL)
    {
        hbrBkgnd = CreateSolidBrush(RGB(0,0,0));
    }
    return (INT_PTR)hbrBkgnd;
    }