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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds