Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 691
  • Last Modified:

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
0
rickatseasoft
Asked:
rickatseasoft
  • 4
  • 2
2 Solutions
 
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
 
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
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.

 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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