CComboBox Change background color only when it has focus

Posted on 2006-04-20
Last Modified: 2013-11-20
I want to change the background color of the CComboBox only when it has focus.  It can change the color of the entire list, or just the color of the item displayed---I can live with either one.

I've tried the standard reflection used by CEdit

In the constructor

    m_clrOverBack=RGB(225,210,210);//Blue background

In CtlColor()
      return (HBRUSH)m_OverBrush.GetSafeHandle();

What am I doing wrong here?

Thanks, Rick
Question by:rickatseasoft
    LVL 48

    Assisted Solution

    LVL 14

    Accepted Solution


    You can download the project from this link.

    Go to the defination of this function : void CPicDrawC::DrawItem(LPDRAWITEMSTRUCT lpdis, bool bHasFocus)

    Make the changes in function as given below.
    Or u can directly replace that function with the function as shown by me.

    void CPicDrawC::DrawItem(LPDRAWITEMSTRUCT lpdis, bool bHasFocus)
          if (lpdis->itemID==(UINT)-1) return;

          CDC dc;
          CRect rect = lpdis->rcItem;
          ASSERT( lpdis->itemID >= 0 );
          const int nIndex = lpdis->itemID;


          // Save these value to restore them when done drawing.
          COLORREF crOldTextColor = dc.GetTextColor();
          COLORREF crOldBkColor = dc.GetBkColor();

          if ((lpdis->itemAction | ODA_SELECT) &&      (lpdis->itemState  & ODS_SELECTED))
                dc.FillSolidRect(rect, ::GetSysColor(COLOR_HIGHLIGHT));

                if( lpdis->itemID == 2)
                      dc.FillSolidRect(rect, RGB(255,0,0));  
                      dc.FillSolidRect(rect, ::GetSysColor(COLOR_WINDOW));  


          HBITMAP hbmp = (HBITMAP)lpdis->itemData;
          CDC MemDC;
          dc.BitBlt(rect.left+5,, PicSizeX, PicSizeY, &MemDC, 0, 0, SRCCOPY);
          // Reset the background color and the text color back to their
          // original values.



    Author Comment


    I just downloaded the source, and will attempt to implement it later today or tomorrow.


    After I Subclass the window, what do I do then?

    Thanks to both, Rick

    Author Comment


    It would seem that the example that you give works for coloring the various elements of the list.  My problem is that I want to hilight the EditBox portion of the list box when it receives focus.

    Thanks, Rick

    Author Comment


    Thanks for trying.  I found the answer in another location; it involved basically

    HBRUSH CStateCombo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) //NON-REFLECTIVE
       HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);

          // TODO: Change any attributes of the DC here
          // TODO: Return a different brush if the default is not desired

        if (m_bHaveFocus&&(nCtlColor == CTLCOLOR_EDIT))    
          return m_hbrBack;
       return hbr;
    LVL 14

    Expert Comment

    Go to this line of code.

     if ((lpdis->itemAction | ODA_SELECT) &&     (lpdis->itemState  & ODS_SELECTED))
            dc.FillSolidRect(rect, ::GetSysColor(COLOR_HIGHLIGHT));

    If you want to capture highlighted state then modify the ::GetSysColor(COLOR_HIGHLIGHT) with your own color.
    i.e. you can write  

       dc.FillSolidRect(rect, RGB(0,255,0));


    Author Comment

    puranik p:

    I am buried right now, but will be back to the CComboBox issue within the next couple of days and will  try this at that time.

    Thanks again, Rick

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now