visual-c++colorsmfcclistctrl

Set background cell color on CListCtrl


I have a table in project :

Now I want to make some of rows colored, so I tried

SetTextBkColor(RGB(122,0,122))

The previous line is in the following context:

BOOL SickLeaveViewPreviousTab::OnInitDialog()
{

    BOOL result = CDialog::OnInitDialog();

    setHighlighted();

    listCtrl.InsertColumn(0,"Name",LVCFMT_CENTER,80);
    listCtrl.InsertColumn(1,"Surname",LVCFMT_CENTER,120);
    listCtrl.ShowGrid();

    for (int x=0;x<_previous->length();++x)
    {
        shared_ptr<SickLeave> sickLeave = _previous->get(x);
        listCtrl.InsertItem(x,_patient->getName().c_str());
        listCtrl.SetItemText(x,1,_patient->getSurname().c_str());           
        listCtrl.SetTextBkColor(RGB(122,0,122));
    }
    UpdateLayout();
    ReleaseResources();
    return result;
}

But the color of the rows doesn't change. How can I solve the problem?

P.S. Yeah, there is a mistake in "desease"...


Solution

  • The answer to your question is on https://stackoverflow.com/a/19701300/383779

    Derive a made-by-you class from CMFCListCtrl which in turn is derived from CListCtrl. Then override the method OnGetCellBkColor the way you want.

    COLORREF CMyColorfulListCtrl::OnGetCellBkColor(int nRow,int nColumn)
    {
        if (nRow == THE_ROW_IM_INTERESTED_IN)
        {
            return WHATEVER_COLOR_I_NEED;
        }
        return CMFCListCtrl::OnGetCellBkColor(nRow, nColumn);
    }