您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> VC/MFC >> 如何让ListBox每一行显示不同的背景颜色啊?

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

来源:网络整理     时间:2016/7/17 18:51:39     关键词:

关于网友提出的“ 如何让ListBox每一行显示不同的背景颜色啊?”问题疑问,本网通过在网上对“ 如何让ListBox每一行显示不同的背景颜色啊?”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 如何让ListBox每一行显示不同的背景颜色啊?
描述:

如何让ListBox每一行显示不同的背景颜色啊?
我搜索过过去的信息,说的都是ListBox整个背景颜色!
那要是一行呢?


解决方案1:

http://www.codeguru.com/listbox/colorlb.shtml

解决方案2:

头文件
#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;
}
都给到这个份上啦.

解决方案3:

大致上如此,有些具体问题遇到时才会知道是问题,不日如何设置自绘风格了,如何取出Listbox的字符串了等等

解决方案4:

add LBS_OWNERDRAWVARIABLE style
sorry I have not read your problemcarefully
Command what is yours
Conquer what is not

解决方案5:

http://www.codeproject.com/combobox/xlistbox.asp?target=XListBox

解决方案6:

具体可以这样,从该类派生一个新类,并添加消息反射,NM_CUSTOMDRAW通知消息
在函数中处理传输过来的结构,如:NMCUSTOMDRAW和包含该结构的NM??????CUSTOMDRAW结构。
可以根据传递过来的NMCUSTOMDRAW结构的dwDrawStage确定是整个控件还是每个项目或者子项目。从而实现自定义控件外观。
以上一些词汇可能有误,因为没有msdn在手边。


以上介绍了“ 如何让ListBox每一行显示不同的背景颜色啊?”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2752655.html

相关图片

相关文章