现在的位置: 首页 > 综合 > 正文

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

2013年10月04日 ⁄ 综合 ⁄ 共 1121字 ⁄ 字号 评论关闭

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;
 }
}

抱歉!评论已关闭.