我想把列表的奇数行和偶数行区分开来,但是不知道怎么做
现在想的是奇数行和偶数行的背景颜色不一样,请问该怎么做啊SetBkColor和SetTextColor是对整个控件有效的吧
谢谢

解决方案 »

  1.   

    需要自己写控件~
    给你一个现成的例子http://www.vckbase.com/document/viewdoc/?id=891
    把源文件包含到工程里
    在定义变量的地方#include “sortlistctrl.h"
    给你的控件ID关联一个CListCtrl对象把ClistCtrl用CSortListCtrl替换后可设置单元格的颜色 进行每行的单元格颜色设置可以达到你要的效果
      

  2.   

    www.codeproject.com上面很多,可以search一下
      

  3.   

    #if !defined(AFX_COLORLISTCTRL_H__04C394FD_80E8_44D1_A263_F78C97A8D727__INCLUDED_)
    #define AFX_COLORLISTCTRL_H__04C394FD_80E8_44D1_A263_F78C97A8D727__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // ColorListCtrl.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CColorListCtrl window  
    #include   <afxtempl.h>   //   CColorListCtrl   class   CColorListCtrl   :   public   CListCtrl   
    {   
    DECLARE_DYNAMIC(CColorListCtrl)   

    public:   
    CColorListCtrl();   
    virtual   ~CColorListCtrl();   
        
    protected:   
    DECLARE_MESSAGE_MAP()   
    protected:   
    virtual   void   PreSubclassWindow();   
    public:   
    //     virtual   void   DrawItem(LPDRAWITEMSTRUCT   /*lpDrawItemStruct*/);   
    COLORREF   SetItemColor(int   nRow,   int   nCol,   COLORREF   clrItem);   
    COLORREF   SetItemColor(LVITEM*   plvItem   =   NULL,   COLORREF   clrItem   =   RGB(0,   0,   0));   
    private:   
    CList<COLORREF*,   COLORREF*>   m_lstItemColor;   
    public:   
    afx_msg   void   OnNMCustomdraw(NMHDR   *pNMHDR,   LRESULT   *pResult);   
    };   ///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_COLORLISTCTRL_H__04C394FD_80E8_44D1_A263_F78C97A8D727__INCLUDED_)// ColorListCtrl.cpp : implementation file
    //#include "stdafx.h"
    #include "ColorListCtrl.h"#include   ".\colorlistctrl.h" #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif//   CColorListCtrl   IMPLEMENT_DYNAMIC(CColorListCtrl,   CListCtrl)   
    CColorListCtrl::CColorListCtrl()   
    {   
    }   CColorListCtrl::~CColorListCtrl()   
    {   
    POSITION   pos   =   m_lstItemColor.GetHeadPosition();   
    while   (pos)   
    {   
    delete   []m_lstItemColor.GetNext(pos);   
    }   
    }   BEGIN_MESSAGE_MAP(CColorListCtrl,   CListCtrl)   
    ON_NOTIFY_REFLECT(NM_CUSTOMDRAW,   OnNMCustomdraw)   
    END_MESSAGE_MAP()   void   CColorListCtrl::PreSubclassWindow()   
    {   
    //   TODO:   在此添加专用代码和/或调用基类   
    ModifyStyle(0,   /*LVS_OWNERDRAWFIXED   |   */LVS_REPORT,   0);   
        
    CListCtrl::PreSubclassWindow();   
    }   COLORREF   CColorListCtrl::SetItemColor(int   nRow,   int   nCol,   COLORREF   clrItem)   
    {   
    int   nCols   =   GetHeaderCtrl()->GetItemCount();   
    int   nRows   =   GetItemCount();   
    if   (nCol   >=   nCols   ||   nRow   >=   nRows)   
    return   COLORREF(-1);   
        
    for   (int   i   =   m_lstItemColor.GetCount();   i   <   nRow   +   1;   i++)   
    {   
    COLORREF   *clrCol   =   new   COLORREF[nCols];   
    ZeroMemory(clrCol,   sizeof(clrCol));   
    m_lstItemColor.AddTail(clrCol);   
    }   
        
    for   ( i   =   0;   i   <   nRows;   i++)   
    {   
    if   (i   ==   nRow)   
    {   
    COLORREF   *clrCol   =   m_lstItemColor.GetAt(m_lstItemColor.FindIndex(nRow));   
    COLORREF   clrRet   =   clrCol[nCol];   
    clrCol[nCol]   =   clrItem;   
    return   clrRet;   
    }   
    }   
        
    return   COLORREF(-1);   
    }   
    COLORREF   CColorListCtrl::SetItemColor(LVITEM*   plvItem,   COLORREF   clrItem)   
    {   
    if   (!plvItem)   
    {   
    return   COLORREF(0);   
    }   
    int   nRow   =   plvItem->iItem;   
    int   nCol   =   plvItem->iSubItem;   
    return   SetItemColor(nCol,   nRow,   clrItem);   
    }   void   CColorListCtrl::OnNMCustomdraw(NMHDR   *pNMHDR,   LRESULT   *pResult)   
    {   
    LPNMLVCUSTOMDRAW   pNMCD   =   reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR);   
    //   TODO:   在此添加控件通知处理程序代码   
    *pResult   =   0;   
        
    int   nRows   =   m_lstItemColor.GetCount();   
    int   nRow   =   int(pNMCD->nmcd.dwItemSpec);   
    COLORREF   *clrCol   =   NULL;   
    if   (nRows   &&   nRow   <   nRows)   
    {   
    clrCol   =   m_lstItemColor.GetAt(m_lstItemColor.FindIndex(nRow));   
    }   
        
    switch(pNMCD->nmcd.dwDrawStage)   
    {   
    case   CDDS_PREPAINT   :   
    *pResult   =   CDRF_NOTIFYITEMDRAW;   
    return;   

    case   CDDS_ITEMPREPAINT:   
    if   (clrCol)   
    pNMCD->clrText   =   clrCol[0];   
    *pResult   =   CDRF_NOTIFYSUBITEMDRAW;   
    return;   

    case   CDDS_SUBITEM   |   CDDS_ITEMPREPAINT:   
    if   (clrCol)   
    pNMCD->clrText   =   clrCol[pNMCD->iSubItem];   
    *pResult   =   CDRF_NEWFONT;   
    return;   
    }   
        
    }   /////////////////////////////////////////////////////////////////////////////
    // CColorListCtrl message handlers
      

  4.   

    一个是ColorListCtrl.h文件
    一个是ColorListCtrl.cpp文件
      

  5.   

    http://vcer.net/1046595482643.html
    里面有楼主想要的答案