Link to home
Start Free TrialLog in
Avatar of IssacJones
IssacJones

asked on

Owner Drawn Combo

Hiya

I have been looking at the following link to create an owner drawn combo:

http://msdn.microsoft.com/en-us/library/y5hb5f9t(VS.71).aspx

The problem is that when I reach ASSERT(lpszText != NULL); the code trips up!

I am using a standard combo with HasStrings set to TRUE. I am using Visual Studio 2008 with Unicode enabled.

Can anybody help?

John
void 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,
      strlen(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();
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of IssacJones
IssacJones

ASKER

Yes, I have checked whether there are items in the combo and used your second condition. Still no luck.

John
This question has been opened for some time and I still haven't resolved it. However, the suggestions from alb and andy helped me so I'm splitting the points amongst them.