CtrlList每行不同颜色

来源:互联网 发布:程序侠淘宝客程序源码 编辑:程序博客网 时间:2024/04/27 07:45
//消息映射中添加
ON_NOTIFY(NM_CUSTOMDRAW,IDC_LIST_DATA,OnCustomdrawListCtrl)
//头文件添加
afx_msg void OnCustomdrawListCtrl(NMHDR *pNMHDR, LRESULT *pResult);

/************************************************************************/
/* CtrlList每行不同颜色                                                                     
/***********************************************************************
*/
void CDbMiddleSrvDlg::OnCustomdrawListCtrl(NMHDR *pNMHDR, LRESULT *pResult)
{
    NMLVCUSTOMDRAW
* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
    
// Take the default processing unless we set this to something else below.
    *pResult = CDRF_DODEFAULT;
    
    m_ctrlListCtrlData.SetExtendedStyle(m_ctrlListCtrlData.GetExtendedStyle() 
| LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
    
//
    
// First thing - check the draw stage. If it's the control's prepaint
    
// stage, then tell Windows we want messages for every item.
    
//m_ctrlListCtrlData.SetBkColor(RGB(0,0,0));

    
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
    {
        
*pResult = CDRF_NOTIFYITEMDRAW;
    }
    
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
    {
        
// This is the notification message for an item. We'll request
        
// notifications before each subitem's prepaint stage.
        
        
*pResult = CDRF_NOTIFYSUBITEMDRAW;
    }
    
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
    {

        COLORREF clrNewTextColor, clrNewBkColor;
        
        
int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
        CString strTemp 
= m_ctrlListCtrlData.GetItemText(nItem,2/*pLVCD->iSubItem*/);
        
        
if("ÔËÐÐ"==strTemp)
        {    
            clrNewTextColor 
= RGB(0,255,0); //Set the text to red
            clrNewBkColor = RGB(0,0,0); //Set the bkgrnd color to blue
        }
        
else
        {    
            clrNewTextColor 
= RGB(255,0,0); //Leave the text black
            clrNewBkColor = RGB(0,0,0); //leave the bkgrnd color white
        }
        pLVCD
->clrText = clrNewTextColor;
        pLVCD
->clrTextBk = clrNewBkColor;
        
        
//m_ctrlListCtrlData.Invalidate();

        
// Tell Windows to paint the control itself.
        *pResult = CDRF_DODEFAULT;

    }
    m_ctrlListCtrlData.UpdateWindow();
}
原创粉丝点击