Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

TreeCtrl - A WTL tree control with Windows Vista style item selection

, 9 Apr 2006 CPOL
A WTL tree control that supports Windows Vista style selection and multiple selection.
treectrldemo.zip
res
ExampleItems.bmp
TreeCtrlDemo.exe.manifest
TreeCtrlDemo.ico
// MainDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

#include <sstream>
#include <iomanip>
#include <atltime.h>
#include "TreeCtrl.h"

class CMainDlg : public CDialogImpl<CMainDlg>
{
public:
	CMainDlg()
	{
		m_bShowThemed = TRUE;
	}
	
	enum { IDD = IDD_MAINDLG };

	BEGIN_MSG_MAP(CMainDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
		COMMAND_ID_HANDLER(IDOK, OnOK)
		COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
		COMMAND_ID_HANDLER(IDC_THEME, OnTheme)
		REFLECT_NOTIFICATIONS()
	END_MSG_MAP()

protected:
	CTreeCtrl m_wndTreeCtrl;
	CEdit m_wndEdit;
	CImageList m_ilItemImages;
	BOOL m_bShowThemed;

	LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		// center the dialog on the screen
		CenterWindow();

		// set icons
		HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
			IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
		SetIcon(hIcon, TRUE);
		HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
			IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
		SetIcon(hIconSmall, FALSE);

		m_wndTreeCtrl.SubclassWindow( GetDlgItem( IDC_TREECTRL ) );
		m_wndEdit.Attach( GetDlgItem( IDC_EDIT ) );
		
		m_wndEdit.SetWindowText( _T( "Click here to remove focus from Tree control (greyed selection)" ) );
		
		if ( !m_ilItemImages.CreateFromImage( IDB_EXAMPLE, 16, 0, RGB( 255, 0, 255 ), IMAGE_BITMAP, LR_CREATEDIBSECTION ) )
			return FALSE;
		
		m_wndTreeCtrl.SetImageList( m_ilItemImages );
		
		HTREEITEM hParent = m_wndTreeCtrl.InsertItem( _T( "Top Level 1.1" ), 0, 1, TVI_ROOT, TVI_LAST );
		m_wndTreeCtrl.InsertItem( _T( "Second Level 2.1" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.InsertItem( _T( "Second Level 2.2" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.Expand( hParent, TVE_EXPAND );
		m_wndTreeCtrl.SelectItem( hParent );
		hParent = m_wndTreeCtrl.InsertItem( _T( "Second Level 2.3" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.InsertItem( _T( "Third Level 3.1" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.InsertItem( _T( "Third Level 3.2" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.Expand( hParent, TVE_EXPAND );
		hParent = m_wndTreeCtrl.InsertItem( _T( "Third Level 3.3" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.InsertItem( _T( "Fourth Level 4.1" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.Expand( hParent, TVE_EXPAND );
		hParent = m_wndTreeCtrl.InsertItem( _T( "Top Level 1.2" ), 0, 1, TVI_ROOT, TVI_LAST );
		m_wndTreeCtrl.InsertItem( _T( "Second Level 2.1" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.InsertItem( _T( "Second Level 2.2" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.Expand( hParent, TVE_EXPAND );
		hParent = m_wndTreeCtrl.InsertItem( _T( "Second Level 2.3" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.InsertItem( _T( "Third Level 3.1" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.InsertItem( _T( "Third Level 3.2" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.Expand( hParent, TVE_EXPAND );
		hParent = m_wndTreeCtrl.InsertItem( _T( "Third Level 3.3" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.InsertItem( _T( "Fourth Level 4.1" ), 0, 1, hParent, TVI_LAST );
		m_wndTreeCtrl.Expand( hParent, TVE_EXPAND );		
		
		m_wndTreeCtrl.SetMultipleSelect( TRUE );
		m_wndTreeCtrl.SetFocus();		
		
		return FALSE;
	}
	
	LRESULT OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		CSimpleDialog<IDD_ABOUTBOX, FALSE> dlg;
		dlg.DoModal();
		return 0;
	}

	LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		// TODO: Add validation code 
		EndDialog(wID);
		return 0;
	}

	LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		EndDialog(wID);
		return 0;
	}
	
	LRESULT OnTheme(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	{
		m_bShowThemed = !m_bShowThemed;
		m_wndTreeCtrl.ShowThemed( m_bShowThemed );
		return 0;
	}
};

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)

Share

About the Author

AlanW

United Kingdom United Kingdom
Alan has been developing applications for a very long time (~10 years), but he's not bitter about this at all. My main area of expertise is C++. He lives in Sweden with his beautiful wife, daughter and son and enjoys climbing mountains and kayaking in his spare time (which isn't much).

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 10 Apr 2006
Article Copyright 2006 by AlanW
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid