NM_CUSTOMDRAW消息处理函数

原理:
(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(#d93c28);//字体颜色,因该是白色   
             pDC->FillSolidRect(&rect,#567de4);//背景,深蓝色吧,试试               
         }   
        else   
        {
             pDC->SetTextColor(#000000);//字体颜色,因该是白色   
             pDC->FillSolidRect(&rect,#ffffff);//背景,深蓝色吧,试试   
         }
         msize = pDC->GetTextExtent(str);
         pDC->DrawText(str,   &rect,    nFormat); //自绘输出
        *pResult = CDRF_SKIPDEFAULT;
     }
}

你可能感兴趣的:(NM_CUSTOMDRAW消息处理函数)