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

本文介绍了一个使用C++实现的自定义列表视图绘制方法,通过重写OnCustomdrawMyList函数,根据不同子项设置不同的文本颜色和背景颜色,实现了列表视图中项目的个性化显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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 }
View Code

 

转载于:https://www.cnblogs.com/cwbo-win/articles/3393880.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值