设置ListCtrl列表控件其中某一行的字体和背景颜色

来源:互联网 发布:linux drop cache 编辑:程序博客网 时间:2024/06/06 00:31

请参阅:http://blog.csdn.net/leixiaohua1020/article/details/12619341


设置ListCtrl列表控件其中某一行的字体和背景颜色,可以最终达到如下效果:


操作步骤如下所示:

1.先添加一个自定义消息

[cpp] view plain copy
  1. ON_NOTIFY ( NM_CUSTOMDRAW,IDC_V_H264_NALLIST, OnCustomdrawMyList )  

注:在BEGIN_MESSAGE_MAP()和END_MESSAGE_MAP()之间。第二个参数是LIstCtrl的ID,第三个参数是消息响应函数。

2.下面是具体的函数。根据表格内容的不同,设置不同的背景颜色。

比如:“NAL负载类型”为SLICE的时候,背景为青色;为SPS的时候,背景为黄色;为PPS的时候,背景为咖啡色。

[cpp] view plain copy
  1. //ListCtrl加颜色  
  2. void CSpecialVH264Dlg::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult )  
  3. {  
  4.     //This code based on Michael Dunn's excellent article on  
  5.     //list control custom draw at http://www.codeproject.com/listctrl/lvcustomdraw.asp  
  6.   
  7.   
  8.     NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );  
  9.   
  10.   
  11.     // Take the default processing unless we set this to something else below.  
  12.     *pResult = CDRF_DODEFAULT;  
  13.   
  14.   
  15.     // First thing - check the draw stage. If it's the control's prepaint  
  16.     // stage, then tell Windows we want messages for every item.  
  17.     if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )  
  18.     {  
  19.         *pResult = CDRF_NOTIFYITEMDRAW;  
  20.     }  
  21.     else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )  
  22.     {  
  23.         // This is the notification message for an item.  We'll request  
  24.         // notifications before each subitem's prepaint stage.  
  25.   
  26.   
  27.         *pResult = CDRF_NOTIFYSUBITEMDRAW;  
  28.     }  
  29.     else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )  
  30.     {  
  31.   
  32.   
  33.         COLORREF clrNewTextColor, clrNewBkColor;  
  34.   
  35.   
  36.         int    nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );  
  37.   
  38.   
  39.         CString strTemp = m_vh264nallist.GetItemText(nItem,2);  
  40.         if(strcmp(strTemp,"SLICE")==0){  
  41.             clrNewTextColor = RGB(0,0,0);       //Set the text   
  42.             clrNewBkColor = RGB(0,255,255);     //青色  
  43.         }  
  44.         else if(strcmp(strTemp,"SPS")==0){  
  45.             clrNewTextColor = RGB(0,0,0);       //text   
  46.             clrNewBkColor = RGB(255,255,0);     //黄色  
  47.         }  
  48.         else if(strcmp(strTemp,"PPS")==0){  
  49.             clrNewTextColor = RGB(0,0,0);       //text  
  50.             clrNewBkColor = RGB(255,153,0);     //咖啡色  
  51.         }else if(strcmp(strTemp,"SEI")==0){  
  52.             clrNewTextColor = RGB(0,0,0);       //text  
  53.             clrNewBkColor = RGB(255,66,255);            //粉红色  
  54.         }else if(strcmp(strTemp,"IDR_SLICE")==0){  
  55.             clrNewTextColor = RGB(0,0,0);       //text  
  56.             clrNewBkColor = RGB(255,0,0);           //红色  
  57.         }else{  
  58.             clrNewTextColor = RGB(0,0,0);       //text  
  59.             clrNewBkColor = RGB(255,255,255);           //白色  
  60.         }  
  61.   
  62.   
  63.         pLVCD->clrText = clrNewTextColor;  
  64.         pLVCD->clrTextBk = clrNewBkColor;  
  65.   
  66.   
  67.   
  68.   
  69.         // Tell Windows to paint the control itself.  
  70.         *pResult = CDRF_DODEFAULT;  
  71.   
  72.   
  73.   
  74.   
  75.     }  
  76. }  

0 0