tags:

views:

57

answers:

2

Hi!

I have a listview with 3 coloumns. The first two columns has values and the third one is empty yet. I want to know, how can i insert a colored text later into the third column? I don't want to color the full row, only the third column with changing colors.

Thanks in advance!

kampi

+5  A: 

You can do this with the CustomDraw handler, ref: MSDN Developing Custom Draw Controls in Visual C++.

Basically it's quite simple (and the MSDN quite long) but it boils down to the following:

add one of these to the usual place:

ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)

Then add this method to the class.

void CMyListView::OnCustomDraw(NMHDR* nmhdr, LRESULT* result)
{
    LPNMLVCUSTOMDRAW  vcd = (LPNMLVCUSTOMDRAW)nmhdr;

    switch(vcd->nmcd.dwDrawStage)
    {
        case CDDS_PREPAINT :
        {
            *result = CDRF_NOTIFYITEMDRAW;
            break;
        }

        case CDDS_ITEMPREPAINT:
        {
            vcd->clrText = RGB(255,0,255); //change the colour of the second row.
            *result = CDRF_NOTIFYSUBITEMDRAW;
            break;
        }
        default:
            *result = 0;
            break;
    }
    return;
}
Richard Harrison
Hi! In the meantime i found a very similar to your code. But my problem is that your code is coloring every second line, what i found is coloring the full column. How can i check what is the value of my listview in a given line row and line? This is important because, i want to color the text according to that what text is in it. Thanks!
kampi
A: 

@Richard Harrison has the right idea in using NM_CUSTOMDRAW.

Rather than re-implementing the needed functionality though you should consider using one of the freely available CListView derived types.

Here is a project that I think will meet your needs.

Brian R. Bondy
Thank you very much! This is what i needed. Thanks again!
kampi