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

Tree control templates

, 11 Jun 2001 CPOL
Three tree control templates to easily operate with CTreeCtrl derived objects
treecontroltemplates_demo.zip
PruebaPTree.aps
PruebaPTree.clw
PruebaPTree.dsp
PruebaPTree.dsw
PruebaPTree.exe
PruebaPTree.plg
res
PruebaPTree.ico
treecontroltemplates_src.zip
// PruebaPTreeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PruebaPTree.h"
#include "PruebaPTreeDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CPruebaPTreeDlg dialog

CPruebaPTreeDlg::CPruebaPTreeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPruebaPTreeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPruebaPTreeDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPruebaPTreeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPruebaPTreeDlg)
	DDX_Control(pDX, IDC_LB_ITEMS, mc_lbItems);
	DDX_Control(pDX, IDC_TC_TREE, mc_tcTree);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPruebaPTreeDlg, CDialog)
	//{{AFX_MSG_MAP(CPruebaPTreeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BT_COLLAPSE, OnBtCollapse)
	ON_BN_CLICKED(IDC_BT_EXPAND, OnBtExpand)
	ON_BN_CLICKED(IDC_BT_RELOAD, OnBtReload)
	ON_BN_CLICKED(IDC_BT_ITERATE_DEEP, OnBtIterateDeep)
	ON_BN_CLICKED(IDC_BT_ITERATE_WIDTH, OnBtIterateWidth)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPruebaPTreeDlg message handlers

BOOL CPruebaPTreeDlg::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
	PopulateTree();	
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPruebaPTreeDlg::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 CPruebaPTreeDlg::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 CPruebaPTreeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPruebaPTreeDlg::PopulateTree()
{
	mc_tcTree.DeleteAllItems();

	HTREEITEM hti1= NULL, hti2= NULL;

	hti1= mc_tcTree.InsertItem("Item 1");
			mc_tcTree.InsertItem("Item 11", hti1);
			mc_tcTree.InsertItem("Item 12", hti1);
			mc_tcTree.InsertItem("Item 13", hti1);
	hti1= mc_tcTree.InsertItem("Item 2");
			mc_tcTree.InsertItem("Item 21", hti1);
			mc_tcTree.InsertItem("Item 22", hti1);
			mc_tcTree.InsertItem("Item 23", hti1);
	hti1= mc_tcTree.InsertItem("Item 3");
				mc_tcTree.InsertItem("Item 31", hti1);
		hti2=	mc_tcTree.InsertItem("Item 32", hti1);
					mc_tcTree.InsertItem("Item 321", hti2);
					mc_tcTree.InsertItem("Item 322", hti2);
					mc_tcTree.InsertItem("Item 323", hti2);
				mc_tcTree.InsertItem("Item 33", hti1);
	hti1= mc_tcTree.InsertItem("Item 4");
			mc_tcTree.InsertItem("Item 41", hti1);
			mc_tcTree.InsertItem("Item 42", hti1);
			mc_tcTree.InsertItem("Item 43", hti1);
	hti1= mc_tcTree.InsertItem("Item 5");
			mc_tcTree.InsertItem("Item 51", hti1);
			mc_tcTree.InsertItem("Item 52", hti1);
			mc_tcTree.InsertItem("Item 53", hti1);
}

void CPruebaPTreeDlg::OnBtCollapse() 
{
	mc_tcTree.SetRedraw(FALSE);

	mc_tcTree.ExpandItem(TVI_ROOT, TVE_COLLAPSE);

	mc_tcTree.SetRedraw(TRUE);
}

void CPruebaPTreeDlg::OnBtExpand() 
{
	mc_tcTree.SetRedraw(FALSE);
	
	mc_tcTree.ExpandItem();
	
	mc_tcTree.SetRedraw(TRUE);
}

void CPruebaPTreeDlg::OnBtReload() 
{
	mc_tcTree.SaveStatus();

	PopulateTree();	

	mc_tcTree.RestoreStatus();
}

void CPruebaPTreeDlg::OnBtIterateDeep() 
{
	mc_lbItems.ResetContent();

	mc_tcTree.Initialize(true);

	HTREEITEM hItem= mc_tcTree.GetNext();

	while (hItem != NULL)
	{
		mc_lbItems.AddString(mc_tcTree.GetItemText(hItem));

		hItem= mc_tcTree.GetNext();
	}
}

void CPruebaPTreeDlg::OnBtIterateWidth() 
{
	mc_lbItems.ResetContent();

	mc_tcTree.Initialize(false);

	HTREEITEM hItem= mc_tcTree.GetNext();

	while (hItem != NULL)
	{
		mc_lbItems.AddString(mc_tcTree.GetItemText(hItem));

		hItem= mc_tcTree.GetNext();
	}
}

// Force compilation to detect errors
template class TTCExpand<CTreeCtrl>;
template class TTCPersist<CTreeCtrl>;
template class TTCIterator<CTreeCtrl>;

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

Francisco José Sen del Prado
Software Developer (Senior)
Spain Spain
I started programming in Basic language when I was 14 years old. The second programming language I learned was Z80 assembler. I've been programming with C++ for the last 20 years. I attended the 2012 C++ Now event in Aspen and it was the most exciting experience I've lived in my professional life. I met a lot of brilliant C++ minds and I have very good memories from that trip.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 12 Jun 2001
Article Copyright 2001 by Francisco José Sen del Prado
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid