Click here to Skip to main content
15,909,566 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created my own custom control class CMyComboBox and its derived from CCombobox.
i have added DrawItem as virtual function but this function is not getting called when loading my dailog
could you please tell me how to call drawitem here
2) i need toget the all the combobox functionalities in this class
could you please give me some suggestions on this

What I have tried:

oid CMyComboBox::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(::GetSysColor(COLOR_HIGHLIGHTTEXT));
dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
dc.FillSolidRect(&lpDrawItemStruct->rcItem, ::GetSysColor(COLOR_HIGHLIGHT));
}
else
{
dc.FillSolidRect(&lpDrawItemStruct->rcItem, crOldBkColor);
}

// Draw the text.
dc.DrawText(
lpszText,
(int)_tcslen(lpszText),
&lpDrawItemStruct->rcItem,
DT_CENTER|DT_SINGLELINE|DT_VCENTER);

// Reset the background color and the text color back to their
// original values.
dc.SetTextColor(crOldTextColor);
dc.SetBkColor(crOldBkColor);

dc.Detach();
}
Posted

1 solution

To get the DrawItem() function be called you have to make the control an owner drawn one by setting the CBS_OWNERDRAWVARIABLE style flag. This can be done by passing the style when calling Create() or specifying the corresponding option in the resource editor when using the contol in a dialog template.
 
Share this answer
 
Comments
Member 13089825 11-Oct-17 5:33am    
hi i tried to pass CBS_OWNERDRAWVARIABLE in create function but still not getting calling drawitem
BOOL TestDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
MyComboBox m_combobox;
CRect rectDummy;
rectDummy.SetRectEmpty();
//m_combobox = new CJimsUiComboBox();
if (m_combobox.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL | CBS_SORT |
CBS_OWNERDRAWVARIABLE, rectDummy, this, 1))
{
}
Jochen Arndt 11-Oct-17 6:10am    
In the above code you are creating a local instance that is deleted when leaving the function. Remove
MyComboBox m_combobox;

and ensure that you have it as a member in your TestDlg class.
Member 13089825 11-Oct-17 7:50am    
HI thank you for your suggestion
drwaitem is getting calling but im getting some junk data here.
combox have the data is "item0","item 1"(added this in dailog->combobox->Data)
but in drawitem its coming some junk value
DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct->CtlType == ODT_COMBOBOX);
LPCTSTR lpszText = (LPCTSTR)lpDrawItemStruct->itemData;// here getting junk string
ex:'敤'

and please suggest me how to change the color of text and background depending on combobox selected item
could you please suggest me here
Jochen Arndt 11-Oct-17 8:09am    
The itemData member contains the strings that has been added using the CCombobBox member functions AddString or InsertString.
So itemData should contain valid strings and I don't know what is wrong.

Note that the values passed to CCombobBox::SetItemData() are different and not provided in the DRAWITEMSTRUCT.
Member 13089825 11-Oct-17 8:17am    
When im inserting data like below its throwing exception
TestDlg::OnInitDialog()
{

CDialogEx::OnInitDialog();

m_combobox.InsertString(0, L"item 0"); // m_combobox is member ofMyComboBox and declared it in testdlg.h MyComboBox m_combobox;

m_combobox.InsertString(1, L"item 1");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900