Trending

Change Background Color Of Individual Rows

Published on: October 19, 2000
Last Updated: October 19, 2000

Change Background Color Of Individual Rows

Published on: October 19, 2000
Last Updated: October 19, 2000

Changing the color of a single row is achieved in essentially the same way as changing the background color of the entire control. See ‘Change background color’.

Instead of using the same color for all rows, use a row specific color. If you want to be able to add any color to any row then you would have to track this information in some data structure.

In the code below we simply highlight the alternate rows with yellow. This code belongs in the DrawItem() function. See ‘Selection highlighting of entire row’ for more details.

	// Draw the background color
	if( bHighlight )
	{
		pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
		pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));

		pDC->FillRect(rcHighlight, &CBrush;(::GetSysColor(COLOR_HIGHLIGHT)));
	}
	else
	{
		CRect rcClient, rcRow = rcItem;
		GetClientRect(&rcClient;);
		rcRow.right = rcClient.right;

		pDC->FillRect(rcRow, &CBrush;(nItem%2 ? ::GetSysColor(COLOR_WINDOW) :
	 					RGB(255,255,0)));
	}

Stay on top of the latest technology trends — delivered directly to your inbox, free!

Subscription Form Posts

Don't worry, we don't spam

Written by Bobby

Bobby Lawson is a seasoned technology writer with over a decade of experience in the industry. He has written extensively on topics such as cybersecurity, cloud computing, and data analytics. His articles have been featured in several prominent publications, and he is known for his ability to distill complex technical concepts into easily digestible content.