怎么在list control 中奇偶 行的颜色不样
展开全部
重写个CListCtrl类,比如CMyListCtrl
头文件
#pragma once
class CMyListCtrl : public CListCtrl
{
public:
CMyListCtrl();
virtual ~CMyListCtrl();
private:
CMap<DWORD,DWORD&,COLORREF,COLORREF&> m_mapItemColor;
public:
void SetItemColor(DWORD dwItemIndex, COLORREF color);
private:
afx_msg void OnNMCustomDraw(NMHDR *pNMHDR, LRESULT *pResult);
protected:
DECLARE_MESSAGE_MAP()
};
CPP
#include "stdafx.h"
#include "MyListCtrl.h"
CMyListCtrl::CMyListCtrl()
{
}
CMyListCtrl::~CMyListCtrl()
{
}
BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomDraw)
END_MESSAGE_MAP()
void CMyListCtrl::OnNMCustomDraw(NMHDR *pNMHDR, LRESULT *pResult)
{
*pResult = CDRF_DODEFAULT;
NMLVCUSTOMDRAW* lplvdr = (NMLVCUSTOMDRAW*)pNMHDR;
NMCUSTOMDRAW& nmcd = lplvdr->nmcd;
switch (lplvdr->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
*pResult = CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT:
{
COLORREF ItemColor;
if (m_mapItemColor.Lookup(nmcd.dwItemSpec, ItemColor) == TRUE)
{
lplvdr->clrTextBk = ItemColor;
*pResult = CDRF_DODEFAULT;
}
break;
}
}
}
void CMyListCtrl::SetItemColor(DWORD dwItemIndex, COLORREF color)
{
m_mapItemColor.SetAt(dwItemIndex, color);
this->RedrawItems(dwItemIndex, dwItemIndex);
UpdateWindow();
}
然后调用SetItemColor,随便设颜色吧
头文件
#pragma once
class CMyListCtrl : public CListCtrl
{
public:
CMyListCtrl();
virtual ~CMyListCtrl();
private:
CMap<DWORD,DWORD&,COLORREF,COLORREF&> m_mapItemColor;
public:
void SetItemColor(DWORD dwItemIndex, COLORREF color);
private:
afx_msg void OnNMCustomDraw(NMHDR *pNMHDR, LRESULT *pResult);
protected:
DECLARE_MESSAGE_MAP()
};
CPP
#include "stdafx.h"
#include "MyListCtrl.h"
CMyListCtrl::CMyListCtrl()
{
}
CMyListCtrl::~CMyListCtrl()
{
}
BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomDraw)
END_MESSAGE_MAP()
void CMyListCtrl::OnNMCustomDraw(NMHDR *pNMHDR, LRESULT *pResult)
{
*pResult = CDRF_DODEFAULT;
NMLVCUSTOMDRAW* lplvdr = (NMLVCUSTOMDRAW*)pNMHDR;
NMCUSTOMDRAW& nmcd = lplvdr->nmcd;
switch (lplvdr->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
*pResult = CDRF_NOTIFYITEMDRAW;
break;
case CDDS_ITEMPREPAINT:
{
COLORREF ItemColor;
if (m_mapItemColor.Lookup(nmcd.dwItemSpec, ItemColor) == TRUE)
{
lplvdr->clrTextBk = ItemColor;
*pResult = CDRF_DODEFAULT;
}
break;
}
}
}
void CMyListCtrl::SetItemColor(DWORD dwItemIndex, COLORREF color)
{
m_mapItemColor.SetAt(dwItemIndex, color);
this->RedrawItems(dwItemIndex, dwItemIndex);
UpdateWindow();
}
然后调用SetItemColor,随便设颜色吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询