共用方式為


CComboBox::DrawItem

呼叫框架,其在主控描繪下拉式方塊的視覺外觀變更。

virtual void DrawItem(
   LPDRAWITEMSTRUCT lpDrawItemStruct 
);

參數

  • lpDrawItemStruct
    out 包含針對需要的繪圖的資訊的 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 類別

階層架構圖

CComboBox::CompareItem

WM_DRAWITEM

CComboBox::MeasureItem

CComboBox::DeleteItem