这里面有两种办法:
第一种是直接生成CListCtrl的派生类
第二种是通过ON_NOTIFY
这里介绍第二种
在.h文件列面写入如下文件
afx_msg void OnCustomdrawMyList(NMHDR* pNMHDR,LRESULT* pResult);
在.cpp文件中写入
ON_NOTIFY(NM_CUSTOMDRAW,IDC_LIST1,OnCustomdrawMyList)
其中的IDC_LIST1是对应CListCtrl的ID
然后以下是代码
void CSearch::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult )
{
//This code based on Michael Dunn's excellent article on
//list control custom draw at
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast( 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 )
{
// 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( pLVCD->nmcd.dwItemSpec );
if(pLVCD->iSubItem==8)
{
CString strTemp = m_list.GetItemText(nItem,pLVCD->iSubItem);
if(strTemp == "共用数据")
{
clrNewTextColor = RGB(0,255,255);
}
else if(strTemp == "用户资料")
{
clrNewTextColor = RGB(255,215,0); //Set the text to red
//clrNewBkColor = RGB(255,255,0); //Set the bkgrnd color to blue
}
else if(strTemp=="申请试阅" || strTemp=="申请试刊")
{
clrNewTextColor=RGB(0,255,0);
}
else if(strTemp=="试阅会员" || strTemp=="试刊会员")
{
clrNewTextColor=RGB(0,0,255);
}
else if(strTemp=="过期会员")
{
clrNewTextColor=RGB(0,0,0);
}
else
{
clrNewTextColor = RGB(255,0,0); //Leave the text black
//clrNewBkColor = RGB(255,255,255); //leave the bkgrnd color white
}
pLVCD->clrText = clrNewTextColor;
//pLVCD->clrTextBk = clrNewBkColor;
}
else if(pLVCD->iSubItem==9)
{
CString strTemp1 = m_list.GetItemText(nItem,pLVCD->iSubItem);
if(StringToTime(strTemp1,1)>=now)
pLVCD->clrText=RGB(0,0,0);
else
pLVCD->clrText=RGB(255,0,0);
}
else if(pLVCD->iSubItem==10)
{
CString strTemp1 = m_list.GetItemText(nItem,pLVCD->iSubItem);
if(StringToTime(strTemp1,1)>=now)
pLVCD->clrText=RGB(0,0,0);
else
pLVCD->clrText=RGB(255,0,0);
}
else if(pLVCD->iSubItem==11)
{
pLVCD->clrText=RGB(0,0,0);
CDC* pDC = CDC::FromHandle(pLVCD->nmcd.hdc);
CRect rect;
m_list.GetSubItemRect(nItem, 1, LVIR_BOUNDS, rect);
pDC->FillSolidRect(&rect, ::GetSysColor(COLOR_WINDOW));
pDC->Rectangle(rect);
if(m_pay[nItem]==1)
{
CBrush brush(RGB(0,255,0));
pDC->FillRect(&rect, &brush);
}
else
{
CBrush brush(RGB(255,0,0));
pDC->FillRect(&rect, &brush);
}
/*
m_list.GetSubItemRect(nItem, 5, LVIR_BOUNDS, rect);
pDC->FillSolidRect(&rect, ::GetSysColor(COLOR_WINDOW));
if(m_list.GetItemState(nItem, LVIS_SELECTED))
{
DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL|CBS_DROPDOWNLIST | CBS_DISABLENOSCROLL;
//CComboBox *pList = new CComboBox();
//pList->Create(dwStyle, rect, WndList, IDC_COMBOBOXINLISTVIEW);
}
*/
}
// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;
}
}
阅读(2462) | 评论(2) | 转发(0) |