VC控件的CListCtrl可以被选择成Owner draw模式,这个模式下需要自己来画图标和文字。一个简单的DrawItem函数重载实现:[原创程序,转载请标明出处]
- void CMyCtrlList::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
-
{
-
int nItem = lpDrawItemStruct->itemID;
-
CRect rcItem;
-
GetItemRect( nItem, rcItem, LVIR_LABEL );
-
rcItem.left += 2;
-
-
LV_ITEM lvi;
-
lvi.mask = LVIF_STATE | LVIF_IMAGE;
-
lvi.iItem = nItem;
-
lvi.iSubItem = 0;
-
lvi.stateMask = 0xFFFF;
-
VERIFY( GetItem( &lvi ) );
-
-
UINT nFormat = DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER;
-
nFormat |= DT_LEFT; // Justify
-
CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
-
BOOL fFocus = ( GetFocus() == this );
-
CString sText ( CListCtrl::GetItemText( nItem, 0 ) );
-
if( lvi.state & LVIS_FOCUSED )
-
{
-
CBrush brush;
-
if( fFocus )
-
{
-
brush.CreateSolidBrush( GetSysColor( COLOR_HIGHLIGHT ) );
-
}
-
else
-
{
-
brush.CreateSolidBrush( GetSysColor( COLOR_INACTIVECAPTIONTEXT ) );
-
}
-
CBrush* pOldBrush = static_cast<CBrush*>( pDC->SelectObject( brush ) );
-
pDC->FillRect( rcItem, &brush );
-
pDC->SelectObject( pOldBrush );
-
-
if( fFocus )
-
{
-
COLORREF colorRef = pDC->SetTextColor( GetSysColor( COLOR_HIGHLIGHTTEXT ) );
-
pDC->DrawText( sText, -1, &rcItem, nFormat );
-
pDC->SetTextColor( colorRef );
-
}
-
else // We have 3 execution path, so need 2 boolean split.
-
{ // So this else is not redundant.
-
pDC->DrawText( sText, -1, &rcItem, nFormat );
-
}
-
}
-
else
-
{
-
pDC->DrawText( sText, -1, &rcItem, nFormat );
-
}
-
CRect rcImage;
-
GetSubItemRect( lpDrawItemStruct->itemID, 0, LVIR_ICON, rcImage );
-
CImageList *pImageList = GetImageList( LVSIL_SMALL );
-
if( pImageList )
-
{
-
COLORREF colorRef = {0};
-
UINT nStyle = ILD_NORMAL;
-
BOOL fFocusAndSelect = ( lvi.state & LVIS_FOCUSED ) && fFocus;
-
if( fFocusAndSelect )
-
{
-
nStyle = ILD_SELECTED | ILD_TRANSPARENT;
-
colorRef = pImageList->SetBkColor( GetSysColor( COLOR_HIGHLIGHT ) );
-
}
-
IMAGEINFO imageinfo;
-
if( pImageList->GetImageInfo( lvi.iImage, &imageinfo ) )
-
{
-
pImageList->DrawIndirect(
-
pDC,
-
lvi.iImage,
-
CPoint( rcImage.left, rcImage.top ),
-
CSize( imageinfo.rcImage.right - imageinfo.rcImage.left, imageinfo.rcImage.bottom - imageinfo.rcImage.top ),
-
CPoint( 0, 0 ),
-
nStyle );
-
}
-
if( fFocusAndSelect )
-
{
-
pImageList->SetBkColor( colorRef );
-
}
-
}
-
}
阅读(6879) | 评论(0) | 转发(1) |