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..