dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Coloring selected row of CListCtrl

  1. #1
    Join Date
    Oct 2019
    Posts
    45

    Coloring selected row of CListCtrl

    I have a class MyList which is derived from CListCtrl.I want to color selected row to RED.I used NMCustomDraw() but problem is as soon as dialog(CDialog) gets opened ,all the rows come with the color which is done for only selected row.
    Code of MyLish.h is as follows:
    Code:
    //MyList.h
    class MyList : public CListCtrl
    {
    	DECLARE_DYNAMIC(MyList)
    
    public:
    	MyList();
    	virtual ~MyList();
    	
    protected:
    	DECLARE_MESSAGE_MAP()
    private:
    		COLORREF mBkColor;
    		COLORREF mForeColor;
    public:
    	afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
    	void SetColor( int row, COLORREF bkColor, COLORREF ForeColor );
    };
    Code for MyList.cpp is as follows:
    Code:
    //MyList.cpp
    #include "stdafx.h"
    #include "MyList.h"
    
    
    // MyList
    
    IMPLEMENT_DYNAMIC(MyList, CListCtrl)
    
    MyList::MyList():mBkColor(RGB(255,0,255)),
    				 mForeColor(RGB(0,255,255))
    {
    
    }
    
    MyList::~MyList()
    {
    }
    
    
    BEGIN_MESSAGE_MAP(MyList, CListCtrl)
    	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnNMCustomdraw)
    END_MESSAGE_MAP()
    
    void MyList::SetColor( int row,COLORREF bkColor, COLORREF ForeColor )
    {
           SetItemState(row, LVIS_SELECTED, LVIS_SELECTED);
    
    	mBkColor = bkColor;
    	mForeColor = ForeColor;
    	Invalidate();
    }
    
    // MyList message handlers
    
    
    
    void MyList::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
    	NMTVCUSTOMDRAW* pLVCD = reinterpret_cast<NMTVCUSTOMDRAW *>( pNMHDR );
    	switch( pNMCD->dwDrawStage )
    	{
    	case CDDS_PREPAINT:
    		// Item prepaint notification.
    		*pResult= CDRF_NOTIFYITEMDRAW;
    		break;
    	case CDDS_ITEMPREPAINT:
    		{
    			if( ( CDIS_SELECTED == ( pNMCD->uItemState & CDIS_SELECTED )))
    			{
    				pNMCD->uItemState = CDIS_DEFAULT;
    				pLVCD->clrText   =  mForeColor;
    				pLVCD->clrTextBk =  mBkColor;
    			}
    		}
    		break;
    
    	default:
    		*pResult = CDRF_DODEFAULT;
    		break;
    	}
    }
    I decalred variable of type MyList m_list in MyDialog.h
    And after that in OnInitDialog() of MyDialog.cpp i just simply invoked m_list.SetColor(LVS_SELECTED, RGB(255,0,0),RGB(33,33,33)) but result of this is color of each row comes out to be red as soon as dialog gets opened.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,579

    Re: Coloring selected row of CListCtrl

    Check it out: https://www.codeproject.com/Articles...l-with-subitem
    Then just remove the code parts you don't need...
    Victor Nijegorodov

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,579

    Re: Coloring selected row of CListCtrl

    Quote Originally Posted by VictorN View Post
    Check it out: https://www.codeproject.com/Articles...l-with-subitem
    Then just remove the code parts you don't need...
    And if it would be not so easy nor interesting for you then have a look at the other articles: https://www.codeproject.com/KB/list/#Custom+Draw
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)