首页 > 解决方案 > WM_CTLCOLORSTATIC 永远不会在 WIN32 应用程序中触发

问题描述

我正在用 C++ 做一个 Win32 应用程序。我有一个静态窗口,我想更改背景的颜色。

我遵循了这里的建议:如何以编程方式设置静态控件背景颜色

但在我的情况下,WM_CTLCOLORSTATIC 永远不会被触发。你知道会发生什么吗?

这是我的一些代码:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            case WM_CTLCOLORSTATIC:
                hdc = reinterpret_cast<HDC>(wParam);
                SetTextColor(hdc, RGB(2000, 50, 100));
                SetBkColor(hdc, RGB(20, 150, 100));
                if (!hBrushLabel) hBrushLabel = CreateSolidBrush(RGB(20, 150, 100));
                return reinterpret_cast<LRESULT>(hBrushLabel);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_CREATE:
        hBrushLabel = NULL;
        AddBuyButtons(hWnd);
        AddText(hWnd);
        AddCartonesSlots(hWnd);
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        if (hBrushLabel) DeleteObject(hBrushLabel);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

我检查过的更改功能称为:

DWORD WINAPI changecolor(HWND h)
{
    if (hBrushLabel) {
        DeleteObject(hBrushLabel);
        hBrushLabel = NULL;
    }
    InvalidateRect(h, NULL, TRUE);
    return 0;
}

希望你能帮我!

标签: c++winapidesktop

解决方案


WM_CTLCOLORSTATIC是它自己的窗口消息,但您正在处理它,就好像它是WM_COMMAND消息的菜单 ID。您需要将case WM_CTLCOLORSTATIC处理程序从内部switch块移动到外部switch块:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            }
        }
        break;

    // MOVED HERE!!!
    case WM_CTLCOLORSTATIC:
        hdc = reinterpret_cast<HDC>(wParam);
        SetTextColor(hdc, RGB(2000, 50, 100));
        SetBkColor(hdc, RGB(20, 150, 100));
        if (!hBrushLabel) hBrushLabel = CreateSolidBrush(RGB(20, 150, 100));
        return reinterpret_cast<LRESULT>(hBrushLabel);
        break;

    case WM_CREATE:
        hBrushLabel = NULL;
        AddBuyButtons(hWnd);
        AddText(hWnd);
        AddCartonesSlots(hWnd);
        break;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
            EndPaint(hWnd, &ps);
        }
        break;

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

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

    return 0;
}

推荐阅读