如何改变clistctrl的背景色,选中的色彩。和头部标题的颜色
采用的是report形式的风格

解决方案 »

  1.   

    up。背景色可以用NMCUSTOMDRAW来设置,选中的色彩如何改变,我也想知道
      

  2.   

    重载NM_CUSTOMDRAW消息
    NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
    //set default draw
    *pResult = CDRF_DODEFAULT;
    if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)
    {
    *pResult = CDRF_NOTIFYITEMDRAW;
    }
    else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
    {
    // This is the notification message for an item.  We'll request
    // notifications before each subitem's prepaint stage. *pResult = CDRF_NOTIFYSUBITEMDRAW;
    }
    else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))
    {//paint the list control items and subitems
    //get the number of item is going to paint
    int nitem = static_cast<int> (pLVCD->nmcd.dwItemSpec);
    //get the number of subitem is going to paint
    int nsubitem = pLVCD->iSubItem;
    //get first selected item position
    POSITION npos = GetFirstSelectedItemPosition();
    if(nitem == GetNextSelectedItem(npos)) //如果被选中使用自绘输出
    { //所画项是选中项
    //get point of default dc objet
    CDC* pdc = CDC::FromHandle(pLVCD->nmcd.hdc);
    CString str;
    CRect rect;
    UINT nFormat = DT_VCENTER | DT_SINGLELINE;
    //get rect of the sub item which is going to paint
    GetSubItemRect(nitem, nsubitem, LVIR_BOUNDS, rect);
    rect.left += 3; //调整自绘输出位置
    //get the context is going to paint on the subitem
    str = GetItemText(nitem,nsubitem);
    pDC->SetTextColor(RGB(0,0,0));//字体颜色,因该是白色
    pDC->FillSolidRect(&rect, RGB(255,255,255));//背景,深蓝色吧,试试
    pDC->SetBkColor(RGB(255,255,255));//字体背景色,颜色同上
    //set back is transparent
    CSize msize;
    //get the context length which is going to paint
    msize = pdc->GetTextExtent(str);
    //paint the text
    pdc->DrawText(str, &rect, nFormat); //自绘输出
    *pResult = CDRF_SKIPDEFAULT; // We've painted everything.
    }
    else
    *pResult = CDRF_DODEFAULT;
    }
    }
      

  3.   

    楼上的兄台,怎么重载NM_CUSTOMDRAW消息啊? 我怎么找不到???
      

  4.   

    http://www.vckbase.com/code/listcode.asp?mclsid=3&sclsid=323
    希望对你有所帮助
      

  5.   

    http://expert.csdn.net/Expert/topic/2499/2499495.xml?temp=.0897333
      

  6.   

    http://www.embhelp.com/bbs/dispbbs.asp?boardID=14&ID=219
      

  7.   

    改变CListCtrl的背景色用SetBkColor()函数就可以!
    如果显示文本还需要用SetTextBkColor()来设置文本的背景色
      

  8.   

    我去年做过,可现在不记的了,好像要改变选中条目的彦色要把ClistCtrl设置为自画,然后重载DRWAW函数,好像还有用到CListCtrl控件的项目结构LITEM。。
    好像是这样,不记的了,回云我帮你查查
      

  9.   

    我这里有一个CExListCtrl类
    功能说明:能将行给予不同的颜色和背景 
    #include "AfxTempl.h"
    /////////////////////////////////////////////////////////////////////////////
    // CExListCtrl windowclass CExListCtrl : public CListCtrl
    {
    // Construction
    public:
    CExListCtrl();// Attributes
    public:// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CExListCtrl)
    //}}AFX_VIRTUAL
    /////////////////////////////////////////////////////////////////
    public:
    //operation
    void SetSelText(COLORREF crSelText);
    void SetSelBk(COLORREF crSelBk);
    void SetSelFontSize(int nFontSize);
    //attribute
    typedef CList<int,int>CItemsList;
    CItemsList m_selItems;
    protected:
    COLORREF m_crSelText; //选中的字体颜色
    COLORREF m_crSelBk; //选中的背景
    int m_nFontSize; //字体大小 
    CFont m_selFont; //选中的字体
    /////////////////////////////////////////////////////////////////// Implementation
    public:
    virtual ~CExListCtrl(); // Generated message map functions
    protected:
    //{{AFX_MSG(CExListCtrl)
    afx_msg void OnDestroy();
    //}}AFX_MSG
    afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult); DECLARE_MESSAGE_MAP()
    };
    CExListCtrl::CExListCtrl()
    {
    POSITION pos=m_selItems.GetHeadPosition();
    while(pos != NULL)
    {
    m_selItems.GetNext(pos);
    m_selItems.RemoveAt(pos);
    } m_crSelText=RGB(0,0,0);
    m_crSelBk=RGB(255,255,255);
    m_nFontSize=15; //创建字体
    LOGFONT lf;
    memset(&lf,0,sizeof(LOGFONT));
    lf.lfHeight=m_nFontSize;
    _tcscpy(lf.lfFaceName,_T("宋体"));
    lf.lfWeight=400;
    //lf.lfItalic=TRUE;  
    m_selFont.CreateFontIndirect(&lf);
    }CExListCtrl::~CExListCtrl()
    {
    m_selFont.DeleteObject();}
    BEGIN_MESSAGE_MAP(CExListCtrl, CListCtrl)
    //{{AFX_MSG_MAP(CExListCtrl)
    ON_WM_DESTROY()
    //}}AFX_MSG_MAP
    ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CExListCtrl message handlers
    /////////////////////////////////////////////////////////////////////
    //主要修改内容:加入链表 给链表中的Item显示指定的颜色
    void CExListCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
    {
    NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);

    //默认处理
    *pResult = CDRF_DODEFAULT;

    //检查Paint的阶段如果要处理哪一段给*pResult赋予相应的值
    if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)
    {
    *pResult = CDRF_NOTIFYITEMDRAW;
    }
    else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
    {
    if(m_selItems.Find(pLVCD->nmcd.dwItemSpec) != NULL)
    {
    ::SelectObject(pLVCD->nmcd.hdc,m_selFont.GetSafeHandle());
    if(pLVCD->nmcd.uItemState & CDIS_SELECTED ) 
    {
    pLVCD->clrText=m_crSelText;
    pLVCD->clrTextBk=m_crSelBk;  
    }
    else
    {
    pLVCD->clrText=m_crSelText;
    pLVCD->clrTextBk=m_crSelBk;
    }
    *pResult = CDRF_NEWFONT;
    }
    else
    *pResult = CDRF_DODEFAULT;
    }


    }void CExListCtrl::OnDestroy() 
    {
    CListCtrl::OnDestroy();

    }void CExListCtrl::SetSelText(COLORREF crSelText)
    {
    m_crSelText=crSelText;
    }void CExListCtrl::SetSelBk(COLORREF crSelBk)
    {
    m_crSelBk=crSelBk;
    }void CExListCtrl::SetSelFontSize(int nFontSize)
    {
    m_nFontSize=nFontSize;
    m_selFont.DeleteObject(); //创建字体
    LOGFONT lf;
    memset(&lf,0,sizeof(LOGFONT));
    lf.lfHeight=m_nFontSize;
    _tcscpy(lf.lfFaceName,_T("宋体"));
    lf.lfWeight=400;
    //lf.lfItalic=TRUE;  
    m_selFont.CreateFontIndirect(&lf);
    }
      

  10.   

    codeproject.com 有个 skinlist