Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / MFC

CRHTree - An Owner-drawn CTreeCtrl that has Open/Close and Checkboxes on the Right

Rate me:
Please Sign up or sign in to vote.
4.76/5 (10 votes)
22 Aug 2007CPOL8 min read 83.4K   4.2K   55  
An owner-drawn CTreeCtrl that has checkboxes and open/close controls aligned vertically on the right hand edge of the tree for easy viewing regardless of the horizontal scroll position.
#pragma once

#include "Theme.h"

/////////////////////////////////////////////////////////////////////
// CRHTree, 2nd version (Aug 07)
// Written by Paul Roberts, http://www.tlhouse.co.uk
//
// Inspired by Jim Alsup's VividTree article: 
// http://www.codeproject.com/treectrl/VividTree.asp
//
// Use it freely, but please leave this header intact
//

class CRHTree : public CTreeCtrl
{
	DECLARE_DYNAMIC(CRHTree)

// Data Members
protected:
	typedef enum 
	{
		eGroupText,
		eGroupBack,
		eItemText,
		eItemBack,
		eWidgetHot
	} TreeColor;

	// Theme stuff
	CTheme							m_Theme;
	BOOL							m_bThemeInited;
	
	// Open/close widget...
	HTREEITEM						m_hWidgetHlt;
	HICON							m_hWidgetExpand;
	HICON							m_hWidgetCollapse;
	HICON							m_hWidgetExpandHot;
	HICON							m_hWidgetCollapseHot;
	COLORREF						m_crWidgetBack;
	
	// Spacers
	int								m_nHSpacer;

// Methods
public:
	CRHTree();
	virtual ~CRHTree();

public:
	virtual void		InvalidateItem( HTREEITEM hItem );
	virtual void		InvalidateAncestors( HTREEITEM hItem );

	// V2 - fix up drawing after possible horz scroll
	virtual BOOL		EnsureVisible( HTREEITEM hItem );

protected:
	// XP/Vista theme support - called on demand
	virtual void		InitTheme();
	
	// Drawing...
	virtual void		DrawBackground( CDC* pDC, CRect rClient );
	virtual void		DrawItems( CDC* pDC, CRect rClient );
	virtual void		DrawItem( CDC* pDC, CRect rClient, HTREEITEM hItem, CRect rItem );
	virtual void		DrawWidget( CDC* pDC, CRect rCell, BOOL bExpanded, BOOL bActive = FALSE );
	virtual void		DrawCheck( CDC* pDC, CRect rCell, int nCheckState );
	virtual void		DrawGroupWash( CDC* pDC, CRect rCell );
	virtual void		DrawGroupTitle( CDC* pDC, CRect rCell, LPCTSTR lpszTitle );
	virtual void		DrawItemTitle( CDC* pDC, CRect rCell, LPCTSTR lpszTitle );
	virtual void		DrawItemImage( CDC* pDC, CRect rCell, HTREEITEM hItem, BOOL bSelected );
	virtual void		OffsetTextRect( CRect& rText );
	virtual void		DrawItemLines( CDC* pDC, HTREEITEM hItem );
	virtual int			CalcHorzSpacer( CDC* pDC );
	virtual BOOL		DrawPNG( UINT nIDPNG, CDC* pDC, int nLeft, int nTop, int nWidth, int nHeight );
	virtual COLORREF	GetPartColor( TreeColor nClr );
	
	// Item state...
	virtual BOOL		IsGroup( HTREEITEM hItem );
	virtual int			GetCheckState( HTREEITEM hItem );
	virtual BOOL		HasImage( HTREEITEM hItem );
	
	// Widget...
	virtual void		PrepareWidgetIcons( CDC* pDC );
	virtual void		DestroyWidgetIcons();
	virtual void		GetWidgetHRange( long& nLeft, long& nRight ); // V2
	virtual BOOL		HitTestWidget( CPoint ptLocal, HTREEITEM& hItem );

	virtual void		HiliteWidget( CPoint ptLocal );
	
	// CheckBoxes...
	virtual BOOL		HitTestCheckBox( CPoint ptLocal, HTREEITEM& hItem );
	

// MFC Stuff below here
protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnPaint();
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
	afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
	afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt); // V2
	virtual afx_msg BOOL OnToolTipText( UINT id, NMHDR* pNMHDR, LRESULT* pResult );
};


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
Started programming on a Commodore Vic 20(!), and later entered employment programming for the Mac back in the days of System 6. Soon the pull of the Dark Side became too strong and I switched to Windows (Win 3.1) and have been coding for Windows ever since.

I'm now lead programmer for a small software house in Glasgow, Scotland. Our main products include PTFB Pro, ColorCache, and LogMeister.

Comments and Discussions