Dynamic Tree Loading

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This article describes how to load a tree structure into MFC’s TreeCtrl class.
The data members are loaded into it dynamically, and it’s very simple to
change the whole structure of the tree. Each node of the tree holds a function
pointer to a function to execute when double clicked.

Here is the basic structure for a node:


typedef struct tagItem
{
	tagITEM * item;
	char * name;
	int (*func)();
	int image;
	int simage;
} Item;


item – A pointer to another branch off the tree (NULL if none).

name – The string the TreeCtrl displays for that node.

func – A function pointer to the function to execute upon double-clicking the node.

image – An integer specifying which image in the image list that the node uses.

simage – An integer specifying which image in the image list that the node uses when selected.

Example use:


int substring1()
{
	AfxMessageBox("substring1");
	return 0;
}

int substring2()
{
	AfxMessageBox("substring2");
	return 0;
}

int string2()
{
	AfxMessageBox("string2");
	return 0;
}

Item BRANCH1[] =
{
	{NULL, "SubString1", substring1, 0, 0},
	{NULL, "SubString2", substring2, 0, 0},
	{NULL, "", NULL, 0, 0}
};

Item root[] =
{
	{NULL, "String1", NULL, 0, 0}, // Root node with a branch on it
	{BRANCH1, NULL, NULL, 0, 0}, // Another Item array for the branch
	{NULL, "String2", string2, 1, 1}, // Single node.
	{NULL, "", NULL, 0, 0} // last element - needed for the loading function to tell when it's at the last node.
};

The root array can contain any number of nodes, or branches, and each branch can do the same.
If the first parameter is NULL, it is a single node with no branches. If the third member
is NULL, there is no function pointer.


To load the TreeCtrl, we now use this recursively called function:


void CTestView::InsertData(HTREEITEM parent, Item * item)
{
	HTREEITEM newparent;

	while(item->name != "") // while we are not at the last member of the structure
	{
		if(item->item == NULL) // if this is a parent node
		{
			// Insert the item into the tree
			newparent = m_pTree->InsertItem(
				TVIF_IMAGE|TVIF_PARAM|TVIF_SELECTEDIMAGE|TVIF_STATE|TVIF_TEXT,
				item->name,
				item->image,
				item->simage,
				(item->func == NULL ? TVIS_BOLD : NULL),
				TVIS_BOLD,
				(LPARAM)item->func,
				parent,
				TVI_LAST);
		}
		else
		{
			InsertData(newparent, item->item); // call this function again with the new structure
		}

		item++; // go to the next member of the array
	}
}

This function is initially called in probably the InitialUpdate function, and it looks like this:


InsertData(TVI_ROOT, root);

Now here is the OnLButtonDblClk handler:


void CTestView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	LPARAM func = m_pTree->GetItemData(m_pTree->GetSelectedItem());

	if(func != NULL)
		(*(reinterpret_cast(func)))(); // Call the function assigned to node

	CTreeView::OnLButtonDblClk(nFlags, point);
}

This code gets a bit complicated with the reinterpret casting, but it’s used to convert
the spare variable in the TreeCtrl TVITEM structure to a function pointer again.


Now you should be able to create your structure to have any arrangement you want, and have
it be able to change with one line, instead of multiple places with a static tree arrangement.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read