Link to home
Start Free TrialLog in
Avatar of ianhoc
ianhoc

asked on

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)
Avatar of frogger1999
frogger1999

don't you just inherit it from CListCtrl

from class CListCtrl

BOOL SetBkColor( COLORREF cr );


doesn't this work?
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.

>> 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?
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;
          }
     }
}
Avatar of ianhoc

ASKER

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
Avatar of ambience
ASKER CERTIFIED SOLUTION
Avatar of pcooke
pcooke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial