如何永久更改ListView中项目的颜色?

时间:2012-08-30 10:48:43

标签: c++ winapi listview common-controls

我创建了listview列表视图

list[0]=CreateWindow(WC_LISTVIEW,L"",WS_DLGFRAME|WS_CHILD|WS_VISIBLE|LVS_REPORT|LVS_SINGLESEL,-,-,-,-,hwnd,NULL,hInstance,NULL);
LVCOLUMN lvcolumn;
lvcolumn.mask = LVCF_FMT|LVCF_TEXT|LVCF_WIDTH;
lvcolumn.fmt = LVCFMT_CENTER;
lvcolumn.pszText = L"a";
lvcolumn.cx = 100;
ListView_InsertColumn(list[0],0,&lvcolumn);
lvcolumn.pszText = L"b";
ListView_InsertColumn(list[0],1,&lvcolumn);
lvcolumn.pszText = L"c";
ListView_InsertColumn(list[0],2,&lvcolumn);
lvcolumn.pszText = L"d";
ListView_InsertColumn(list[0],3,&lvcolumn);

我使用LPNMLVCUSTOMDRAW插入颜色项

case WM_NOTIFY:
    if( ((LPNMHDR)lParam)->code == NM_CUSTOMDRAW){
        switch( ((LPNMLVCUSTOMDRAW)lParam)->nmcd.dwDrawStage){
            case CDDS_PREPAINT:
                return CDRF_NOTIFYITEMDRAW;
                break;
            case CDDS_ITEMPREPAINT:
            {
                LPNMLVCUSTOMDRAW customDraw = (LPNMLVCUSTOMDRAW)lParam;

                customDraw->clrText = getcolor();
                return CDRF_NEWFONT;
                break;
            }
            default: return CDRF_DODEFAULT;
        }
    }
    break;

getcolor是函数响应colorref。每个项目都有自己的文字颜色。但是当父窗口隐藏并再次显示时,项目颜色会发生变化。那么如何保持它不会改变,或者我们有另一种设置文本颜色的方法

我再通过这种方式创建一个新的listview项目(如果有人需要它)

ListView_InsertItem(hwndlist,&lvitem);
ListView_SetItemText(hwndlist,i,k,(LPWSTR)strarr[k].c_str());

0 个答案:

没有答案