如何设置列表框的背景颜色

how to set a listbox background color

我不知道如何设置 ListBox 背景颜色,而不只是一个项目。

g_hChatEdit(listbox)

代码:

case WM_MEASUREITEM:
    if ((UINT)wParam == IDE_CHATEDIT) {
        LPMEASUREITEMSTRUCT lpmis = (LPMEASUREITEMSTRUCT)lParam;
        lpmis->itemHeight = 22;
        HBRUSH hbr = CreateSolidBrush(bkgrgb);
        SetWindowLong(g_hChatEdit, GCL_HBRBACKGROUND, (LONG)hbr);
        //BeginPaint(g_hChatEdit, &ps);
        hdc=GetDC(g_hChatEdit);
        RECT rc;
        GetClientRect(g_hChatEdit, &rc);
        FillRect(hdc, &rc, hbr);
        InvalidateRect(g_hChatEdit, 0, true);
        UpdateWindow(g_hChatEdit);
        return true;
    }
    break;

列表框背景颜色仍然是白色

根据此板

How to change background color of a list box? @ codeguru

您需要父级 window 拦截 WM_CTLCOLORthese variant 消息之一,以便为控件提供所需的颜色。

看看这个是否有效。请注意 hbrBkgnd 不应是临时变量。

HBRUSH hbrBkgnd = NULL;

       case WM_CTLCOLORLISTBOX: //or WM_CTLCOLORSTATIC
       if ((UINT)lParam == g_hChatEdit) 
       {
            HDC hdcStatic = (HDC)wParam;
            SetTextColor(hdcStatic, RGB(255,255,255));
            SetBkColor(hdcStatic, RGB(0,128,0));
            if (hbrBkgnd == NULL)
                hbrBkgnd = CreateSolidBrush(RGB(200,200,255));
            return (INT_PTR)hbrBkgnd;
       }