• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2865
  • Last Modified:

changing background color of CListCtrl

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
ianhoc
Asked:
ianhoc
1 Solution
 
frogger1999Commented:
don't you just inherit it from CListCtrl

from class CListCtrl

BOOL SetBkColor( COLORREF cr );


doesn't this work?
0
 
frogger1999Commented:
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
 
nietodCommented:
>> 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
pcookeCommented:
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
 
ianhocAuthor Commented:
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
 
ambienceCommented:
0
 
pcookeCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now