CtrlList每行不同颜色

本文介绍如何使用 C++ 在 ListCtrl 控件中实现不同行显示不同颜色的效果,通过重写 OnCustomdrawListCtrl 函数并设置不同的背景和文字颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ExpandedBlockStart.gif代码
//消息映射中添加
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();
}

 

转载于:https://www.cnblogs.com/duzouzhe/archive/2010/01/05/1639880.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值