Change background color
Posted
by Zafir Anjum
on August 6th, 1998
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;
}

Comments
how to change the listview control background mode to transparence??
Posted by Legacy on 08/01/2002 12:00amOriginally posted by: dsfy
in dire need of this ,please help me !thanks
ReplyHow to chang background color of Header control?
Posted by Legacy on 07/21/2002 12:00amOriginally posted by: MikeGong
ReplyThe easy way is SetBkColor() and SetTextBkColor()
Posted by Legacy on 02/14/2002 12:00amOriginally posted by: Moisha
Reply
Why don't you use SetTextBkColor? Easier Way!
Posted by Legacy on 12/12/2001 12:00amOriginally posted by: Radu
ReplyWhere is SourceCode?
Posted by Legacy on 11/29/2001 12:00amOriginally posted by: Duke
I can't find Demo Project or Source Code....
Where can I get it?
Thank you in advance...
-
ReplyWhere is SourceCode?
Posted by yogesh28577 on 11/28/2006 02:39amplz tell me where i found source code thanks yogesh yogesh28577@yahoo.co.in
ReplyAgain, If using ListView ....
Posted by Legacy on 09/03/2001 12:00amOriginally posted by: Jesvh
DrawItem() is not a member function of CListView....
If you use GetListCtrl() to get list control of CListView , how can I override its member function DrawItem()?
Thank you for your help!!
Replysource code
Posted by Legacy on 06/27/2001 12:00amOriginally posted by: mouse
Hi,
Any source code for this?
Thx,
Zhou Hao
ReplyWhy not use SetBkColor()?
Posted by Legacy on 01/20/2001 12:00amOriginally posted by: toreaian
Is there any problem if I use SetBkColor() and
SetTextBkColor()?
Reply
How to do if you use CListView?
Posted by Legacy on 10/24/2000 12:00amOriginally posted by: Broderick
I was wondering how to change the background color if you have a class derived from CListView.
Does this code only work if you use CListCtrl?
Thanks!
ReplyChange code: Change background color
Posted by Legacy on 02/23/1999 12:00amOriginally posted by: Jaewoon Lee
ReplyLoading, Please Wait ...