4

I want to mark a special line in the listview with a color, so it'll look like this : http://www.codeproject.com/KB/list/CColorListCtrl/rowCell.jpg (The first row - which is green).

Im not sure how exactly do I do this, and got a bit confused by the results on the network... So how can I do it?

Thanks!

1 Answer 1

6

From Codeproject

First we handle the WM_NOTIFY message to use a NM_CUSTOMDRAW notification. We don't need to use any owner drawn listview, and since this is custom drawn, we can paint the items/subitems at our will.

if(((LPNMHDR)lParam)->code == NM_CUSTOMDRAW) {
    SetWindowLong(hWnd, DWL_MSGRESULT, 
    (LONG)ProcessCustomDraw(lParam));
    return TRUE;
}

-

LRESULT ProcessCustomDraw (LPARAM lParam) {
LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
switch(lplvcd->nmcd.dwDrawStage) 
{
    case CDDS_PREPAINT : //Before the paint cycle begins
        //request notifications for individual listview items
        return CDRF_NOTIFYITEMDRAW;

    case CDDS_ITEMPREPAINT: //Before an item is drawn
        if (((int)lplvcd->nmcd.dwItemSpec%2)==0)
        {
            //customize item appearance
            lplvcd->clrText   = RGB(255,0,0);
            lplvcd->clrTextBk = RGB(200,200,200);
            return CDRF_NEWFONT;
        }
        else{
            lplvcd->clrText   = RGB(0,0,255);
            lplvcd->clrTextBk = RGB(255,255,255);

            return CDRF_NEWFONT;
        }
        break;

    //Before a subitem is drawn
    case CDDS_SUBITEM | CDDS_ITEMPREPAINT: 
        if (iSelect == (int)lplvcd->nmcd.dwItemSpec)
        {
            if (0 == lplvcd->iSubItem)
            {
                //customize subitem appearance for column 0
                lplvcd->clrText   = RGB(255,0,0);
                lplvcd->clrTextBk = RGB(255,255,255);

                //To set a custom font:
                //SelectObject(lplvcd->nmcd.hdc, 
                //    <your custom HFONT>);

                return CDRF_NEWFONT;
            }
            else if (1 == lplvcd->iSubItem)
            {
                //customize subitem appearance for columns 1..n
                //Note: setting for column i 
                //carries over to columnn i+1 unless
                //      it is explicitly reset
                lplvcd->clrTextBk = RGB(255,0,0);
                lplvcd->clrTextBk = RGB(255,255,255);

                return CDRF_NEWFONT;
            }
        }
    }
    return CDRF_DODEFAULT;
}

EDIT

Well, I went ahead and made a small example. It might be a bit dirty but it works. You just need to fill in some of your own variables, but it works.

LRESULT MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
    case WM_CREATE: 
        {
            //Initialise something
            INITCOMMONCONTROLSEX icex; //Hehehe
            icex.dwICC = ICC_LISTVIEW_CLASSES;
            InitCommonControlsEx(&icex);

            //Add listview
            listview = CreateWindow(WC_LISTVIEW, 
                                        "LISTVIEW",
                                         WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_REPORT,
                                         0, 0,
                                         400,
                                         400,
                                         hWnd,
                                         0,
                                         hInstance,
                                         NULL);

            ListView_SetExtendedListViewStyle(listview, LVS_EX_FULLROWSELECT);

            //Add column
            LVCOLUMN lvc;
            lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
            lvc.iSubItem = 0;
            lvc.pszText = (char*) "test";
            lvc.cx = 200; 
            lvc.fmt = LVCFMT_RIGHT;
            ListView_InsertColumn(listview, 0, &lvc);
            ListView_InsertColumn(listview, 0, &lvc);

            //Add item
            LVITEM lvI;
            lvI.pszText   = (char*) "item";
            lvI.mask      = LVIF_TEXT;
            lvI.stateMask = 0;
            lvI.iSubItem  = 0;
            lvI.iItem  = 0;
            ListView_InsertItem(listview, &lvI);

            lvI.iSubItem  = 1;
            lvI.pszText   = (char*) "subitem";
            ListView_SetItem(listview, &lvI);

            //Add item
            lvI.pszText   = (char*) "item";
            lvI.mask      = LVIF_TEXT;
            lvI.stateMask = 0;
            lvI.iSubItem  = 0;
            lvI.iItem  = 1;
            ListView_InsertItem(listview, &lvI);

            lvI.iSubItem  = 1;
            lvI.pszText   = (char*) "subitem";
            ListView_SetItem(listview, &lvI);
        }
    break;
    case WM_CLOSE:  
        DestroyWindow(hWnd);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        running = false;
        return 0;
    case WM_COMMAND:
        switch(LOWORD(wParam)) {
        }
    break;
    case WM_NOTIFY:
        if(((LPNMHDR)lParam)->code == NM_CUSTOMDRAW) {
                LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)lParam;
                switch(lplvcd->nmcd.dwDrawStage) {
                    case CDDS_PREPAINT:
                        return CDRF_NOTIFYITEMDRAW;
                    break;
                    case CDDS_ITEMPREPAINT:
                        if (((int)lplvcd->nmcd.dwItemSpec%2)==0) {
                            lplvcd->clrText   = RGB(0,0,0);
                            lplvcd->clrTextBk = RGB(255,0,0);
                        } else {
                            lplvcd->clrText   = RGB(0,0,255);
                            lplvcd->clrTextBk = RGB(255,255,255);
                        }
                        return CDRF_NEWFONT;
                    break;
                    //There would be some bits here for subitem drawing but they don't seem neccesary as you seem to want a full row color only
                    case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
                        return CDRF_NEWFONT;    
                    break;
                }


                return TRUE;
        }
    default:
        return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return 0;
}
5
  • Thanks! but, is iSelect the index selected in the listview? Also, I did not understand how do I use this code, to start painting on a listView item. I've added the if statement at the top to my code, though it doesnt get inside it...
    – Amit
    May 3, 2014 at 21:11
  • It seems to be the selected item: iSelect=SendMessage(hList,LVM_GETNEXTITEM, -1,LVNI_FOCUSED); // return item selected Did you place this inside your event loop?
    – Eejin
    May 3, 2014 at 21:19
  • Then, where do I put the if statement, asking if it is NM_CUSTOMDRAW? Just in my WM_CREATE before I create my ListView? I've put the code, yet it doesnt get inside the if statement, and nothing is really happening... It isnt evne get inside the ProcessCustomDraw function... I've noticed that in the if(((LPNMHDR)lParam)->code == NM_CUSTOMDRAW) , the variable ((LPNMHDR)lParam)->code is allways never equal to NM_CUSTOMEDRAW... Do I need to use something like SendMessage(Hwnd, NM_CUSTOMEDRAW, ....) ?
    – Amit
    May 3, 2014 at 21:35
  • Im a bit confused with your code, not sure how to use it... It'd really help if you'll explain more how to use it... Thanks!
    – Amit
    May 3, 2014 at 22:06
  • I added a small example of how I did it. The method codeproject used didn't quite work for me so I embedded the drawing in the main event loop (you might want to move this somewhere else for estetic reasons ;) )
    – Eejin
    May 3, 2014 at 22:31

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.