CComboBox Change background color only when it has focus

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_clrOverFore=RGB(0,0,0);
    m_clrOverBack=RGB(225,210,210);//Blue background
    m_OverBrush.CreateSolidBrush(m_clrOverBack);

In CtlColor()
      pDC->SetTextColor(m_clrFore);
      pDC->SetBkColor(m_clrBack);
      return (HBRUSH)m_OverBrush.GetSafeHandle();

What am I doing wrong here?

Thanks, Rick
rickatseasoftAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

puranik_pCommented:

You can download the project from this link.
http://www.codersource.net/mfc_ccombobox_ownerdraw_picture.html


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;

      dc.Attach(lpdis->hDC);

      // 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(bHasFocus)
            {
                  dc.DrawFocusRect(rect);
                  
            }      
      }
      else
      {
            if( lpdis->itemID == 2)
                  dc.FillSolidRect(rect, RGB(255,0,0));  
            else
                  dc.FillSolidRect(rect, ::GetSysColor(COLOR_WINDOW));  

      }

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

      dc.Detach();

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rickatseasoftAuthor Commented:
puranik:

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

Alex:

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

Thanks to both, Rick
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

rickatseasoftAuthor Commented:
puranik:

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
0
rickatseasoftAuthor Commented:
Guys:

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))    
    {
      pDC->SetBkColor(RGB(210,210,225));
      return m_hbrBack;
   }
   return hbr;
0
puranik_pCommented:
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));


0
rickatseasoftAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.