how can I set static controls background color programmatically

て烟熏妆下的殇ゞ 提交于 2020-01-11 13:10:27

问题


I want to change label background color within a function, I tried this code but nothing changed after calling changecolor function

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;
}

I read that Static controls send their parent a WM_CTLCOLORSTATIC message just before they paint themselves. code is implemented within CALLBACK function, but where this code is called (changing color)?, how can I call SetTextColor within a function

code example :

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:


Try something more like this:

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;
}

There is a similar example in the WM_CTLCOLORSTATIC documentation.

The following C++ example shows how to set the text foreground and background colors of a static control in response to the WM_CTLCOLORSTATIC message. The hbrBkgnd variable is a static HBRUSH variable that is initialized to NULL, and stores the background brush between calls to WM_CTLCOLORSTATIC. The brush must be destroyed by a call to the DeleteObject function when it is no longer needed, typically when the associated dialog box is destroyed.

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;
    }


来源:https://stackoverflow.com/questions/32923691/how-can-i-set-static-controls-background-color-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!