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: May 18, 2015 While the idea of using facial and/or gesture recognition to create a modern, intuitive game seems attractive, some developers may want to leverage Unity 3D as a way to accelerate their development efforts. There are many different ways in which Intel and Unity Technologies have been working together to help speed the development of games with the Intel&reag; RealSense™ SDK (software developer kit). Check out this webcast to join a panel of experts as they …

  • Employees must exchange sensitive emails with customers and partners. These emails might contain protected health information, protected financial information, or corporate information that should not be made public. Globalscape® Mail Express® allows you to encrypt the emails that it manages so that no one but the sender and recipient--not even the administrator--can view the contents of the email. "Secure the Transfer of Sensitive Emails" is the property of GlobalSCAPE

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date