如题!急等!
我要求改变其中某一行的背景颜色,也就是可能每一行的背景颜色都不同。
要改变的行不一定有数据。即空行也要能改变。

解决方案 »

  1.   

    http://www.vckbase.com/document/viewdoc/?id=1480
    http://www.vckbase.com/document/viewdoc/?id=891
      

  2.   

    http://www.vckbase.com/code/listcode.asp?mclsid=3&sclsid=323
      

  3.   

    首先感谢“秋天的叶子”我刚才在那试了几个Demo。
    发现都不能改变没有填充数据的行的背景颜色!
    我把行号设成没有数据的行,都会报错!即我需要一上来还没有数据的时候就能改变某一行的背景颜色。
      

  4.   

    继承CCtrlList,再重载OnPaint(),什么都自己绘,就OK了
      

  5.   

    问题是不会画啊!
    我是在OnNMCustomdraw里画的,但就是画不了没有数据的行啊!
    救命啊!
      

  6.   

    参考一下吧
    //MyListCtrl.h
    #pragma once
    #include "ListCtrlWithCustomDraw.h"class CMyListCtrl : public CListCtrlWithCustomDraw
    {
    // Construction
    public:
    CMyListCtrl();// Attributes
    public:
    struct ItemColor
    {
    int item;
    COLORREF color;
    ItemColor(){};
    ItemColor(int row, COLORREF rowcolor){item = row; color = rowcolor;}
    };
    typedef CArray<ItemColor, ItemColor&> ItemColorArray;
    ItemColorArray m_ICArray;// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyListCtrl)
    //}}AFX_VIRTUAL
    virtual bool IsNotifyItemDraw();
    virtual bool IsNotifySubItemDraw(int nItem, UINT nState, LPARAM lParam);
    virtual COLORREF TextColorForSubItem(int nItem, int nSubItem, UINT nState, LPARAM lParam);// Implementation
    public:
    void ResetAllItemColor();
    void ChangeItemColor(int row, COLORREF color=0);
    void AddItem(int row, const CStringArray& textArray, COLORREF color=0);
    BOOL DeleteItem(int nItem);
    virtual ~CMyListCtrl(); // Generated message map functions
    protected:
    //{{AFX_MSG(CMyListCtrl)
    // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };
      

  7.   

    //ListCtrlWithCustomDraw.h
    #pragma once
    class CListCtrlWithCustomDraw : public CListCtrl
    {
    // Construction
    public:
    CListCtrlWithCustomDraw();

    // Attributes
    public:

    // Operations
    public:

    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CListCtrlWithCustomDraw)
    //}}AFX_VIRTUAL

    // Implementation
    public:
    virtual ~CListCtrlWithCustomDraw();protected:
    CFont* m_pOldItemFont;
    CFont* m_pOldSubItemFont; //
    // Callbacks for whole control
    //

    // do we want to do the drawing ourselves?
    virtual bool IsDraw() { return false; }
    // if we are doing the drawing ourselves
    // override and put the code in here
    // and return TRUE if we did indeed do
    // all the drawing ourselves
    virtual bool OnDraw(CDC* /*pDC*/) { return false; }
    // do we want to handle custom draw for
    // individual items
    virtual bool IsNotifyItemDraw() { return false; }
    // do we want to be notified when the
    // painting has finished
    virtual bool IsNotifyPostPaint() { return false; }
    // do we want to do any drawing after
    // the list control is finished
    virtual bool IsPostDraw() { return false; }
    // if we are doing the drawing afterwards ourselves
    // override and put the code in here
    // the return value is not used here
    virtual bool OnPostDraw(CDC* /*pDC*/) { return false; }

    //
    // Callbacks for each item
    //

    // return a pointer to the font to use for this item.
    // return NULL to use default
    virtual CFont* FontForItem(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return NULL; }
    // return the text color to use for this item
    // return CLR_DEFAULT to use default
    virtual COLORREF TextColorForItem(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; }
    // return the background color to use for this item
    // return CLR_DEFAULT to use default
    virtual COLORREF BkColorForItem(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; }
    // do we want to do the drawing for this item ourselves?
    virtual bool IsItemDraw(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
    // if we are doing the drawing ourselves
    // override and put the code in here
    // and return TRUE if we did indeed do
    // all the drawing ourselves
    virtual bool OnItemDraw(CDC* /*pDC*/, int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
    // do we want to handle custom draw for
    // individual sub items
    virtual bool IsNotifySubItemDraw(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
    // do we want to be notified when the
    // painting has finished
    virtual bool IsNotifyItemPostPaint(int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
    // do we want to do any drawing after
    // the list control is finished
    virtual bool IsItemPostDraw() { return false; }
    // if we are doing the drawing afterwards ourselves
    // override and put the code in here
    // the return value is not used here
    virtual bool OnItemPostDraw(CDC* /*pDC*/, int /*nItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; } //
    // Callbacks for each sub item
    //

    // return a pointer to the font to use for this sub item.
    // return NULL to use default
    virtual CFont* FontForSubItem(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return NULL; }
    // return the text color to use for this sub item
    // return CLR_DEFAULT to use default
    virtual COLORREF TextColorForSubItem(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; }
    // return the background color to use for this sub item
    // return CLR_DEFAULT to use default
    virtual COLORREF BkColorForSubItem(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return CLR_DEFAULT; }
    // do we want to do the drawing for this sub item ourselves?
    virtual bool IsSubItemDraw(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
    // if we are doing the drawing ourselves
    // override and put the code in here
    // and return TRUE if we did indeed do
    // all the drawing ourselves
    virtual bool OnSubItemDraw(CDC* /*pDC*/, int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
    // do we want to be notified when the
    // painting has finished
    virtual bool IsNotifySubItemPostPaint(int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }
    // do we want to do any drawing after
    // the list control is finished
    virtual bool IsSubItemPostDraw() { return false; }
    // if we are doing the drawing afterwards ourselves
    // override and put the code in here
    // the return value is not used here
    virtual bool OnSubItemPostDraw(CDC* /*pDC*/, int /*nItem*/, int /*nSubItem*/, UINT /*nState*/, LPARAM /*lParam*/) { return false; }

    // Generated message map functions
    protected:
    //{{AFX_MSG(CListCtrlWithCustomDraw)
    afx_msg void OnCustomdraw(NMHDR* pNMHDR, LRESULT* pResult);
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()
    };
      

  8.   

    自己到网上找一下,参考一下别人是怎么写的。改造一下就行了。就是Owner-Draw技术。对于你的问题就是提供DrawItem方法。其实没有上面那么复杂,你就不用看上面的了。一般是十分钟就可以搞定的事。记住在创建自定义Owner-Draw控件时,设定控件style为Owner-Draw。你也可以到MSDN上查一下DrawItem,应该会教你如何Owner-Draw的。
      

  9.   

    到我的网站上找吧
    http://www.iuishop.com,在“文档”中有个“自绘ListCtrl”里面的源代码。
      

  10.   

    谢谢各位。
    OwnerDraw实现起来好像很麻烦。慢慢学!我现在是把背景形成一幅背景图片实现的。