CListCtrl修改指定行的颜色

添加自己的列表控件类CMyListCtrl m_List派生CListCtrl.

//添加消息
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CMyListCtrl::OnNMCustomdraw)
//函数实现
void CMyListCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
	 /*修改内容: 
	   改用switch case语句
	   case语句中增加了CDDS_ITEMPREPAINT和CDDS_ITEMPOSTPAINT两个状态判断
	   同时在改变颜色时增加了一个函数SetItemState
	   */
	 static BOOL s_bThisItemSelect = FALSE;   
	 NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR); 
	 int nItemIndex=pLVCD->nmcd.dwItemSpec;
	 *pResult = CDRF_DODEFAULT;   
	 switch (pLVCD->nmcd.dwDrawStage) 
	 {   
	 case CDDS_PREPAINT:  
		 *pResult = CDRF_NOTIFYITEMDRAW;  
		 break;   
	 case CDDS_ITEMPREPAINT:  
		 {    
			 UINT ordin = pLVCD->nmcd.lItemlParam; 
			 s_bThisItemSelect = FALSE; 
			 //选中行颜色改变
			 if (GetItemState(nItemIndex,LVIS_SELECTED) == LVIS_SELECTED/*pLVCD->nmcd.uItemState & CDIS_SELECTED*/)
			 {      
				 s_bThisItemSelect = TRUE; 
				 //增加了下面这个SetItemState函数
				 SetItemState(pLVCD->nmcd.dwItemSpec, 0, LVIS_SELECTED); 
				 pLVCD->clrText = RGB(0,0,255)/*m_SelectItemTextColor*/;     
				 pLVCD->clrTextBk =RGB(255,128,0)/*m_SelectItemBkColor*/;   
			 }          
			 
             else if((nItemIndex==m_nHoverIndex))
			 { //热点行
				 pLVCD->clrTextBk=m_HoverItemBkColor;
				 pLVCD->clrText=m_HoverItemTextColor;
			 }
			 else if(nItemIndex % 2==0)
			 {//偶数行 比如 0、2、4、6
				 pLVCD->clrTextBk=m_EvenItemBkColor;
				 pLVCD->clrText=m_EvenItemTextColor;
			 }
			 else
			 {	//奇数行 比如 1、3、5、7
				 pLVCD->clrTextBk=m_OddItemBkColor;
				 pLVCD->clrText=m_OddItemTextColor;
			 }
		 *pResult = CDRF_NOTIFYPOSTPAINT;    
	}       
		 break;    
		 //加了一个状体判断
	 case CDDS_ITEMPOSTPAINT:   
		 if (s_bThisItemSelect) 
	 	 SetItemState(pLVCD->nmcd.dwItemSpec, 0xFF, LVIS_SELECTED);  
		 break;  
	 } 
	
}

 
 
//添加PreSubclassWindow
void CMyListCtrl::PreSubclassWindow()
{
	// TODO: Add your specialized code here and/or call the base class

	 ModifyStyle(LVS_SHOWSELALWAYS, LVS_SINGLESEL);    // TRICKY!!! can`t with LVS_SHOWSELALWAYS 
	 SetExtendedStyle(GetExtendedStyle()|LVS_EX_FULLROWSELECT);    
	 CListCtrl::PreSubclassWindow(); 
}

我空间代码可以自己看下。 http://download.csdn.net/detail/lcalqf/7815847

你可能感兴趣的:(ListCtrl)