Change the background color of individual columns
Posted
by Zafir Anjum
on August 6th, 1998
To set the background color of a column, the list view control has to be owner drawn. In an owner drawn control, each row is drawn by the application program rather than by the control. So when drawing a row, the background color can be set to our choice within the rectangle occupied by a particular column. To implement an owner drawn control see the topic 'Selection highlighting of entire row'.

The code segment below illustrates how to set yellow as the background color for alternate columns. The code shown is part of the DrawItem() function. The first segment draws the background color just before drawing the state icon. The second segment draws the background color within the loop that draws the labels for the remaining columns.
// Set clip region
rcCol.right = rcCol.left + GetColumnWidth(0);
CRgn rgn;
rgn.CreateRectRgnIndirect(&rcCol);
pDC->SelectClipRgn(&rgn);
rgn.DeleteObject();
// Draw column background
if( !bHighlight )
pDC->FillRect(rcCol, &CBrush(RGB(255,255,0)));
// Draw state icon
if (lvi.state & LVIS_STATEIMAGEMASK)
:
:
:
:
for(int nColumn = 1; GetColumn(nColumn, &lvc); nColumn++)
{
rcCol.left = rcCol.right;
rcCol.right += lvc.cx;
// Draw column background
if( !bHighlight || ( bHighlight && m_nHighlight == HIGHLIGHT_NORMAL ) )
if( (nColumn+1) % 2 )
pDC->FillRect(rcCol, &CBrush(RGB(255,255,0)));

Comments
Doesn't work - Brush clears text
Posted by Legacy on 09/13/2001 12:00amOriginally posted by: Bill Munro
ReplyFillrect causes the brush to clear any text already initialized via SetItemText in OnInitialUpdate. I tried SetBkColor, and that did not work either. The NM_CUSTOMDRAW solution posted elsewhere on this site did not work either, because I am using VC5, which does not seem to have Subitem access in its version of COMMCTRL.H. Any suggestions??
Using NM_CUSTOMDRAW to change text and backgrond colours in a listview
Posted by Legacy on 04/19/2000 12:00amOriginally posted by: Brad Spencer
ReplyIt doesn't work!
Posted by Legacy on 08/06/1999 12:00amOriginally posted by: Nicola Genchi
ReplyCustom Draw
Posted by Legacy on 03/25/1999 12:00amOriginally posted by: Philip Lee
Setting the text color/background color and font of individual rows and/or sub-items can be done without full owner draw by using custom draw.
Sorry I haven't got an example at the moment but look in the platform SDK under NM_CUSTOMDRAW.
ReplyChange text color for highlighted columns
Posted by Legacy on 01/18/1999 12:00amOriginally posted by: Hoyle Greene
I am having trouble dtermining if the text color can be changed for individual columns of a row. Help?
Reply