各位大侠,请问怎么在list control 中比如有四条记录,分别用不同的颜色显示,可以吗?

hongzhh 2001-11-20 02:28:45
...全文
103 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
prog_st 2001-11-20
  • 打赏
  • 举报
回复
可以给例程,Email
dongfa 2001-11-20
  • 打赏
  • 举报
回复
这是我的一个程序:
头文件:
class CGSListBox : public CListBox
{
// Construction
public:
CGSListBox();

// Attributes
public:

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CGSListBox)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
//}}AFX_VIRTUAL

// Implementation
public:
void DrawGSItem(int nIndex, CDC* pDC, CRect& rect, int nType = 0);
int AddGSData(GSObjData* pGSData);
virtual ~CGSListBox();

// Generated message map functions
protected:
//{{AFX_MSG(CGSListBox)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};

实现:
CGSListBox::CGSListBox()
{
}

CGSListBox::~CGSListBox()
{
}


BEGIN_MESSAGE_MAP(CGSListBox, CListBox)
//{{AFX_MSG_MAP(CGSListBox)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CGSListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect = lpDrawItemStruct->rcItem;
int nIndex = lpDrawItemStruct->itemID;
// Set the background mode to TRANSPARENT.
int nOldMode = pDC->SetBkMode(TRANSPARENT);
BOOL bSel = FALSE;
if ((lpDrawItemStruct->itemState & ODS_SELECTED))
// Item has been selected - hilite frame.
bSel = TRUE;
if (!(lpDrawItemStruct->itemState & ODS_SELECTED))
// Item has been unselected.
bSel = FALSE;
CBrush br(RGB(50, 50, 220));
pDC->FillRect(rect, &br);
DrawGSItem(nIndex, pDC, rect);
if((lpDrawItemStruct->itemState & ODS_SELECTED))
pDC->InvertRect(rect);
// if ((lpDrawItemStruct->itemState & ODS_FOCUS))
// pDC->DrawFocusRect(rect);
// Set the background to old mode.
pDC->SetBkMode(nOldMode);
}

void CGSListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = 44;
}

int CGSListBox::AddGSData(GSObjData *pGSObjData)
{
int nItem = AddString(_T("无"));
if(nItem != LB_ERR)
{
SetItemData(nItem, (DWORD)pGSObjData);
return nItem;
}
return 0;
}

void CGSListBox::DrawGSItem(int nIndex, CDC *pDC, CRect &rect, int nType)
{
if(nIndex < 0 || nIndex >= GetCount())
return ;
GSObjData* pGSObjData = (GSObjData*)GetItemData(nIndex);
ASSERT(pGSObjData);
int nLeft = 4;
int nLength = 46;

CString strIndex, strCode, strName, strStart, strEnd, strPer, strTotal;

strIndex.Format("%03d", nIndex + 1);
strCode = pGSObjData->m_strGSCode;
strName = pGSObjData->m_strGSName;
strStart = pGSObjData->m_strGSStartData;
strEnd = pGSObjData->m_strGSEndData;
strPer = pGSObjData->m_strGSPercentData;
strTotal = pGSObjData->m_strGSTotalData;

CFont newFont;
newFont.CreateFont(22, 0, 0, 0, 700 , 0, 0, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
PROOF_QUALITY, VARIABLE_PITCH | FF_DONTCARE, "Fixedsys");
CFont* oldFont = pDC->SelectObject(&newFont);

pDC->SetTextColor(RGB(255, 255, 255));
pDC->DrawText(strIndex,
CRect(rect.left + nLeft, rect.top, rect.left + nLength, rect.bottom),
DT_LEFT | DT_VCENTER | DT_SINGLELINE);
nLeft = 50;
nLength = 80;
pDC->SetTextColor(RGB(255, 255, 0));
pDC->DrawText(strCode,
CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom),
DT_CENTER | DT_VCENTER | DT_SINGLELINE);
nLeft = 130;
nLength = 90;
pDC->SetTextColor(RGB(255, 255, 180));
pDC->DrawText(strName,
CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom),
DT_CENTER | DT_VCENTER | DT_SINGLELINE);
nLeft = 220;
nLength = 80;
if(pGSObjData->m_bIsStop)
{
pDC->SetTextColor(RGB(255, 255, 255));
}
else
{
if(GSObjData::StringToDouble(strEnd) < GSObjData::StringToDouble(strStart))
pDC->SetTextColor(RGB(255, 0, 0));
else if(GSObjData::StringToDouble(strEnd) > GSObjData::StringToDouble(strStart))
pDC->SetTextColor(RGB(0, 255, 0));
else
pDC->SetTextColor(RGB(255, 255, 255));
}
pDC->DrawText(strEnd,
CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom),
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
nLeft = 300;
nLength = 80;
pDC->DrawText(strStart,
CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom),
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
nLeft = 380;
nLength = 80;
pDC->DrawText(strPer,
CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom),
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
nLeft = 460;
nLength = 120;
pDC->DrawText(strTotal,
CRect(rect.left + nLeft, rect.top, rect.left + nLeft + nLength, rect.bottom),
DT_RIGHT | DT_VCENTER | DT_SINGLELINE);
rect.bottom -= 2;
CPen newPen1(PS_SOLID, 1, RGB(128, 128, 128));
CPen newPen2(PS_SOLID, 1, RGB(192, 192, 192));
pDC->SelectObject(&newPen2);
pDC->MoveTo(rect.left, rect.bottom);
pDC->LineTo(rect.right, rect.bottom);
pDC->SelectObject(&newPen1);
rect.bottom += 1;
pDC->MoveTo(rect.left, rect.bottom);
pDC->LineTo(rect.right, rect.bottom);
}

CListCtrl要有owner draw风格:Variable可以
dana_wx 2001-11-20
  • 打赏
  • 举报
回复
怎么做呢 我也相知道
dana_wx 2001-11-20
  • 打赏
  • 举报
回复
我也相知道
dana_wx 2001-11-20
  • 打赏
  • 举报
回复
用什么啊 ?
hongzhh 2001-11-20
  • 打赏
  • 举报
回复
help
hongzhh 2001-11-20
  • 打赏
  • 举报
回复
想知道怎么做,有人愿意赐教
吗?
XIAYYS 2001-11-20
  • 打赏
  • 举报
回复
听课
tar 2001-11-20
  • 打赏
  • 举报
回复
是mm啊!!
hongzhh 2001-11-20
  • 打赏
  • 举报
回复
小妹初学vc++
不知具体如何实现?
smallfool 2001-11-20
  • 打赏
  • 举报
回复
对于LVS_REPORT风格的列表框,重载DrawItem显然很容易解决.
Hover 2001-11-20
  • 打赏
  • 举报
回复
用不同的图标显示吧

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧