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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Due to internal controls and regulations, the amount of long term archival data is increasing every year. Since magnetic tape does not need to be periodically operated or connected to a power source, there will be no data loss because of performance degradation due to the drive actuator. Read this white paper to learn about a series of tests that determined magnetic tape is a reliable long-term storage solution for up to 30 years.

Most Popular Programming Stories

More for Developers

RSS Feeds