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 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:00am

    Originally posted by: dsfy

    in dire need of this ,please help me !thanks

    Reply
  • How to chang background color of Header control?

    Posted by Legacy on 07/21/2002 12:00am

    Originally posted by: MikeGong

    Please tell me how to change background color of 
    
    Header control.

    thanks

    Reply
  • The easy way is SetBkColor() and SetTextBkColor()

    Posted by Legacy on 02/14/2002 12:00am

    Originally posted by: Moisha

    I had to indicate in some way which of the two
    
    CListView-derived views has the input focus.
    Changing background color would be ok.

    The only bad thing about SetBkColor() and SetTextBkColor()
    is that you have to invoke them both.

    The rest is very simple.

    #define RGB_INFOCUS RGB(0xff,0xff,0xff)
    #define RGB_NOFOCUS RGB(0xe0,0xe0,0xe0)

    ....

    void CXYZView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
    {

    COLORREF c=bActivate?RGB_INFOCUS:RGB_NOFOCUS;
    CListCtrl&l=GetListCtrl();
    l.SetBkColor(c);
    l.SetTextBkColor(c);
    l.Invalidate(TRUE);

    CListView::OnActivateView(bActivate, pActivateView, pDeactiveView);
    }

    And it is also important to Create() views with
    the BgColor that corresponds to "no focus" state:

    BOOL CXYZView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
    {
    ....
    CListCtrl&l=GetListCtrl();
    l.SetBkColor(RGB_NOFOCUS);
    l.SetTextBkColor(RGB_NOFOCUS);
    ....
    }

    That's all!

    Reply
  • Why don't you use SetTextBkColor? Easier Way!

    Posted by Legacy on 12/12/2001 12:00am

    Originally posted by: Radu

    Then you don't need OWNERDRAWFIXED. So you don't need all the code to draw everything yourself.Also you don't need to handle OnEraseBkgnd to change the background color of the control itself(se example below). 
    
    

    Here is an example:


    BOOL ConfigEditDlg::OnInitDialog()
    {
    listVals = "0x18";

    typedef struct
    {
    TCHAR Name[10];
    }TXREF;

    TXREF Channels[] =
    {
    {"TX0"},
    {"TX1"},
    {"TX2"},
    {"TX3"},
    {"TX4"},
    {"TX5"},
    {"TX6"},
    {"TX7"},
    {"TX8"},
    {"TX9"},
    {"TX10"},
    {"TX11"},


    };

    m_bluecolor=RGB(225,240,250); // blue

    CDialog::OnInitDialog();
    UpdateData(TRUE); // bring the information from the dialog.


    //list control initializing

    //insert 2 columns m_listCtrl.InsertColumn(0,"Values",LVCFMT_LEFT,50,0); m_listCtrl.InsertColumn(1,"Channels",LVCFMT_LEFT,80,1);

    //populate columns with values and labels
    for (int k= 0; k<12; k++)
    {
    m_listCtrl.InsertItem(k,listVals);
    m_listCtrl.SetItemText(k,1,Channels[k].Name);
    }

    //Sets the text background color
    m_listCtrl.SetTextBkColor(m_bluecolor);

    //Sets the the background color of the control so you don't //need to handle OnEraseBkgnd
    m_listCtrl.SetBkColor(m_bluecolor);

    return TRUE;
    }

    Reply
  • Where is SourceCode?

    Posted by Legacy on 11/29/2001 12:00am

    Originally posted by: Duke

    I can't find Demo Project or Source Code....

    Where can I get it?

    Thank you in advance...

    • Where is SourceCode?

      Posted by yogesh28577 on 11/28/2006 02:39am

      plz tell me where i found source code thanks yogesh yogesh28577@yahoo.co.in

      Reply
    Reply
  • Again, If using ListView ....

    Posted by Legacy on 09/03/2001 12:00am

    Originally 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!!

    Reply
  • source code

    Posted by Legacy on 06/27/2001 12:00am

    Originally posted by: mouse

    Hi,

    Any source code for this?

    Thx,

    Zhou Hao

    Reply
  • Why not use SetBkColor()?

    Posted by Legacy on 01/20/2001 12:00am

    Originally 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:00am

    Originally 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!

    Reply
  • Change code: Change background color

    Posted by Legacy on 02/23/1999 12:00am

    Originally posted by: Jaewoon Lee

    I think hard coding is not good style... :-(
    
    So, I changed some codes in ListCtrlEx.cpp.


    1.
    From "pDC->FillRect(rcRow, &CBrush(RGB(255,255,0));"
    To "pDC->FillRect(rcRow, &CBrush(GetBkCOlor()));"


    2.

    From:
    // Draw normal and overlay icon
    pImageList = GetImageList(LVSIL_SMALL);
    if (pImageList) {
    UINT nOvlImageMask=lvi.state & LVIS_OVERLAYMASK;
    pImageList->Draw(pDC, lvi.iImage,
    CPoint(rcIcon.left, rcIcon.top),
    (bHighlight?ILD_BLEND50:0) | ILD_TRANSPARENT | nOvlImageMask );
    }
    // Draw item label - Column 0
    rcLabel.left += offset/2;
    rcLabel.right -= offset;

    pDC->DrawText(sLabel,-1,rcLabel,DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP
    | DT_VCENTER | DT_END_ELLIPSIS);

    To:

    // Draw normal and overlay icon
    pImageList = GetImageList(LVSIL_SMALL);
    if (pImageList) {
    UINT nOvlImageMask=lvi.state & LVIS_OVERLAYMASK;
    pImageList->Draw(pDC, lvi.iImage,
    CPoint(rcIcon.left, rcIcon.top),
    (bHighlight?ILD_BLEND50:0) | ILD_TRANSPARENT | nOvlImageMask );
    }
    // Draw item label - Column 0
    rcLabel.left += offset/2;
    rcLabel.right -= offset;

    pDC->SetTextColor(GetTextColor()); // Insert this code
    pDC->SetBkColor(GetTextBkColor()); // Insert this code

    pDC->DrawText(sLabel,-1,rcLabel,DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP
    | DT_VCENTER | DT_END_ELLIPSIS);


    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • 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 …

  • Mobile devices, social business apps, and business analytics are converging with the Cloud to create the most substantial changes in technology since the Internet revolution. Businesses have to change the way they think and operate, and with rising budgets for technology, they need someone to provide the services that will keep them competitive in this environment. Learn more about the important technology trends you need to stay on top of to ensure your business doesn't get left behind.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds