Dynamic Tree Loading

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.



Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds