重载NM_CUSTOMDRAW消息 NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR); //set default draw *pResult = CDRF_DODEFAULT; if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT) { *pResult = CDRF_NOTIFYITEMDRAW; } else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT) { // This is the notification message for an item. We'll request // notifications before each subitem's prepaint stage. *pResult = CDRF_NOTIFYSUBITEMDRAW; } else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM)) {//paint the list control items and subitems //get the number of item is going to paint int nitem = static_cast<int> (pLVCD->nmcd.dwItemSpec); //get the number of subitem is going to paint int nsubitem = pLVCD->iSubItem; //get first selected item position POSITION npos = GetFirstSelectedItemPosition(); if(nitem == GetNextSelectedItem(npos)) //如果被选中使用自绘输出 { //所画项是选中项 //get point of default dc objet CDC* pdc = CDC::FromHandle(pLVCD->nmcd.hdc); CString str; CRect rect; UINT nFormat = DT_VCENTER | DT_SINGLELINE; //get rect of the sub item which is going to paint GetSubItemRect(nitem, nsubitem, LVIR_BOUNDS, rect); rect.left += 3; //调整自绘输出位置 //get the context is going to paint on the subitem str = GetItemText(nitem,nsubitem); pDC->SetTextColor(RGB(0,0,0));//字体颜色,因该是白色 pDC->FillSolidRect(&rect, RGB(255,255,255));//背景,深蓝色吧,试试 pDC->SetBkColor(RGB(255,255,255));//字体背景色,颜色同上 //set back is transparent CSize msize; //get the context length which is going to paint msize = pdc->GetTextExtent(str); //paint the text pdc->DrawText(str, &rect, nFormat); //自绘输出 *pResult = CDRF_SKIPDEFAULT; // We've painted everything. } else *pResult = CDRF_DODEFAULT; } }
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
//set default draw
*pResult = CDRF_DODEFAULT;
if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
{
// This is the notification message for an item. We'll request
// notifications before each subitem's prepaint stage. *pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))
{//paint the list control items and subitems
//get the number of item is going to paint
int nitem = static_cast<int> (pLVCD->nmcd.dwItemSpec);
//get the number of subitem is going to paint
int nsubitem = pLVCD->iSubItem;
//get first selected item position
POSITION npos = GetFirstSelectedItemPosition();
if(nitem == GetNextSelectedItem(npos)) //如果被选中使用自绘输出
{ //所画项是选中项
//get point of default dc objet
CDC* pdc = CDC::FromHandle(pLVCD->nmcd.hdc);
CString str;
CRect rect;
UINT nFormat = DT_VCENTER | DT_SINGLELINE;
//get rect of the sub item which is going to paint
GetSubItemRect(nitem, nsubitem, LVIR_BOUNDS, rect);
rect.left += 3; //调整自绘输出位置
//get the context is going to paint on the subitem
str = GetItemText(nitem,nsubitem);
pDC->SetTextColor(RGB(0,0,0));//字体颜色,因该是白色
pDC->FillSolidRect(&rect, RGB(255,255,255));//背景,深蓝色吧,试试
pDC->SetBkColor(RGB(255,255,255));//字体背景色,颜色同上
//set back is transparent
CSize msize;
//get the context length which is going to paint
msize = pdc->GetTextExtent(str);
//paint the text
pdc->DrawText(str, &rect, nFormat); //自绘输出
*pResult = CDRF_SKIPDEFAULT; // We've painted everything.
}
else
*pResult = CDRF_DODEFAULT;
}
}
希望对你有所帮助
如果显示文本还需要用SetTextBkColor()来设置文本的背景色
好像是这样,不记的了,回云我帮你查查
功能说明:能将行给予不同的颜色和背景
#include "AfxTempl.h"
/////////////////////////////////////////////////////////////////////////////
// CExListCtrl windowclass CExListCtrl : public CListCtrl
{
// Construction
public:
CExListCtrl();// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CExListCtrl)
//}}AFX_VIRTUAL
/////////////////////////////////////////////////////////////////
public:
//operation
void SetSelText(COLORREF crSelText);
void SetSelBk(COLORREF crSelBk);
void SetSelFontSize(int nFontSize);
//attribute
typedef CList<int,int>CItemsList;
CItemsList m_selItems;
protected:
COLORREF m_crSelText; //选中的字体颜色
COLORREF m_crSelBk; //选中的背景
int m_nFontSize; //字体大小
CFont m_selFont; //选中的字体
/////////////////////////////////////////////////////////////////// Implementation
public:
virtual ~CExListCtrl(); // Generated message map functions
protected:
//{{AFX_MSG(CExListCtrl)
afx_msg void OnDestroy();
//}}AFX_MSG
afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult); DECLARE_MESSAGE_MAP()
};
CExListCtrl::CExListCtrl()
{
POSITION pos=m_selItems.GetHeadPosition();
while(pos != NULL)
{
m_selItems.GetNext(pos);
m_selItems.RemoveAt(pos);
} m_crSelText=RGB(0,0,0);
m_crSelBk=RGB(255,255,255);
m_nFontSize=15; //创建字体
LOGFONT lf;
memset(&lf,0,sizeof(LOGFONT));
lf.lfHeight=m_nFontSize;
_tcscpy(lf.lfFaceName,_T("宋体"));
lf.lfWeight=400;
//lf.lfItalic=TRUE;
m_selFont.CreateFontIndirect(&lf);
}CExListCtrl::~CExListCtrl()
{
m_selFont.DeleteObject();}
BEGIN_MESSAGE_MAP(CExListCtrl, CListCtrl)
//{{AFX_MSG_MAP(CExListCtrl)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CExListCtrl message handlers
/////////////////////////////////////////////////////////////////////
//主要修改内容:加入链表 给链表中的Item显示指定的颜色
void CExListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
//默认处理
*pResult = CDRF_DODEFAULT;
//检查Paint的阶段如果要处理哪一段给*pResult赋予相应的值
if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
{
if(m_selItems.Find(pLVCD->nmcd.dwItemSpec) != NULL)
{
::SelectObject(pLVCD->nmcd.hdc,m_selFont.GetSafeHandle());
if(pLVCD->nmcd.uItemState & CDIS_SELECTED )
{
pLVCD->clrText=m_crSelText;
pLVCD->clrTextBk=m_crSelBk;
}
else
{
pLVCD->clrText=m_crSelText;
pLVCD->clrTextBk=m_crSelBk;
}
*pResult = CDRF_NEWFONT;
}
else
*pResult = CDRF_DODEFAULT;
}
}void CExListCtrl::OnDestroy()
{
CListCtrl::OnDestroy();
}void CExListCtrl::SetSelText(COLORREF crSelText)
{
m_crSelText=crSelText;
}void CExListCtrl::SetSelBk(COLORREF crSelBk)
{
m_crSelBk=crSelBk;
}void CExListCtrl::SetSelFontSize(int nFontSize)
{
m_nFontSize=nFontSize;
m_selFont.DeleteObject(); //创建字体
LOGFONT lf;
memset(&lf,0,sizeof(LOGFONT));
lf.lfHeight=m_nFontSize;
_tcscpy(lf.lfFaceName,_T("宋体"));
lf.lfWeight=400;
//lf.lfItalic=TRUE;
m_selFont.CreateFontIndirect(&lf);
}