Set Color of CListCtrl

How to set different background color for each row of the CListCtrl ?
wpkhooAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vinayak KumbarSr Program ManagerCommented:
Hi

Derive Ur own class say CVinListCtrl from CListCtrl and map CustomDraw() function and put the following code


void CVinListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMLVCUSTOMDRAW  lplvcd = LPNMLVCUSTOMDRAW)pNMHDR;

switch(lplvcd->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
     *pResult = CDRF_NOTIFYITEMDRAW;          
      break;
case CDDS_ITEMPREPAINT:
{
     CDC *pDC= CDC::FromHandle(lplvcd->nmcd.hdc);
     int iRow = lplvcd->nmcd.dwItemSpec;
           
    switch(iRow)
    {
     case 0:      
           lplvcd->clrText = RGB(0,0,128);
           lplvcd->clrTextBk = RGB(0,0,255);
           break;
     case 1:  
          lplvcd->clrText = RGB(255,0,0);
          lplvcd->clrTextBk = RGB(0,255,0);
          break;
      case 2:             
          lplvcd->clrText = RGB(0,128,0);
          lplvcd->clrTextBk = RGB(255,0,0);
           break;
      case 3:      
          lplvcd->clrText = RGB(128,0,64);
          lplvcd->clrTextBk = RGB(0,0,255);
         break;                        
    }
}
break;
default:
     *pResult = CDRF_DODEFAULT;
      break;
            
}

}

Here I am setting the text and background color for 3 rows, similarly U can do for others.
Hope that helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.