CListCtrl列表中,改写几列的文字颜色

CListCtrl列表中,改写几列的文字颜色

在ListCtrl控件绘画前处理NM_CUSTOMDRAW消息。
告诉Windows我们想对每个Item处理NM_CUSTOMDRAW消息。当这些消息中的一个到来,告诉Windows我们想在每个SubItem的绘制前处理这个消息当这些消息到达,我们就为每个SubItem设置文字和背景的颜色。

void CMyDlg::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult )
{
  NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );     // Take the default processing unless we set this to something else below.    
  *pResult = CDRF_DODEFAULT;     // First thing - check the draw stage. If it's the control's prepaint    
  // stage, then tell Windows we want messages for every item.     
  if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
  {
      *pResult = CDRF_NOTIFYITEMDRAW;
  }    
  elseif ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )        
  {
          // This is the notification message for an item. We'll request        
          // notifications before each subitem's prepaint stage.         
          *pResult = CDRF_NOTIFYSUBITEMDRAW;        
  }    
  elseif ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )        
  {
          // This is the prepaint stage for a subitem. Here's where we set the        
          // item's text and background colors. Our return value will tell         
          // Windows to draw the subitem itself, but it will use the new colors        
          // we set here.        
          // The text color will cycle through red, green, and light blue.        
          // The background color will be light blue for column 0, red for        
          // column 1, and black for column 2.            
          
          COLORREF crText, crBkgnd;                
          if ( 0 == pLVCD->iSubItem )
          {            
            crText = RGB(255,0,0);            
            crBkgnd = RGB(128,128,255);            
          }        
          elseif ( 1 == pLVCD->iSubItem )            
          {         
             crText = RGB(0,255,0);            
             crBkgnd = RGB(255,0,0);            
          }        
          else            
          {          
             crText = RGB(128,128,255);            
             crBkgnd = RGB(0,0,0);          
          }         
          // Store the colors back in the NMLVCUSTOMDRAW struct.        
          
          pLVCD->clrText = crText;        
          pLVCD->clrTextBk = crBkgnd;         
          
          // Tell Windows to paint the control itself.        
          *pResult = CDRF_DODEFAULT;        
   }
}

 

 

这里需要注意两件事:

l         clrTextBk的颜色只是针对每一列,在最后一列的右边那个区域颜色也还是和ListCtrl控件的背景颜色一致。l         当我重新看文档的时候,我注意到有一篇题目是“NM_CUSTOMDRAW (list view)”的文章,它说你可以在最开始的custom draw消息中返回CDRF_NOTIFYSUBITEMDRAW就可以处理SubItem了,而不需要在CDDS_ITEMPREPAINT绘画段中去指定CDRF_NOTIFYSUBITEMDRAW。但是我试了一下,发现这种方法并不起作用,你还是需要处理CDDS_ITEMPREPAINT段。

处理“绘画之后”的段
      到限制为止的例子都是处理“绘画前”的段,当 Windows 绘制 List Item 之前就改变它的外观。然而,在“绘制前”,你的绘制行为时被限制的,你只能改变字体的颜色或者外观。如果你想改变图标的绘制,你可以在“绘画前”把整个 Item 重画或者在“绘画后”去做这件事。当你做在绘画后去做“自定义绘画”是,你的“绘画处理函数”就会在 Windows 画完整个 Item 或者 SubItem 的时候被调用,你就可以随心所欲的乱画了!!
      在这个例子里,我将创建一个 ListCtrl ,一般的 ListCtrl 的 Item 如果被选择了,则其 Icon 也会呈现出被选择的状态。而我创建的这个 ListCtrl 的 Icon 是不会呈现被选择的状态的。步骤如下:

对ListCtrl在“绘画前”处理NM_CUSTOMDRAW消息。告诉Windows我们想在每个Item被画的时候获得NM_CUSTOMDRAW消息。当这些消息来临,告诉Windows我们想在你画完的时候获取NM_CUSTOMDRAW消息。当这些消息来到的时候,我们就重新画每一个Item的图标。

 

void CPanel3::OnCustomdrawList ( NMHDR* pNMHDR, LRESULT* pResult )
{
 NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
    
    *pResult = 0;
 
    // If this is the beginning of the control's paint cycle, request
    // notifications for each item.
 
    if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
 {
        *pResult = CDRF_NOTIFYITEMDRAW;
 }
    else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
 {
        // This is the pre-paint stage for an item.  We need to make another
        // request to be notified during the post-paint stage.
  
        *pResult = CDRF_NOTIFYPOSTPAINT;
 }
    else if ( CDDS_ITEMPOSTPAINT == pLVCD->nmcd.dwDrawStage )
 {
        // If this item is selected, re-draw the icon in its normal
        // color (not blended with the highlight color).
        LVITEM rItem;
        int    nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
  
        // Get the image index and state of this item.  Note that we need to
        // check the selected state manually.  The docs _say_ that the
        // item's state is in pLVCD->nmcd.uItemState, but during my testing
        // it was always equal to 0x0201, which doesn't make sense, since
        // the max CDIS_ constant in commctrl.h is 0x0100.
  
        ZeroMemory ( &rItem, sizeof(LVITEM) );
        rItem.mask  = LVIF_IMAGE | LVIF_STATE;
        rItem.iItem = nItem;
        rItem.stateMask = LVIS_SELECTED;
        m_list.GetItem ( &rItem );
  
        // If this item is selected, redraw the icon with its normal colors.
  
        if ( rItem.state & LVIS_SELECTED )
  {
            CDC*  pDC = CDC::FromHandle ( pLVCD->nmcd.hdc );
            CRect rcIcon;
   
            // Get the rect that holds the item's icon.
            m_list.GetItemRect ( nItem, &rcIcon, LVIR_ICON );
   
            // Draw the icon.
            m_imglist.Draw ( pDC, rItem.iImage, rcIcon.TopLeft(),
    ILD_TRANSPARENT );
   
            *pResult = CDRF_SKIPDEFAULT;
  }
 }
} 


 

重复,custom draw让我们可以做尽可能少的工作,上面的例子就是让Windows帮我们做完全部的工作,然后我们就重新对选择状态的Item的图标做重画,那就是我们看到的那个图标。执行结果如下:

 

唯一的不足是,这样的方法会让你感觉到一点闪烁。因为图标被画了两次(虽然很快)。
 
用Custom Draw代替Owner Draw
另外一件优雅的事情就是你可以使用Custom Draw来代替Owner Draw。它们之间的不同在我看来就是:
l         写Custom Draw的代码比写Owner Draw的代码更容易。
如果你只需要改变某行的外观,你可以不用管其他的行的绘画,让WINDOWS去做就行了。但如果你使用
Owner Draw,你必须要对所有的行作处理。当你想对控件作所有的处理时,你可以在处理 NM_CUSTOMDRAW
消息的最后返回 CDRF_SKIPDEFAULT,这有点和我们到目前为止所做的有些不同。 CDRF_SKIPDEFAULT
告诉Windows由我们来做所有的控件绘画,你不用管任何事。
我没有在这里包含这个例子的代码,因为它有点长,但是你可以一步步地在调试器中调试代码,你可以看到每一
步发生了什么。如果你把窗口摆放好,让你可以看到调试器和演示的程序,那在你一步步的调试中,你可以看到
控件每一步的绘制,这里的ListCtrl是很简单的,只有一列并且没有列头,如下:

 


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值