博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CListCtrl列表中,改写几列的文字颜色
阅读量:4111 次
发布时间:2019-05-25

本文共 4696 字,大约阅读时间需要 15 分钟。

ListCtrl控件添加NM_CUSTOMDRAW消息

 VC6没有ListCtrl控件的NM_CUSTOMDRAW属性,要自己添加,添加的步骤:

1、Message Map

BEGIN_MESSAGE_MAP( ... , ...)

        //{
{AFX_MSG_MAP(CCustomDrawDlg)
        // ......
       ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST, OnNMCustomdrawList)
                 // Add here
                 // IDC_LIST Specify the ID of the  Ctrl
                 // OnNMCustomdrawCtrl  Specify the function name
       //}}AFX_MSG_MAP
END_MESSAGE_MAP()

2、类的声明中添加

 afx_msg void OnNMCustomdrawList(NMHDR *pNMHDR, LRESULT *pResult);

3、对应的cpp文件中添加定义

 

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

在ListCtrl控件绘画前处理NM_CUSTOMDRAW消息。

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

  1. void CMyDlg::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult )  
  2. {  
  3.   NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );     // Take the default processing unless we set this to something else below.       
  4.   *pResult = CDRF_DODEFAULT;     // First thing - check the draw stage. If it's the control's prepaint       
  5.   // stage, then tell Windows we want messages for every item.        
  6.   if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )  
  7.   {  
  8.       *pResult = CDRF_NOTIFYITEMDRAW;  
  9.   }      
  10.   elseif ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )          
  11.   {  
  12.           // This is the notification message for an item. We'll request           
  13.           // notifications before each subitem's prepaint stage.            
  14.           *pResult = CDRF_NOTIFYSUBITEMDRAW;          
  15.   }      
  16.   elseif ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )          
  17.   {  
  18.           // This is the prepaint stage for a subitem. Here's where we set the           
  19.           // item's text and background colors. Our return value will tell            
  20.           // Windows to draw the subitem itself, but it will use the new colors           
  21.           // we set here.           
  22.           // The text color will cycle through red, green, and light blue.           
  23.           // The background color will be light blue for column 0, red for           
  24.           // column 1, and black for column 2.               
  25.             
  26.           COLORREF crText, crBkgnd;                  
  27.           if ( 0 == pLVCD->iSubItem )  
  28.           {              
  29.             crText = RGB(255,0,0);              
  30.             crBkgnd = RGB(128,128,255);              
  31.           }          
  32.           elseif ( 1 == pLVCD->iSubItem )              
  33.           {           
  34.              crText = RGB(0,255,0);              
  35.              crBkgnd = RGB(255,0,0);              
  36.           }          
  37.           else              
  38.           {            
  39.              crText = RGB(128,128,255);              
  40.              crBkgnd = RGB(0,0,0);            
  41.           }           
  42.           // Store the colors back in the NMLVCUSTOMDRAW struct.           
  43.             
  44.           pLVCD->clrText = crText;          
  45.           pLVCD->clrTextBk = crBkgnd;           
  46.             
  47.           // Tell Windows to paint the control itself.           
  48.           *pResult = CDRF_DODEFAULT;          
  49.    }  
  50. }  
void CMyDlg::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult ){  NMLVCUSTOMDRAW* pLVCD = reinterpret_cast
( 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段。

 

 

转载地址:http://cdlsi.baihongyu.com/

你可能感兴趣的文章
C++模板
查看>>
【C#】如何实现一个迭代器
查看>>
【C#】利用Conditional属性完成编译忽略
查看>>
DirectX11 光照演示示例Demo
查看>>
VUe+webpack构建单页router应用(一)
查看>>
Node.js-模块和包
查看>>
(python版)《剑指Offer》JZ01:二维数组中的查找
查看>>
管理用户状态——Cookie与Session
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
PHP 7 的五大新特性
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
【增强学习在无人驾驶中的应用】
查看>>
OpenCV meanshift目标跟踪总结
查看>>
就在昨天,全球 42 亿 IPv4 地址宣告耗尽!
查看>>
卧槽!Java 虚拟机竟然还有这些性能调优技巧...
查看>>
听说玩这些游戏能提升编程能力?
查看>>
如果你还不了解 RTC,那我强烈建议你看看这个!
查看>>