Click here to Skip to main content
15,889,833 members
Articles / Desktop Programming / Win32

MultiSelection Tree Control

Rate me:
Please Sign up or sign in to vote.
2.87/5 (7 votes)
27 Oct 2008CPOL1 min read 38.4K   1.4K   11  
This tree control allows the user to select multiple tree items and enable selection through rubber banding.
// CustomTreeCtrlDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CustomTreeCtrl.h"
#include "CustomTreeCtrlDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define NMEMBERSTAMPPROJ 4
#define NMEMBERCASTPROJ  3

__PROJECTMEMBER::~__PROJECTMEMBER()
{
	delete m_pCustomData;
	m_pCustomData = NULL;

}
__PROJECTLEAD::~__PROJECTLEAD()
{
	delete m_pCustomData;
	m_pCustomData = NULL;


	delete[] m_projMembers;
	m_projMembers = NULL;
	
}
__PROJECT::~__PROJECT()
{
	delete m_pCustomData;

	//Loop through the project leads and delete the custom data
	delete m_projLead;
	m_projLead = NULL;

	m_pCustomData = NULL;

}



void ItemInfo( LOOPINFO* pLoopInfo )
{
	if( !pLoopInfo->hItem )
		return;

	if( !pLoopInfo->pParent )
		return;

	CCustomTreeCtrlDlg* pDlg = (CCustomTreeCtrlDlg*)(pLoopInfo->pParent);
	CString str = pDlg->m_treeCtrl.GetItemText( pLoopInfo->hItem );
	::AfxMessageBox( str );
	

}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCustomTreeCtrlDlg dialog

CCustomTreeCtrlDlg::CCustomTreeCtrlDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCustomTreeCtrlDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCustomTreeCtrlDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pImageList = NULL;
}
CCustomTreeCtrlDlg::~CCustomTreeCtrlDlg()
{
	//Delete the imagelist
	if( m_pImageList )
	{
		m_pImageList->DeleteImageList();
		delete m_pImageList;
	}
	
	if( m_pStampProject )
		delete m_pStampProject;

	if( m_pCastProject )
		delete m_pCastProject;
	
}

void CCustomTreeCtrlDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCustomTreeCtrlDlg)
	DDX_Control(pDX, IDC_SEL_LIST, m_ctrlSelList);
	DDX_Control(pDX, IDC_TREE1, m_treeCtrl);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCustomTreeCtrlDlg, CDialog)
	//{{AFX_MSG_MAP(CCustomTreeCtrlDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ITERATE, OnIterate)
	ON_MESSAGE( TREE_SELCHANGED,OnTreeSelectionChanged )
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCustomTreeCtrlDlg message handlers

BOOL CCustomTreeCtrlDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_treeCtrl.AddLineStyle();
	m_treeCtrl.AddLineAtRoot();
	m_treeCtrl.AddButton();
	m_treeCtrl.EnableEditLabel();

	m_treeCtrl.SetBackgroundColor(RGB(91,168,111));
	m_treeCtrl.SetLineColor( RGB(255,0,0));
	m_treeCtrl.SetIndent(25);

	InitializeTreeControl();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCustomTreeCtrlDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCustomTreeCtrlDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCustomTreeCtrlDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//******************************************************************************
//To initialize the tree control
void CCustomTreeCtrlDlg::InitializeTreeControl()
{
	//First create the imagelist
	CreateImageList();

	//Set the image list to the tree control
	m_treeCtrl.SetImageList(m_pImageList,TVSIL_NORMAL);

	//Insert the tree items: TV_INSERTSTRUCT is a typedef of TVINSERTSTRUCT
	CUSTOMINSERTSTRUCT tvIS;
	

	//Insert the root item
	//---------------------
		tvIS.m_tvIS.hParent		= NULL;			//As this is a root
		tvIS.m_tvIS.hInsertAfter	= TVI_ROOT;		//Root item
		//Valid members of this item are: text,image and the selected image
		tvIS.m_tvIS.item.mask		= TVIF_IMAGE | TVIF_TEXT | TVIF_SELECTEDIMAGE; 
		tvIS.m_tvIS.item.iImage	= 0;			//Index of the image "Company"
		tvIS.m_tvIS.item.pszText	= "ISE";		//Label of the item
		tvIS.m_tvIS.item.iSelectedImage = 0;		//Index of the image to be shown when item is selected
		
		HTREEITEM hRoot;
		hRoot = m_treeCtrl.InsertItem(&tvIS);

	//Create the project members
	//--------------------------

		//STAMP
		m_pStampProjectMembers = new PROJECTMEMBER[NMEMBERSTAMPPROJ];
		m_pStampProjectMembers[0].m_sName = "Babu";
		m_pStampProjectMembers[1].m_sName = "Saravanan";
		m_pStampProjectMembers[2].m_sName = "Rajendra";
		m_pStampProjectMembers[3].m_sName = "Sundar";

		//CAST
		m_pCastProjectMembers = new PROJECTMEMBER[NMEMBERCASTPROJ];
		m_pCastProjectMembers[0].m_sName = "Shetty";
		m_pCastProjectMembers[1].m_sName = "Prashanth";
		m_pCastProjectMembers[2].m_sName = "Kamesh";
		 



	//Create the project lead
	//-----------------------
		//STAMP
		m_pStampProjectLead = new PROJECTLEAD;
		m_pStampProjectLead->m_sName = "Achutha";
		m_pStampProjectLead->m_nProjmembers = NMEMBERSTAMPPROJ;
		m_pStampProjectLead->m_projMembers = m_pStampProjectMembers;

		//CAST
		m_pCastProjectLead = new PROJECTLEAD;
		m_pCastProjectLead->m_sName = "Achutha";
		m_pCastProjectLead->m_nProjmembers = NMEMBERCASTPROJ;
		m_pCastProjectLead->m_projMembers = m_pCastProjectMembers;


	//Create the project 
	//------------------
		//STAMP
		m_pStampProject	= new PROJECT;
		m_pStampProject->m_sTitle = "Cast";
		m_pStampProject->m_nProjLeads = 1;
		m_pStampProject->m_projLead = m_pStampProjectLead;

		//CAST
		m_pCastProject	= new PROJECT;
		m_pCastProject->m_sTitle = "Stamp";
		m_pCastProject->m_nProjLeads = 1;
		m_pCastProject->m_projLead = m_pCastProjectLead;

		 

	//Inser the project
		InserProject( m_pStampProject, hRoot );
		InserProject( m_pCastProject, hRoot );
	




}
//******************************************************************************
void CCustomTreeCtrlDlg::CreateImageList()
{
	//Create the image list
	m_pImageList = new CImageList;

	//ILC_MASK - ImageList contains 2 bitmaps.One is a monochrome bitmap
	//used as the mask.Masking is needed to provide the transparent image
	m_pImageList->Create( 32,32,ILC_COLOR32 | ILC_MASK,5,5);

	//Load the images one by one
	CBitmap bmp,maskBmp;
	bmp.LoadBitmap( IDB_BITMAP_COMPANY );
	maskBmp.LoadBitmap( IDB_BITMAP_COMPANY_MASK );
	m_pImageList->Add(&bmp,&maskBmp );
	bmp.DeleteObject();
	maskBmp.DeleteObject();

	bmp.LoadBitmap( IDB_BITMAP_FOLDER );
	maskBmp.LoadBitmap( IDB_BITMAP_FOLDER_MASK );
	m_pImageList->Add(&bmp,&maskBmp );
	bmp.DeleteObject();
	maskBmp.DeleteObject();

	bmp.LoadBitmap( IDB_BITMAP_FOLDER_OPEN );
	maskBmp.LoadBitmap( IDB_BITMAP_FOLDER_OPEN_MASK );
	m_pImageList->Add(&bmp,&maskBmp );
	bmp.DeleteObject();
	maskBmp.DeleteObject();

	bmp.LoadBitmap( IDB_BITMAP_MANAGER );
	maskBmp.LoadBitmap( IDB_BITMAP_MANAGER_MASK );
	m_pImageList->Add(&bmp,&maskBmp );
	bmp.DeleteObject();
	maskBmp.DeleteObject();

	bmp.LoadBitmap( IDB_BITMAP_HEAD );
	maskBmp.LoadBitmap( IDB_BITMAP_HEAD_MASK );
	m_pImageList->Add(&bmp,&maskBmp );
	bmp.DeleteObject();
	maskBmp.DeleteObject();
}
//******************************************************************************
//To inser the project into the tree
void CCustomTreeCtrlDlg::InserProject(PPROJECT pProj,HTREEITEM hRoot)
{
	if( !pProj ||  !hRoot )
		return;
	
	
	CUSTOMINSERTSTRUCT tvIS;

	//First Inser the project item
	//------------------------------
		tvIS.m_tvIS.hParent		=	hRoot;		//As the project in under the root
		tvIS.m_tvIS.hInsertAfter	=	TVI_FIRST;	//
		//Valid members of this item are: text,image and the selected image
		tvIS.m_tvIS.item.mask		= TVIF_IMAGE | TVIF_TEXT | TVIF_SELECTEDIMAGE; 
		//Index of the image for project
		tvIS.m_tvIS.item.iImage	= 1;
		//Label of the item
		tvIS.m_tvIS.item.pszText	= (LPSTR)( (pProj->m_sTitle).operator LPCTSTR());
		//Index of the image to be shown when item is selected
		tvIS.m_tvIS.item.iSelectedImage = 1;	

		//Create the custom data
		PCUSTOMITEMDATA pCustomData = new CUSTOMITEMDATA;
		pCustomData->m_iNormalImage = 1;
		pCustomData->m_iDragImage   = 1;
		pCustomData->m_iExpandedImage = 2;
		pCustomData->m_iState		  = PROJ;
		//Disable the label edit
		pCustomData->m_bEditable	= false;
		tvIS.m_pCustomData          = pCustomData;
		pProj->m_pCustomData	=	pCustomData;
		
	HTREEITEM hPRoject = m_treeCtrl.InsertItem(&tvIS);
		
	//Insert the project lead
	//-----------------------
		int i = 0;
		for( i = 0; i < pProj->m_nProjLeads; i++ )
		{
			InserProjectLead( &pProj->m_projLead[i],hPRoject);
		}
}
//******************************************************************************
//To insert the project leader
void CCustomTreeCtrlDlg::InserProjectLead(PPROJECTLEAD pProjLead,HTREEITEM hProj )
{
	if( !hProj ||  !pProjLead )
		return;

		CUSTOMINSERTSTRUCT tvIS;

	//Insert the project leader
	//-------------------------
		tvIS.m_tvIS.hParent		=	hProj;		//As the project in under the root
		tvIS.m_tvIS.hInsertAfter	=	TVI_LAST;	//
		//Valid members of this item are: text,image and the selected image
		tvIS.m_tvIS.item.mask		= TVIF_IMAGE | TVIF_TEXT | TVIF_SELECTEDIMAGE; 
		//Index of the image for project leader
		tvIS.m_tvIS.item.iImage	= 3;
		//Label of the item
		tvIS.m_tvIS.item.pszText	= (LPSTR)( (pProjLead->m_sName).operator LPCTSTR());
		//Index of the image to be shown when item is selected
		tvIS.m_tvIS.item.iSelectedImage = 3;
		
		//Create the custom data
		PCUSTOMITEMDATA pCustomData = new CUSTOMITEMDATA;
		pCustomData->m_iNormalImage = 3;
		pCustomData->m_iDragImage   = 3;
		pCustomData->m_iExpandedImage = 3;
		pCustomData->m_iState		  = PROJLEAD;
		tvIS.m_pCustomData          = pCustomData;
		pProjLead->m_pCustomData	=	pCustomData;

		HTREEITEM hProjLead = m_treeCtrl.InsertItem(&tvIS);


		//Insert the project members
		//--------------------------		
		int i = 0;
		for( i = 0; i < pProjLead->m_nProjmembers; i++ )
		{
			InsertProjectMembers( &pProjLead->m_projMembers[i] ,hProjLead );
		}
	

}
//******************************************************************************
//To insert the project leader
void CCustomTreeCtrlDlg::InsertProjectMembers( PPROJECTMEMBER pProjMem,HTREEITEM hProjLead )
{
	if( !pProjMem || !hProjLead )
		return;

	CUSTOMINSERTSTRUCT tvIS;

	//Insert the project leader
	//-------------------------
		tvIS.m_tvIS.hParent		=	hProjLead;		//As the project in under the root
		tvIS.m_tvIS.hInsertAfter	=	TVI_LAST;	//
		//Valid members of this item are: text,image and the selected image
		tvIS.m_tvIS.item.mask		= TVIF_IMAGE | TVIF_TEXT | TVIF_SELECTEDIMAGE; 
		//Index of the image for project leader
		tvIS.m_tvIS.item.iImage	= 4;
		//Label of the item
		tvIS.m_tvIS.item.pszText	= (LPSTR)( (pProjMem->m_sName).operator LPCTSTR());
		//Index of the image to be shown when item is selected
		tvIS.m_tvIS.item.iSelectedImage = 4;

		//Create the custom data
		PCUSTOMITEMDATA pCustomData = new CUSTOMITEMDATA;
		pCustomData->m_iNormalImage = 4;
		pCustomData->m_iDragImage   = 4;
		pCustomData->m_iExpandedImage = 4;
		pCustomData->m_iState		  = PROJMEMBER;
		tvIS.m_pCustomData          = pCustomData;
		pProjMem->m_pCustomData	=	pCustomData;

		HTREEITEM hProjMem = m_treeCtrl.InsertItem(&tvIS);

	/*	//For each member insert a trainee: JUST FOR CHECKING
		tvIS.m_tvIS.hParent		=	hProjMem;		//As the project in under the root
		tvIS.m_tvIS.hInsertAfter	=	TVI_LAST;	//
		tvIS.m_tvIS.item.mask		= TVIF_IMAGE | TVIF_TEXT | TVIF_SELECTEDIMAGE; 
		tvIS.m_tvIS.item.iImage	= 4;
		char name[64];
		strcpy( name, (pProjMem->m_sName).operator LPCTSTR() );
		strcat( name,"Trainee");

		tvIS.m_tvIS.item.pszText	= name;
		tvIS.m_tvIS.item.iSelectedImage = 4;
		HTREEITEM hProjTrainee = m_treeCtrl.InsertItem(&tvIS);*/

	
}

void CCustomTreeCtrlDlg::OnIterate() 
{
	// TODO: Add your control notification handler code here

	//Get the current selected item and loop through till end
	HTREEITEM hCurSelItem	=	m_treeCtrl.GetSelectedItem();

	if( !hCurSelItem )
		return;
	m_treeCtrl.IterateItems( ItemInfo,hCurSelItem,NULL );
}

LRESULT CCustomTreeCtrlDlg::OnTreeSelectionChanged(WPARAM wparam,LPARAM lparam )
{
	CArray<HTREEITEM,HTREEITEM>*	pSelArray = (CArray<HTREEITEM,HTREEITEM>*)wparam;

	int iSelItemCnt = (*pSelArray).GetSize();

	int i = 0;
	m_ctrlSelList.ResetContent();
	for( ; i < iSelItemCnt; i++ )
	{
		HTREEITEM hItem = (*pSelArray)[i];
		CString str = m_treeCtrl.GetItemText( hItem );
		m_ctrlSelList.AddString( str );
	}

	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)


Written By
Software Developer (Senior)
India India
I'm working as Senior software Engineer since 7 years and interested in MFC and COM programming.

Comments and Discussions