MFC list control parity row color different settings

Suppose your List Control is placed on the window XXX, the ID number is IDC_FILELIST, then find the class CXXXDlg.cpp corresponding to the window XXX in the class view, right-click the property and select the icon to add an event,

Find IDC_FILELIST and add NM_CUSTOMDRAW message

Then add the following code in the OnNMCustomdrawFilelist() function:

[cpp]  view plain  copy
  1. void CXXXDlg::OnNMCustomdrawFilelist(NMHDR *pNMHDR, LRESULT *pResult)  
  2. {  
  3.     //LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);  
  4.     // TODO: Add your control notification handler code here  
  5.     *pResult = 0;  
  6.   
  7.     NMLVCUSTOMDRAW* pNMCD = (NMLVCUSTOMDRAW*)(pNMHDR);  
  8.   
  9.     if(CDDS_PREPAINT == pNMCD->nmcd.dwDrawStage)  
  10.     {  
  11.         *pResult = CDRF_NOTIFYITEMDRAW;  
  12.     }  
  13.     elseif (CDDS_ITEMPREPAINT == pNMCD->nmcd.dwDrawStage)   
  14.     {  
  15.         *pResult = CDRF_NOTIFYSUBITEMDRAW;  
  16.     }  
  17.     elseif((CDDS_ITEMPREPAINT|CDDS_SUBITEM)==pNMCD->nmcd.dwDrawStage)   
  18.     {  
  19.         COLORREF   clrNewTextColor, clrNewBkColor;  
  20.         int nItem = static_cast<int>(pNMCD->nmcd.dwItemSpec);  
  21.         if(nItem %2)  
  22.         {  
  23.             clrNewTextColor = RGB(0,0,0);  
  24.             clrNewBkColor = RGB(204,255,255);  
  25.         }  
  26.         else  
  27.         {  
  28.             clrNewTextColor = RGB(0,0,0);  
  29.             clrNewBkColor = RGB(255,255,255);  
  30.         }  
  31.   
  32.         pNMCD->clrText = clrNewTextColor;  
  33.         pNMCD->clrTextBk = clrNewBkColor;  
  34.         *pResult = CDRF_DODEFAULT;  
  35.     }  
  36. }  


It involves two structures, NMLVCUSTOMDRAW and NMCUSTOMDRAW. Their prototypes are as follows:

[cpp]  view plain  copy
  1. typedefstruct tagNMLVCUSTOMDRAW    
  2. {   
  3. NMCUSTOMDRAW nmcd;                 // The structure containing the information of the customer-drawn control   
  4. COLORREF             clrText;              // 列表视图显示文字的颜色   
  5. COLORREF             clrTextBk;          // 列表视图显示文字的背景色   
  6. } NMLVCUSTOMDRAW, *LPNMLVCUSTOMDRAW;  

 

[cpp]  view plain  copy
  1. typedef struct tagNMCUSTOMDRAWINFO {  
  2. NMHDR hdr; //是一个包含NM_CUSTOMDRAW 这个通知消息的NMHDR结构体的句柄  
  3. DWORD dwDrawStage; //指定当前的绘制阶段,共有四个阶段,注释附后  
  4. HDC hdc; // 一个指向控件设备上下文的DC句柄,通过这个hdc可以操作任何GDI函数  
  5. RECT rc; //指定绘制的矩形区域  
  6. DWORD dwItemSpec; //绘制项的说明  
  7. UINT uItemState; //当前项的状态  
  8. LPARAM lItemlParam //应用程序定义的数据  
  9. } NMCUSTOMDRAW, FAR * LPNMCUSTOMDRAW;  
  10.   
  11. 上面涉及到的NMHDR结构  
  12. typedef struct tagNMHDR {   
  13. HWND hwndFrom; //指向控件放送消息的窗口句柄  
  14. UINT idFrom;  //控件发送消息的标识符  
  15. UINT code; //通知代码,不支持NM_RCLICK和NM_RDBCLICK通知代码  
  16. } NMHDR;  
  17.   
  18. NMLVCUSTOMDRAW.nmcd.dwDrawStage字段主要包含如下4种枚举类型:  
  19. CDDS_POSTERASE: 表示在擦除循环结束之后的阶段  
  20. CDDS_POSTPAINT:表示在绘画循环结束之后的阶段  
  21. CDDS_PREERASE: 表示在擦除循环开始之前的阶段  
  22. CDDS_PREPAINT: 表示在绘画前阶段  
  23.   
  24. 指定上面的某个阶段可用的值如下:  
  25. CDDS_ITEMPREPAINT:表示在列表项的绘画前阶段  
  26. CDDS_ITEMPOSTPAINT: 表示在列表项的绘画后阶段  
  27. CDDS_ITEMPOSTERASE: 表示在列表项的擦除后阶段  
  28. CDDS_ITEMPREERASE: 表示在列表项的擦除前阶段  
  29. CDDS_ITEM:表示要绘制项的信息已经可用。  

 

dwDrawStage是用来指定当前绘制的阶段的,那么就是说单一的NM_CUSTOMDRAW处理在每个绘制阶段都进行调用。那么怎么知道当前要绘制的是哪个阶段呢? 或者说绘制了当前阶段之后,下一个阶段是对哪个阶段进行绘制呢?

This is done by setting the second parameter pResult of the NM_CUSTOMDRAW message function. In fact, if the pResult value is not set, then when the CDDS_PREPAINT function is called in the initial stage, the NM_CUSTOMDRAW message function will not be called anymore, because you have not passed it which stage of the next stage of the drawing operation.

Technically, there are only two phases that need to be specified for the draw phase (CDDS_PAREPAINT and CDDS_ITEMPREPAINT) because they affect the content of the sent message. In general, it is usually only necessary to specify at the end of the handler which drawing phase the code will handle. The following are the values ​​used to specify the desired draw phase:

CDRF_DEFAULT : Instructs the control to draw itself. Change the value to the default value, it should not be used in combination with other values

CDRF_SKIPDEFAULT: Used to specify that the control does not draw anything at all

CDRF_NEWFONT: used when code changes the font of a draw item/child

CDRF_NOTIFYPOSTPAINT: Causes notification messages to be sent after the control or each item/child is painted

CDRF_NOTIFYITEMDRAW: Indicates that the item (or child) will be drawn. Note that the value below it is the same as CDRF_NOTIFYSUBITEMDRAW

CDRF_NOTIFYSUBITEMDRAW: Indicates that the child (or items) will be drawn. Note that the value below it is the same as CDRF_NOTIFYITEMDRAW

CDRF_NOTIFYPOSTERASE: used when a notification code is required after the control is removed

 

Reprinted from: http://blog.csdn.net/xiaolongwang2010/article/details/11891331

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325866021&siteId=291194637