CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2019
    Posts
    82

    How to use ownerdrawn ccombobox in MyCdialog class

    I have derived a class from CComboBox.I want to change background color of selected/hovered string in CComboBox to Red.
    Below is code of .h and .cpp files of derived class.
    Code:
    //MyCCombo.h
    
    #include "Stdafx.h"
    
    class MyCCombo : public CComboBox 
    {
    public:
    MyCCombo();
    ~MyCCombo();
    
    protected:
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    DECLARE_MESSAGE_MAP()
    };
    Code:
    //MyCCombo.cpp
    
    void MyCCombo::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {
       ASSERT(lpDrawItemStruct->CtlType == ODT_COMBOBOX);
       LPCTSTR lpszText = (LPCTSTR)lpDrawItemStruct->itemData;
       ASSERT(lpszText != NULL);
       CDC dc;
    
       dc.Attach(lpDrawItemStruct->hDC);
    
       // Save these value to restore them when done drawing.
       COLORREF crOldTextColor = dc.GetTextColor();
       COLORREF crOldBkColor = dc.GetBkColor();
    
       // If this item is selected, set the background color
       // and the text color to appropriate values. Erase
       // the rect by filling it with the background color.
       if ((lpDrawItemStruct->itemAction & ODA_SELECT) &&
           (lpDrawItemStruct->itemState & ODS_SELECTED))
       
     {
          dc.SetTextColor(RGB(33,33,33));
          dc.SetBkColor(RGB(255,0,0));
          dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(255,0,0));
       }
       else
       {
          dc.FillSolidRect(&lpDrawItemStruct->rcItem, crOldBkColor);
       }
    
    
       dc.Detach();
    }
    Now in MyCdialog(derived from Cdialog).h ,i include the #MyCComboBox.h and declared the variable of type MyCComboBox m_box; and used m_box in MyCdialog.cpp through DDX mapping
    But result is not reflected..

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to use ownerdrawn ccombobox in MyCdialog class

    What happens when you set a breakpoint inside the DrawItem code? Does the dc.SetBkColor() code get called?

  3. #3
    Join Date
    Oct 2019
    Posts
    82

    Re: How to use ownerdrawn ccombobox in MyCdialog class

    Drawitem gets called and also the color is also getting reflected but issue now is color is reflected in complete background dropdown list but i want color change on selected or hovered item and plus text of item is distored

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured