Chinaunix首页 | 论坛 | 博客
  • 博客访问: 142089
  • 博文数量: 10
  • 博客积分: 2431
  • 博客等级: 大尉
  • 技术积分: 266
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-02 00:56
文章分类
文章存档

2016年(1)

2013年(2)

2011年(3)

2010年(4)

分类: C/C++

2011-08-08 14:33:21

VC控件的CListCtrl可以被选择成Owner draw模式,这个模式下需要自己来画图标和文字。一个简单的DrawItem函数重载实现:[原创程序,转载请标明出处]
  1. void CMyCtrlList::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
  2. {
  3.     int nItem = lpDrawItemStruct->itemID;
  4.     CRect rcItem;
  5.     GetItemRect( nItem, rcItem, LVIR_LABEL );
  6.     rcItem.left += 2;

  7.     LV_ITEM lvi;
  8.     lvi.mask = LVIF_STATE | LVIF_IMAGE;
  9.     lvi.iItem = nItem;
  10.     lvi.iSubItem = 0;
  11.     lvi.stateMask = 0xFFFF;
  12.     VERIFY( GetItem( &lvi ) );

  13.     UINT nFormat = DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER;
  14.     nFormat |= DT_LEFT; // Justify
  15.     CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
  16.     BOOL fFocus = ( GetFocus() == this );
  17.     CString sText ( CListCtrl::GetItemText( nItem, 0 ) );
  18.     if( lvi.state & LVIS_FOCUSED )
  19.     {
  20.         CBrush brush;
  21.         if( fFocus )
  22.         {
  23.             brush.CreateSolidBrush( GetSysColor( COLOR_HIGHLIGHT ) );
  24.         }
  25.         else
  26.         {
  27.             brush.CreateSolidBrush( GetSysColor( COLOR_INACTIVECAPTIONTEXT ) );
  28.         }
  29.         CBrush* pOldBrush = static_cast<CBrush*>( pDC->SelectObject( brush ) );
  30.         pDC->FillRect( rcItem, &brush );
  31.         pDC->SelectObject( pOldBrush );

  32.         if( fFocus )
  33.         {
  34.             COLORREF colorRef = pDC->SetTextColor( GetSysColor( COLOR_HIGHLIGHTTEXT ) );
  35.             pDC->DrawText( sText, -1, &rcItem, nFormat );
  36.             pDC->SetTextColor( colorRef );
  37.         }
  38.         else // We have 3 execution path, so need 2 boolean split.
  39.         { // So this else is not redundant.
  40.             pDC->DrawText( sText, -1, &rcItem, nFormat );
  41.         }
  42.     }
  43.     else
  44.     {
  45.         pDC->DrawText( sText, -1, &rcItem, nFormat );
  46.     }
  47.     CRect rcImage;
  48.     GetSubItemRect( lpDrawItemStruct->itemID, 0, LVIR_ICON, rcImage );
  49.     CImageList *pImageList = GetImageList( LVSIL_SMALL );
  50.     if( pImageList )
  51.     {
  52.         COLORREF colorRef = {0};
  53.         UINT nStyle = ILD_NORMAL;
  54.         BOOL fFocusAndSelect = ( lvi.state & LVIS_FOCUSED ) && fFocus;
  55.         if( fFocusAndSelect )
  56.         {
  57.             nStyle = ILD_SELECTED | ILD_TRANSPARENT;
  58.             colorRef = pImageList->SetBkColor( GetSysColor( COLOR_HIGHLIGHT ) );
  59.         }
  60.         IMAGEINFO imageinfo;
  61.         if( pImageList->GetImageInfo( lvi.iImage, &imageinfo ) )
  62.         {
  63.             pImageList->DrawIndirect(
  64.                 pDC,
  65.                 lvi.iImage,
  66.                 CPoint( rcImage.left, rcImage.top ),
  67.                 CSize( imageinfo.rcImage.right - imageinfo.rcImage.left, imageinfo.rcImage.bottom - imageinfo.rcImage.top ),
  68.                 CPoint( 0, 0 ),
  69.                 nStyle );
  70.         }
  71.         if( fFocusAndSelect )
  72.         {
  73.             pImageList->SetBkColor( colorRef );
  74.         }
  75.     }
  76. }

阅读(6879) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~