MSDN的例子: CListBox::DrawItem virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );ParameterslpDrawItemStructA long pointer to a DRAWITEMSTRUCT structure that contains information about the type of drawing required.ResCalled by the framework when a visual aspect of an owner-draw list box changes. The itemAction and itemState members of the DRAWITEMSTRUCT structure define the drawing action that is to be performed. By default, this member function does nothing. Override this member function to implement drawing for an owner-draw CListBox object. The application should restore all graphics device interface (GDI) objects selected for the display context supplied in lpDrawItemStruct before this member function terminates.See CWnd::OnDrawItem for a description of the DRAWITEMSTRUCT structure.Example // CMyListBox 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: // pmyListBox->Create( // WS_CHILD|WS_VISIBLE|WS_BORDER|WS_HSCROLL|WS_VSCROLL| // LBS_SORT|LBS_MULTIPLESEL|LBS_OWNERDRAWVARIABLE, // myRect, pParentWnd, 1); // void CMyListBox::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, 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(); }
CListBox::DrawItem
virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );ParameterslpDrawItemStructA long pointer to a DRAWITEMSTRUCT structure that contains information about the type of drawing required.ResCalled by the framework when a visual aspect of an owner-draw list box changes. The itemAction and itemState members of the DRAWITEMSTRUCT structure define the drawing action that is to be performed. By default, this member function does nothing. Override this member function to implement drawing for an owner-draw CListBox object. The application should restore all graphics device interface (GDI) objects selected for the display context supplied in lpDrawItemStruct before this member function terminates.See CWnd::OnDrawItem for a description of the DRAWITEMSTRUCT structure.Example
// CMyListBox 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:
// pmyListBox->Create(
// WS_CHILD|WS_VISIBLE|WS_BORDER|WS_HSCROLL|WS_VSCROLL|
// LBS_SORT|LBS_MULTIPLESEL|LBS_OWNERDRAWVARIABLE,
// myRect, pParentWnd, 1);
//
void CMyListBox::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,
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();
}
但是我不知道在哪儿调用这个函数,也不知道怎么调用。
因为我添加的listctl控件在*dlg.cpp文件中初始化的,我觉得我应该在对话框的初始化函数中就调用列表控件的重绘函数,但是不知道怎么调用,请各位指点