Link to home
Start Free TrialLog in
Avatar of lily1102
lily1102

asked on

dynamically change background color for certain cells in the CListCtrl using report style

I use CListCtrl with report style to list some text with grids.
I would like to change the background color for some cells.
For example, if the user push button A, the background for the cell at row 3 and col 4 will be changed to yellow. And if the user push button B, the the background for the cell at row 3 and col 4 will be changed to red. I have been searched the internet for good solutions but they are all too complicated for me. Currently, I use CustomDraw below. However, it will change the background clolor for the "whole" row, which is not what I really want. In the code, indexVector is an vector variable in the Mydialog object used for indicating which row needs to change the background.
I would like to have a function that I can pass in the row and col and it change the background for that cell for me in the CListCtrl.
Thanks very much for any comments.

void Mydialog::OnCustomDrawList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	//SAMPLE: for this notification, the structure is actually a
	// NMLVCUSTOMDRAW that tells you what's going on with the custom
	// draw action. So, we'll need to cast the generic pNMHDR pointer.
	LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
	//SAMPLE: The first notification we'll receive is CDDS_PREPAINT,
	// right at the beginning of the paint cycle. We'll want to return
	// item-specific notifications so we can deal with things the right
	// way.
	if (lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT)
	{
		*pResult = CDRF_NOTIFYITEMDRAW;
	}
	// otherwise, we might be really getting a notification about
	// painting something.
	else if (lplvcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
	// Because we returned CDRF_NOTIFYITEMDRAW in response to
    // CDDS_PREPAINT, CDDS_ITEMPREPAINT is sent when the control is
    // about to paint an item.
	{
		if (indexVector.at(lplvcd->nmcd.dwItemSpec) == 1 )
		{
            lplvcd->clrText = RGB(255, 0, 0);
            lplvcd->clrTextBk = RGB(0, 255, 255);
            *pResult = CDRF_NEWFONT;
		}
		else
		{
		// no change; tell the control to do the default
			*pResult = CDRF_DODEFAULT;
        }
	}
	else
	{
		*pResult = 0;
	}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Naveen_R
Naveen_R

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lily1102
lily1102

ASKER

Thanks so much for the wonderful solution that you provided. Super!!