松鼠的博客

导航

CListCtrl自绘项

原理:
(1)在NM_CUSTOMDRAW消息处理函数中根据 dwDrawStage 状态来编写不同的处理代码
(2)主要是判断 CDDS_ITEMPREPAINT 状态
(3)不要在对话框资源管理器中设置CListCtrl控件的Owner draw fixed属性,否则在Debug模式下报错。

方法一,使用NM_CUSTOMDRAW消息映射
(1)声明消息处理函数:afx_msg void OnCustomDrawList ( NMHDR* pNMHDR, LRESULT* pResult );
(1)手动添加消息映射:ON_NOTIFY_REFLECT ( NM_CUSTOMDRAW, OnCustomDrawList )
(2)编写自绘消息处理函数

void CListCtrl2::OnCustomDrawList ( NMHDR* pNMHDR, LRESULT* pResult )
{     
    NMLVCUSTOMDRAW
*   pLVCD   =   reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);   
    
*pResult = CDRF_DODEFAULT; 
  
    int rowIndex = static_cast<int>(pLVCD->nmcd.dwItemSpec);
    
if(pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)   
    
{   
        
*pResult = CDRF_NOTIFYITEMDRAW;   
    }
   
    
else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)   
    
{     
        
*pResult = CDRF_NOTIFYSUBITEMDRAW;   
    }
//画项
    else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))   
    
{
        
int nitem = static_cast<int>   (pLVCD->nmcd.dwItemSpec);   
        
int nsubitem = pLVCD->iSubItem;   
        CDC
* pDC = CDC::FromHandle(pLVCD->nmcd.hdc);  
        CString str;   
        CRect rect;
        CSize msize; 
        UINT nFormat 
= DT_VCENTER | DT_SINGLELINE;   
        rect.left 
+= 3//调整自绘输出位置   
        
//get   rect   of   the   sub   item   which   is   going   to   paint   
        GetSubItemRect(nitem,nsubitem,LVIR_BOUNDS,rect);     
        
//get   the   context   is   going   to   paint   on   the   subitem   
        str = GetItemText(nitem,nsubitem);   

        
//如果选中
        if(LVIS_SELECTED == this->GetItemState(rowIndex,LVIS_SELECTED)//判断当前项是否选中   
        
            
//所画项是选中项   
            pDC->SetTextColor(RGB(217,60,40));//字体颜色,因该是白色   
            pDC->FillSolidRect(&rect,RGB(86,125,228));//背景,深蓝色吧,试试               
        }
   
        
else   
        
{
            pDC->SetTextColor(RGB(0,0,0));//字体颜色,因该是白色   
            pDC->FillSolidRect(&rect,RGB(255,255,255));//背景,深蓝色吧,试试   
        }

        msize 
= pDC->GetTextExtent(str);
        pDC
->DrawText(str,   &rect,   nFormat); //自绘输出
        *pResult = CDRF_SKIPDEFAULT;
    }

}

 

相关参考:
Defining a Message Handler for a Reflected Message
TN062: Message Reflection for Windows Controls
http://blog.csdn.net/pandera/archive/2006/01/24/587382.aspx

posted on 2008-12-10 14:02  Xproer-松鼠  阅读(2186)  评论(0)    收藏  举报