-
January 11th, 2020, 10:55 AM
#1
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..
-
January 11th, 2020, 02:41 PM
#2
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?
-
January 12th, 2020, 03:40 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|