?
Solved

changing background color of CListCtrl

Posted on 2003-03-22
7
Medium Priority
?
2,830 Views
Last Modified: 2010-05-18
I have derived a class CColorListCtrl from CListCtrl and I am trying to change the background color depending on whether the ListCtrl is disabled or not.

I have accomplished similar feats with CEdit and CListBox, but I can't find where to put my code in the CColorListCtrl class.

CTLCOLOR doesn't trap, neither does Create or DrawItem..... any suggestions?

Thanks
Ian (ianhoc@ameritech.net)
0
Comment
Question by:ianhoc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:frogger1999
ID: 8188442
don't you just inherit it from CListCtrl

from class CListCtrl

BOOL SetBkColor( COLORREF cr );


doesn't this work?
0
 
LVL 1

Expert Comment

by:frogger1999
ID: 8188503
Actually I just tried a sample prog and I guess I couldn't get the backround color set with

SetBkColor( COLORREF cr );

to show through.

So I just called

SetBkColor( COLORREF cr );
and
SetTextBkColor(COLORREF cr);

with the same color and it works like a charm.

0
 
LVL 22

Expert Comment

by:nietod
ID: 8188591
>> CTLCOLOR doesn't trap
Do you understand that the WM_CTLCOLOR messages are sent to the window that is the PARENT of the control.  this way the parent window can set the color scheme and other appearance attributes of all the controls on the window.   So were you looking for this message on the list control window, or its parent window?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:pcooke
ID: 8189641
Have you tried trapping NM_CUSTOMDRAW messages? You then need to check what drawing stage is happening. I've used the code below to get an alternating colour effect for the lines in a list control, amongst other things.
(The namespaced items are only there for autocompletion in Visual Studio 6 - VS .Net shouldn't need them.)

void CAxEREnquiryListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
     BOOL bRowColourToggle = FALSE;

     NMLVCUSTOMDRAW * plvcd = reinterpret_cast<NMLVCUSTOMDRAW *>(pNMHDR);
     BOOL bHandled = TRUE;
     CListCtrl * pList = GetListCtrl();


     if(plvcd){
          switch(plvcd->nmcd.dwDrawStage){
               case CONS_LVCD::CD_MSGS::dwCDDS_PREPAINT:
                    *pResult = CDRF_NOTIFYITEMDRAW;
                    break;

               case CONS_LVCD::CD_MSGS::dwCDDS_ITEMPREPAINT:
                    if(pList->m_hWnd == plvcd->nmcd.hdr.hwndFrom){
                         bRowColourToggle = (plvcd->nmcd.dwItemSpec % 2);

                         plvcd->clrTextBk = bRowColourToggle ?
                              CONS_COLORS::ORDINALS::crefaryListStripes[CONS_COLORS::ORDINALS::dwCO_EnquiryListBack1] :
                              CONS_COLORS::ORDINALS::crefaryListStripes[CONS_COLORS::ORDINALS::dwCO_EnquiryListBack2] ;

                         plvcd->clrText = CONS_COLORS::crefEnquiriesTextForeGround;
                    }

                    *pResult = CDRF_NEWFONT;
                    //*pResult = CDRF_NOTIFYSUBITEMDRAW;
                    break;

               case CONS_LVCD::CD_MSGS::dwCDDS_ITEMPREPAINT | CONS_LVCD::CD_MSGS::dwCDDS_SUBITEM:
                    plvcd->clrText = RGB(0xCC, 0xCC, 0xCC);
                    plvcd->clrTextBk = RGB(0xEE, 0xEE, 0xEE);
                    *pResult = CDRF_NEWFONT;
                    break;

               default:
                    bHandled = FALSE;
          }
     }
}
0
 

Author Comment

by:ianhoc
ID: 8191741
Thanks for the input here guys:

Frogger, where did you put the code
"SetBkColor( COLORREF cr );
and
SetTextBkColor(COLORREF cr);" ?
I can't seem to get it to do anything.

nietod, thanks for enlightening me - a solution would be nice.

pcooke, I tried your code (thanks for the detailed reply!)
I trapped the OnNotify within the CColorListCtrl class wizard, but can find no references to OnCustomDraw. - How would I cath the OnCustomDraw notification - any cahnges to the clrTextBk and clrText have no affect from within OnNotify.

Ian
0
 
LVL 22

Expert Comment

by:ambience
ID: 8192812
0
 

Accepted Solution

by:
pcooke earned 1000 total points
ID: 8193587
To catch the NM_CUSTOMDRAW message, first create a handler function similar to:
     afx_msg void OnCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);

Create a body similar to my original comment. Now add an entry to the message map:
eg:
     ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)

This works for a view that inherits from CListView. If it's a control on a form, try an ON_NOTIFY,
eg:
     ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, OnCustomdraw)

The value of (*pResult) is what the framework uses to find out what you are trying to do;
If you set it to CDRF_DODEFAULT then it will just do whatever the default is.
At the prepaint stage, set it to CDRF_NOTIFYITEMDRAW so the framework will tell you when an item needs drawing.
At the drawing stage, set it to CDRF_NEWFONT to let the framework know that you have changed something.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question