Changing the Background Color of Individual Listview Columns

Environment: VC6 SP4, NT4 SP3, Win9x/Me/2K/XP, IE 4.0 or above

To set the background color of a column, the list view control has to be owner drawn. The textbackground and textcolor of items can be set by using ownerdraw, but the empty space in the listview remains. For this, we have to override OnEraseBkgnd(CDC* pDC).

// Add this to the message map
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
ON_WM_ERASEBKGND()

// You need to declare the oncustomdraw message handlers to
// the header
afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);

// Then add this code
void CMyView::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
  *pResult = 0;

  LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;

  switch(lplvcd->nmcd.dwDrawStage)
  {
    case CDDS_PREPAINT :
    {
      *pResult = CDRF_NOTIFYITEMDRAW;
      return;
    }

    // Modify item text and or background
    case CDDS_ITEMPREPAINT:
    {
      lplvcd->clrText = RGB(0,0,0);
      // If you want the sub items the same as the item,
      // set *pResult to CDRF_NEWFONT
      *pResult = CDRF_NOTIFYSUBITEMDRAW;
      return;
    }

    // Modify sub item text and/or background
    case CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEM:
    {
        if(lplvcd->iSubItem %2){
          lplvcd->clrTextBk = RGB(255,255,255);
        }
        else{
          lplvcd->clrTextBk = RGB(247,247,247);
        }

        *pResult = CDRF_NEWFONT;
        return;
      }
  }

}

BOOL CMyView::OnEraseBkgnd(CDC* pDC)
{
  CRect rect;
  GetClientRect(rect);

  CBrush brush0(RGB(247, 247, 247));
  CBrush brush1(RGB(255, 255, 255));

  SCROLLINFO si;
  ZeroMemory(&si, sizeof(SCROLLINFO));
    si.cbSize = sizeof(SCROLLINFO);
    si.fMask = SIF_POS;
  GetScrollInfo(SB_HORZ, &si);
  rect.left -= si.nPos;


  for(int i=0;i<=GetListCtrl().GetHeaderCtrl()
                             ->GetItemCount();i++){
    rect.right = rect.left+GetListCtrl().GetColumnWidth(i);
    pDC->FillRect(&rect,i%2 ? &brush1 : &brush0);
    rect.left += GetListCtrl().GetColumnWidth(i);
  }

  brush0.DeleteObject();
  brush1.DeleteObject();

  return FALSE;
}


Comments

  • Changing the Background Color of Individual Listview Columns

    Posted by billgate_qn on 03/28/2006 06:41am

    I think this title is excellent but it doen't have the objects demo source if you have, please share it for us Thanks and best regards

    Reply
  • Add a icon to a list view column other than the first column

    Posted by Legacy on 05/02/2003 12:00am

    Originally posted by: Cinky

    Hi,
    
    

    I need a List view control, which i can subclass and a icon instead of text to the 4th column of the listview control
    If anyone has any suggestions plz reply to this mail
    Also if there r controls avaulable with these feature that r free or can be bought - please let me know
    Thanks a lot!!!!!!!!!!!!!!

    • up to it

      Posted by kinglu on 02/16/2009 01:23am

      I need to do the same thing, but I have never found the resolutoin.

      Reply
    Reply
  • Could you share the object demo source?

    Posted by Legacy on 03/13/2003 12:00am

    Originally posted by: zikin

    It seem so wonderful! could you share it for me to study ?

    Reply
  • Where is Source Code?

    Posted by Legacy on 08/31/2002 12:00am

    Originally posted by: guoj

    Where I can find it?

    Reply
  • Very nice.

    Posted by Legacy on 08/27/2002 12:00am

    Originally posted by: Dong Soo Han

    Very nice and impressive. Good Job.
    Thanks.

    Reply
  • Super, but need full source code

    Posted by Legacy on 08/26/2002 12:00am

    Originally posted by: Bala

    Hi,

    I want to change Background color for my ClistCtrl, When I click CListCtrl Check Box, if I uncheck, it shoud repaint to original color. Please help for this.

    If u send full code for my requirements, I would be thankfull to you,

    REgards
    Bala...

    Reply
  • Source Code

    Posted by Legacy on 08/26/2002 12:00am

    Originally posted by: yy

    Source Code!!!

    Reply
  • good

    Posted by Legacy on 08/25/2002 12:00am

    Originally posted by: xiaogi

    excellent
    it helps solve my problem

    Reply
  • Very nice and good job

    Posted by Legacy on 08/23/2002 12:00am

    Originally posted by: internet_man

    What setting bold font in ListView?

    Reply
  • Problem if using style LVS_ICON, LVS_SMALLICON and LVS_LIST

    Posted by Legacy on 08/23/2002 12:00am

    Originally posted by: internet_man

    If using style LVS_ICON, LVS_SMALLICON and LVS_LIST
    not correct background.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds