[CListCtrl]改變儲存格文字顏色

資料也是在網路上找來的,只是急著測試,就沒有記住來源了。所以一定會有雷同,找時間在把內容整理過嚕@@

需要用到NM_CUSTOMDRAW訊息,這要放在Dialog的message map內:
ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST, &CXListCtrl::OnNMCustomdrawLst)

因此要改寫CListCtrl::OnNMCustomdrawLst()
 void CXListCtrl::OnNMCustomdrawLst(NMHDR *pNMHDR, LRESULT *pResult)  
{  
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);  
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);  
// TODO: Add your control notification handler code here  
*pResult = 0;  
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )  
{  
*pResult = CDRF_NOTIFYITEMDRAW;  
}else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage ) {  
//  // This is the prepaint stage for an item. Here's where we set the  
//  // item's text color. Our return value will tell Windows to draw the  
//  // item itself, but it will use the new color we set here.  
//  // We'll cycle the colors through red, green, and light blue.  
*pResult = CDRF_NOTIFYSUBITEMDRAW;        //To Draw sub item request  
} else if ( (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;  
//                Sub Item Index                                    Item Index  
if ( (3 == pLVCD->iSubItem && pLVCD->nmcd.dwItemSpec == 1) ||  
(3 == pLVCD->iSubItem && pLVCD->nmcd.dwItemSpec == 3) )  
{  
crText = RGB(255,0,0);  
//crBkgnd = RGB(128,128,255);  
}  
else if ( 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;  
*pResult = CDRF_NOTIFYPOSTPAINT|CDRF_NOTIFYSUBITEMDRAW;  
}  
}  

No comments:

Post a Comment

Build docker image from multiple build contexts

Build docker image from multiple build contexts ...