如何设置列表框背景颜色

时间:2015-03-16 04:14:56

标签: c winapi listbox

我不知道如何设置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;

列表框背景颜色仍为白色

2 个答案:

答案 0 :(得分:1)

根据这个委员会

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

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

答案 1 :(得分:1)

看看这个是否有效。请注意,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;
       }