Click here to Skip to main content
15,942,710 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi friends,
I am using CListCtrl with check boxes in my code. I want to change the color of row if its check box is checked. I have tried using function SetTextBkColor on 'OnClickLstdata' event but it changes color of all inserted rows. Please provide the solution. Thanks... :)
Posted
Updated 2-Jun-10 2:20am
v2

I would favor custom drawing over owner drawn list controls, since you only have to draw the part you're interested in, and let the control draw the rest.
In your case, all you have to do is set a color at the right place.

void CMyListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLVCUSTOMDRAW pNMCD = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR);

	switch (pNMCD->nmcd.dwDrawStage)
	{
	case CDDS_PREPAINT:
		*pResult = CDRF_NOTIFYITEMDRAW;
		break;
	case CDDS_ITEMPREPAINT:
		*pResult = CDRF_NOTIFYSUBITEMDRAW;
		break;
	case (CDDS_ITEMPREPAINT | CDDS_SUBITEM):
		if (HasDifferentColor(pNMCD->iItem))
			pNMCD->clrTextBk = m_clrDifferent;
		break;
...

Edit: Corrected the last case constant
 
Share this answer
 
v2
Comments
mikhil10 10-Jun-10 7:14am    
Thanks Niklas .. & all others for help .. with some modifications I have succefully implemented above code .
You have to derive a new class from CListCtrl, and override the OnPaint() event, drawing the item the way you want to. Keep in mind that you have to handle ALL painting situations when you do this.
 
Share this answer
 
Comments
mikhil10 2-Jun-10 5:36am    
isnt there any alternate way? I just need to change the color of checked row.
#realJSOP 2-Jun-10 6:10am    
Sometimes, the hard way is the only way. You may be able to find an example on google. MFC has been around a long time.
I've not done this in yonks but as no one else has answered...

If you want to do it "raw" SDK style handle WM_NOTIFY from the list view control and process the NM_CUSTOMDRAW notification. That will tell you what's being drawn and enable you to control the process.

As you seem to using MFC how about overriding DrawItem()? That should tell you everything you need to know and enable you to customise the process.

Anyway, have a look at this[^]article on MSDN for some background, especially the sections on custom drawing.

Hope that points you in the right direction,

Cheers,

Ash
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900