다음을 통해 공유


CListBox::DrawItem

소유자 그리기 목록 상자 변경 내용을 시각적 측면이 때 프레임 워크에서 호출 됩니다.

virtual void DrawItem( 
   LPDRAWITEMSTRUCT lpDrawItemStruct  
);

매개 변수

  • lpDrawItemStruct
    하 긴 포인터는 DRAWITEMSTRUCT 필요한 드로잉 유형에 대 한 정보를 포함 하는 구조입니다.

설명

ItemActionitemState 의 멤버는 DRAWITEMSTRUCT 구조 수행 되는 그리기 작업을 정의 합니다.

기본적으로이 함수는 실행 되지 않습니다. 드로잉에 대 한 소유자 그리기를 구현 하려면이 멤버 함수를 재정의 합니다. CListBox 개체입니다. 응용 프로그램에서 디스플레이 컨텍스트를 제공에 대해 선택한 모든 그래픽 장치 인터페이스 (GDI) 개체를 복원 해야 lpDrawItemStruct 전에이 멤버 함수를 종료 합니다.

참조 CWnd::OnDrawItem 에 있는 DRAWITEMSTRUCT 구조.

예제

// CMyODListBox is my owner-drawn list box derived from CListBox. This  
// example draws an item's text centered vertically and horizontally. The  
// list box control was created with the following code: 
//   m_myODListBox.Create( 
//      WS_CHILD|WS_VISIBLE|WS_BORDER|WS_HSCROLL|WS_VSCROLL| 
//      LBS_SORT|LBS_MULTIPLESEL|LBS_OWNERDRAWVARIABLE|LBS_WANTKEYBOARDINPUT, 
//      CRect(10,250,200,450), pParentWnd, IDC_MYODLISTBOX); 
// 
void CMyODListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
   ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);
   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. Also, erase 
   // 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);
   }

   // If this item has the focus, draw a red frame around the 
   // item's rect. 
   if ((lpDrawItemStruct->itemAction | ODA_FOCUS) &&
      (lpDrawItemStruct->itemState & ODS_FOCUS))
   {
      CBrush br(RGB(255, 0, 0));
      dc.FrameRect(&lpDrawItemStruct->rcItem, &br);
   }

   // 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

참고 항목

참조

CListBox 클래스

계층 구조 차트

CListBox::CompareItem

CWnd::OnDrawItem

WM_DRAWITEM

CListBox::MeasureItem

CListBox::DeleteItem