如何让ListBox每一行显示不同的背景颜色啊?

mikeyangoo 2003-10-15 02:56:12
如何让ListBox每一行显示不同的背景颜色啊?
我搜索过过去的信息,说的都是ListBox整个背景颜色!
那要是一行呢?
...全文
1259 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
mikeyangoo 2003-10-17
  • 打赏
  • 举报
回复
thank jiangsheng(蒋晟.Net)
thank everybody!
thank you very much!
add mark afternoon!
xharry 2003-10-16
  • 打赏
  • 举报
回复
http://www.codeguru.com/listbox/colorlb.shtml
bcpl 2003-10-16
  • 打赏
  • 举报
回复
http://www.codeproject.com/combobox/xlistbox.asp?target=XListBox
mikeyangoo 2003-10-16
  • 打赏
  • 举报
回复
KevinCloud(零度火焰)
不好意思,问一下,我程序试过了,为什么还是没有变色!
我发现程序运行时没有跳到过DrawItem()中?
是不是要用什么消息吗?还是要在外部调用!
真的不好意思,我是初学者!谢谢你的代码!
麻烦你解释一下了!
yintongshun 2003-10-16
  • 打赏
  • 举报
回复
http://www.vckbase.com/code/listcode.asp?mclsid=3&sclsid=323
蒋晟 2003-10-16
  • 打赏
  • 举报
回复
add LBS_OWNERDRAWVARIABLE style

sorry I have not read your problemcarefully

Command what is yours
Conquer what is not
蒋晟 2003-10-16
  • 打赏
  • 举报
回复
Why not customdraw?
Customizing a Control's Appearance Using Custom Draw
Using Custom Draw
The following code fragment is a portion of a WM_NOTIFY handler that illustrates how to handle custom draw notifications sent to a list-view control:

Hide Example


LPNMLISTVIEW pnm = (LPNMLISTVIEW)lParam;

switch (pnm->hdr.code){
...
case NM_CUSTOMDRAW:

LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;

switch(lplvcd->nmcd.dwDrawStage) {

case CDDS_PREPAINT :
return CDRF_NOTIFYITEMDRAW;

case CDDS_ITEMPREPAINT:
SelectObject(lplvcd->nmcd.hdc,
GetFontForItem(lplvcd->nmcd.dwItemSpec,
lplvcd->nmcd.lItemlParam) );
lplvcd->clrText = GetColorForItem(lplvcd->nmcd.dwItemSpec,
lplvcd->nmcd.lItemlParam);
lplvcd->clrTextBk = GetBkColorForItem(lplvcd->nmcd.dwItemSpec,
lplvcd->nmcd.lItemlParam);

/* At this point, you can change the background colors for the item
and any subitems and return CDRF_NEWFONT. If the list-view control
is in report mode, you can simply return CDRF_NOTIFYSUBITEMREDRAW
to customize the item's subitems individually */
...

return CDRF_NEWFONT;
// or return CDRF_NOTIFYSUBITEMREDRAW;

case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
SelectObject(lplvcd->nmcd.hdc,
GetFontForSubItem(lplvcd->nmcd.dwItemSpec,
lplvcd->nmcd.lItemlParam,
lplvcd->iSubItem));
lplvcd->clrText = GetColorForSubItem(lplvcd->nmcd.dwItemSpec,
lplvcd->nmcd.lItemlParam,
lplvcd->iSubItem));
lplvcd->clrTextBk = GetBkColorForSubItem(lplvcd->nmcd.dwItemSpec,
lplvcd->nmcd.lItemlParam,
lplvcd->iSubItem));

/* This notification is received only if you are in report mode and
returned CDRF_NOTIFYSUBITEMREDRAW in the previous step. At
this point, you can change the background colors for the
subitem and return CDRF_NEWFONT.*/
...
return CDRF_NEWFONT;
}
...
}

The first NM_CUSTOMDRAW notification has the dwDrawStage member of the NMCUSTOMDRAW structure set to CDDS_PREPAINT. The handler returns CDRF_NOTIFYITEMDRAW to indicate that it wishes to modify one or more items individually.

If CDRF_NOTIFYITEMDRAW was returned in the previous step, the next NM_CUSTOMDRAW notification has dwDrawStage set to CDDS_ITEMPREPAINT. The handler retrieves the current color and font values. At this point, you can specify new values for small icon, large icon, and list modes. If the control is in report mode, you can also specify new values that will apply to all subitems of the item. If you have changed anything, return CDRF_NEWFONT. If the control is in report mode and you want to handle the subitems individually, return CDRF_NOTIFYSUBITEMREDRAW.

The final notification is only sent if the control is in report mode and you returned CDRF_NOTIFYSUBITEMREDRAW in the previous step. The procedure for changing fonts and colors is the same as that step, but it only applies to a single subitem. Return CDRF_NEWFONT to notify the control if the color or font was changed.

Related Topics

Custom Draw Reference
SAMPLE: CustDTv Illustrates Custom Draw in a TreeView (Q248496)

Command what is yours
Conquer what is not
mikeyangoo 2003-10-16
  • 打赏
  • 举报
回复
为什么Demo能显示!我的不能呢?
我新建工程,加入相应文件,随后调用AddLine!
Demo能响应到DrawItem,而我的不能?
why?
要做什么设置吗?
mikeyangoo 2003-10-16
  • 打赏
  • 举报
回复
bcpl(我是宇宙中的一粒沙子)
我用那个类为什么还是不能显示啊?
我发现不能跳到DrawItem().
天啊?为什么啊?
mikeyangoo 2003-10-16
  • 打赏
  • 举报
回复
问一下,是如何调用到DrawItem()?
yushulei 2003-10-16
  • 打赏
  • 举报
回复
做过listctrl,只要响应custondraw自己画就行了。这个还没用过。
smallcrocodile 2003-10-15
  • 打赏
  • 举报
回复
找个第三方的控件
KevinCloud 2003-10-15
  • 打赏
  • 举报
回复
头文件
#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 "stdafx.h"
#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;

}
都给到这个份上啦.
xharry 2003-10-15
  • 打赏
  • 举报
回复
可是msdn说Custom Draw支持:
Header controls
List view controls
Rebar controls
Toolbar controls
Tooltip controls
Trackbar controls
Tree view controls

没有说支持List Box啊,难道List Box 属于List View麽
mahatma_cn 2003-10-15
  • 打赏
  • 举报
回复
当然要是找内裤也可以,但是应该自己明白怎么做才行,否则用C#,他直接支持不同颜色。
feng1976 2003-10-15
  • 打赏
  • 举报
回复
最省事的就是找个类库。调调函数就解决了,呵呵
feng1976 2003-10-15
  • 打赏
  • 举报
回复
大致上如此,有些具体问题遇到时才会知道是问题,不日如何设置自绘风格了,如何取出Listbox的字符串了等等
mahatma_cn 2003-10-15
  • 打赏
  • 举报
回复
具体可以这样,从该类派生一个新类,并添加消息反射,NM_CUSTOMDRAW通知消息
在函数中处理传输过来的结构,如:NMCUSTOMDRAW和包含该结构的NM??????CUSTOMDRAW结构。
可以根据传递过来的NMCUSTOMDRAW结构的dwDrawStage确定是整个控件还是每个项目或者子项目。从而实现自定义控件外观。
以上一些词汇可能有误,因为没有msdn在手边。
xharry 2003-10-15
  • 打赏
  • 举报
回复
关注一下!!!!
我知道怎么改变List Control的每行的颜色,我以为用在List Box上也可以呢,
试了一下发现不行,我估计映射WM_DRAWITEM可以,当我不知道,怎么把List Box
的ID传给OnDrawItem():(
wuxfBrave 2003-10-15
  • 打赏
  • 举报
回复
http://www.vckbase.com/code/listcode.asp?mclsid=3&sclsid=323
看看这里的例子,希望对你有帮助

15,979

社区成员

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

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