CComboBox::DrawItem

调用由结构,当所有者描述组合框的可视方面是更改。

virtual void DrawItem( 
   LPDRAWITEMSTRUCT lpDrawItemStruct  
);

参数

  • lpDrawItemStruct
    对包含有关所需的绘图的类型的信息 DRAWITEMSTRUCT 结构的指针。

备注

DRAWITEMSTRUCT 结构的 itemAction 成员定义要执行的绘制事件。 对于此结构的声明参见 CWnd::OnDrawItem

默认情况下,此成员函数不执行任何操作。 重写该成员函数的实现所有者描述 CComboBox 对象的绘图。 此成员函数终止之前,应用程序应恢复为显示上下文中选择的所有图形设备接口(GDI)对象提供了在 lpDrawItemStruct。

示例

// CMyComboBox is my owner-drawn combo box derived from CComboBox. This  
// example draws an item's text centered vertically and horizontally. The  
// combo box control was created with the following code: 
//   pmyComboBox->Create( 
//      WS_CHILD|WS_VISIBLE|WS_BORDER|WS_HSCROLL|WS_VSCROLL| 
//      CBS_SORT|CBS_OWNERDRAWVARIABLE, 
//      myRect, pParentWnd, 1); 
// 
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,
      (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();
}

要求

Header: afxwin.h

请参见

参考

CComboBox Class

层次结构图

CComboBox::CompareItem

WM_DRAWITEM

CComboBox::MeasureItem

CComboBox::DeleteItem