跪求各位兄弟来帮手啦.关于ListBox的颜色问题.

KevinCloud 2003-08-21 10:03:16
如何使往一个ListBox里加入String的时候,每条String用不同的颜色显示捏?

俺研究不出来,老板又赶着我要...

先拜一个.
...全文
267 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
KevinCloud 2003-08-22
  • 打赏
  • 举报
回复
结帖
mfc168 2003-08-21
  • 打赏
  • 举报
回复
这里有你问题的答案:
http://www.vckbase.com/code/listcode.asp?mclsid=3&sclsid=&page=2
带文字与颜色编辑功能的列表控件
wj59 2003-08-21
  • 打赏
  • 举报
回复
重载,OK,UP!
heweixing_77 2003-08-21
  • 打赏
  • 举报
回复
网上有很多例子,你用Google随便搜索一下吧。
一般地说是要重载CListBox,而且要自己写DrawItem(..)函数。

你也可以在对话框的OnCtlColor函数里面处理ListBox,这样可能会有些麻烦,我只是让ListBox显示背景图(和对话框一致)。
if((pWnd->GetDlgCtrlID()==IDC_LIST1)){
pDC->SelectObject(&m_FontList);//用自己自定义的字体
pDC->SetBkMode(TRANSPARENT);
if(!m_bListFirst)
{
m_listMemDC.CreateCompatibleDC(pDC);
m_listMemDC.SelectObject(&m_bmLOG);
m_bListFirst=TRUE;
}
pDC->BitBlt(0, 0, m_rectLOG.Width(), m_rectLOG.Height(),
&m_listMemDC, 0, 0, SRCCOPY); //显示背景图
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
你可以在显示背景图的地方修改,填充一个矩形就行了,但是要根据行高来填充不同的颜色。我认为可以这样做,没试过,不妨试试。
我还是认为,自己重载好一些。
KevinCloud 2003-08-21
  • 打赏
  • 举报
回复
解决问题了....好复杂...贴出来给大家看看了 是在一个论坛上看到的

下面提供了一个能显示带颜色文字的列表框类,类名是CColorListBox,它的基类是MFC类中的CListBox,它和类CListBox类不一样,它允许能

在列表框中显示有颜色的文字,在该类中的addstring()和insertstring()就干这个的,比如你可以用下面的方法:

m_clistbox.addstring("hey it's red!",RGB(255,0,0));
m_clistbox.insertstring(index,"hi i'm green",RGB(0,255,0));

它的使用是很简单的,你只需要用资源编辑器就够了。

首先,在对话框中添加一个列表框,你得把对话框的属性该一下ower draw:选fixed。当然你的列表框中还应该有字符串(要不然怎么有带

颜色的文字呢?)

然后,还要把有关于这个类的头文件放到你的工程中去。还有下面的语句也是要注意的,

下面是一部分代码:

#include "colorlistbox.h" // better be in stdafx.h
#include "mydialog.h"

......

bool cmydialog::oninitdialog()
{
cdialog::oninitdialog();

// sublclass listbox so that our ccolorlistbox receives windows events
m_clistbox.subclassdlgitem(idc_clistbox, this); // idc_clistbox is the listbox's resource id

.....
}
下面是头文件和那个CPP文件,全部代码如下:

//以下是头文件
#if !defined(AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCLUDED_)
#define AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

//*************************************************************
// ColorListBox.h : header file
//
// MFC ListBox with optional color
//
// Version: 1.0 01/10/1998 (c)Patrice Godard
//
//**************************************************************

/////////////////////////////////////////////////////////////////////////////
// CColorListBox window

class CColorListBox : public CListBox
{
// Construction
public:
CColorListBox();

// Attributes
public:

// Operations
public:
int AddString( LPCTSTR lpszItem);
int AddString( LPCTSTR lpszItem, COLORREF rgb);
int InsertString( int nIndex, LPCTSTR lpszItem, COLORREF rgb);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CColorListBox)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CColorListBox();

// Generated message map functions
protected:
//{{AFX_MSG(CColorListBox)
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_COLORLISTBOX_H__5529A6B1_584A_11D2_A41A_006097BD277B__INCLUDED_)

//以下是实现文件
//*************************************************************
// ColorListBox.cpp : implementation file
//
// MFC ListBox with optional color
//
// Version: 1.0 01/10/1998 (c)Patrice Godard
//
//**************************************************************

#include "ColorListBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CColorListBox

CColorListBox::CColorListBox()
{
}

CColorListBox::~CColorListBox()
{
}


BEGIN_MESSAGE_MAP(CColorListBox, CListBox)
//{{AFX_MSG_MAP(CColorListBox)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorListBox message handlers



void CColorListBox::DrawItem(LPDRAWITEMSTRUCT lpdis)
{
if (lpdis->itemID < 0)
return;

COLORREF cvText;
COLORREF cvBack;
CString itemString;

if ((lpdis->itemState & ODS_SELECTED) && // if item has been selected
(lpdis->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
DrawFocusRect(lpdis->hDC, &lpdis->rcItem);


if (!(lpdis->itemState & ODS_SELECTED) && // if item has been deselected
(lpdis->itemAction & ODA_SELECT))
DrawFocusRect(lpdis->hDC, &lpdis->rcItem);


if(lpdis->itemData) // if color information is present
cvText = SetTextColor(lpdis->hDC, lpdis->itemData);
else // if no color information, use default system colors
cvText = SetTextColor(lpdis->hDC, GetSysColor((lpdis->itemState & ODS_SELECTED)
? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT));

// always use system colors for background
cvBack = SetBkColor(lpdis->hDC, GetSysColor((lpdis->itemState & ODS_SELECTED)
? COLOR_HIGHLIGHT : COLOR_WINDOW));

// get and display item text
GetText(lpdis->itemID, itemString );
DrawText(lpdis->hDC, itemString, -1, &lpdis->rcItem, DT_LEFT | DT_SINGLELINE);

// restore DC colors
SetTextColor(lpdis->hDC, cvText);
SetBkColor(lpdis->hDC, cvBack);
}


//***********************************************
// original AddString() method
//
// purpose: Add a string to the listbox
//
// parameters:
// lpszItem: pointer to item text
//
// remarks:
// provided because CListBox::AddString is
// NOT virtual
//
// return: item index
//***********************************************
int CColorListBox::AddString( LPCTSTR lpszItem)
{
return ((CListBox*)this)->AddString(lpszItem);
}


//***********************************************
// new AddString() method
//
// purpose: Add a string to the listbox
//
// parameters:
// lpszItem: pointer to item text
// rgb: text color as a COLORREF
//
// return: item index
//***********************************************
int CColorListBox::AddString( LPCTSTR lpszItem,COLORREF rgb )
{
int item = AddString(lpszItem);
if(item >=0)
SetItemData(item,rgb);
return item;
}


//***********************************************
// new InsertString() method
//
// purpose: Insert a string to the listbox
//
// parameters:
// nIndex: index of inserted item
// lpszItem: pointer to item text
// rgb: text color as a COLORREF
//
// return: item index
//***********************************************
int CColorListBox::InsertString( int nIndex, LPCTSTR lpszItem, COLORREF rgb)
{
int item = ((CListBox*)this)->InsertString(nIndex,lpszItem);
if(item >=0)
SetItemData(item,rgb);
return item;

}

译完于2001年9月12号。我希望这能起到抛砖引玉的作用,有技术的读者能把它给扩展了。
KevinCloud 2003-08-21
  • 打赏
  • 举报
回复
何兄你的方法.....改起来恐怖呀......

3楼的兄弟.我要的是listbox,不是listctrl啊...

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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