Change background color | CodeGuru

Change background color

Changing the background color of a listview control is very easy if you are already using an owner drawn control. See ‘Selection highlighting of entire row’ for code on how to implement an owner drawn listview control. Once the basic owner draw code is in place all you need to do to get a different […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More


Changing the background color of a listview control is very easy if you are


already using an owner drawn control. See


‘Selection highlighting of entire row’

for code on how to implement an owner drawn listview control. Once the basic owner
draw code is in place all you need to do to get a different background color is to
fill the row rectangle with the desired color before drawing the individual elements of the row.

If you are using the implementation shown in
‘Selection highlighting of entire row’
then you can change the code to draw the background color or highlight in the DrawItem() function.
Change the code given below

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

		pDC->FillRect(rcHighlight, &CBrush(::GetSysColor(COLOR_HIGHLIGHT)));
	}
	else
		pDC->FillRect(rcHighlight, &CBrush(::GetSysColor(COLOR_WINDOW)));

to the following code. This well set the background to yellow.

	// 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(RGB(255,255,0));
// Remove	pDC->FillRect(rcHighlight, &CBrush(::GetSysColor(COLOR_WINDOW)));
	}

You also have to add a handler for the WM_ERASEBKGND message. This message is sent when the background needs to be painted. This is necessary because the DrawItem() function is called only for list rows. This leaves the area below the last row and to the right of the last column and that’s where the WM_ERASEBKGND handler comes in. Here’s the code.

BOOL CMyListCtrl::OnEraseBkgnd(CDC* pDC)
{
	CRect rcClient;
	GetClientRect( &rcClient );

	pDC->FillRect(rcClient, &CBrush(RGB(255,255,0)));
	return TRUE;
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.