次の方法で共有


CComboBox::DrawItem

更新 : 2007 年 11 月

オーナー描画コンボ ボックスの外観が変更されたときに、フレームワークによって呼び出されます。

virtual void DrawItem(
   LPDRAWITEMSTRUCT lpDrawItemStruct 
);

パラメータ

  • lpDrawItemStruct
    要求する描画種別に関する情報を持つ DRAWITEMSTRUCT 構造体へのポインタ。

解説

DRAWITEMSTRUCT 構造体の itemAction メンバは、実行する描画動作を定義します。この構造体については、「CWnd::OnDrawItem」を参照してください。

既定では、このメンバ関数は何も実行しません。オーナー描画の CComboBox オブジェクトの描画を実装するには、このメンバ関数をオーバーライドします。このメンバ関数が終了する前に、アプリケーションでは lpDrawItemStruct で指定されているディスプレイ コンテキストに対して選択した、すべてのグラフィカル デバイス インターフェイス (GDI: Graphical Device Interface) オブジェクトを元に戻しておく必要があります。

使用例

// 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();
}

必要条件

ヘッダー : afxwin.h

参照

参照

CComboBox クラス

階層図

CComboBox::CompareItem

WM_DRAWITEM

CComboBox::MeasureItem

CComboBox::DeleteItem

その他の技術情報

CComboBox のメンバ