您的位置:首页 > 其它

Windows 界面:OnCustomDraw 设置 CListCtrol 任意行的文本字体颜色以及背景色

2008-01-30 17:57 627 查看
1,首先使用 ClassWizard 建立一个 CListCtrl 的派生类,在它的头文件消息响应函数中添加:

// Generated message map functions
protected:
//{{AFX_MSG(CScanFileList) //}}AFX_MSG
afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);

DECLARE_MESSAGE_MAP()

2,相应的,类实现文件添加:

BEGIN_MESSAGE_MAP(CScanFileList, CListCtrlEx)
//{{AFX_MSG_MAP(CScanFileList)
...
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
...
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

3,函数实现:

void CScanFileList::OnCustomDraw ( 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;

// First thing - check the draw stage. If it's the control's prepaint
// stage, then tell Windows we want messages for every item.

if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{

// 这里仅仅比较文本,注意:index == pLVCD->nmcd.dwItemSpec
if (GetItemText(pLVCD->nmcd.dwItemSpec, 2) == m_lpszEncrypt)
{
pLVCD->clrText = RGB(255,0,0);
pLVCD->clrTextBk = RGB(255,0,0);
}

// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐