VC 由已知的树结构生成TreeView
来源:百度知道 编辑:UC知道 时间:2024/06/14 20:18:58
struct Person
{
string name;
string fatherName;
string sex;
string birthday;
Person *father;
Person *next;
Person *child;
};
如何使用treeview控件由生成已知的树结构。
使用递归调用,可以实现,代码很简单。
(缺点,执行速度慢,耗费系统资源——耗堆栈)。
假设 定义了控件变量 CTreeCtrl*m_tree;
( 可以通过TreeView控件来得到其CTreeCtrl指针,具体看MSDN)。
void EnumTree( Person*thePerson, HTREEITEM hParent, HTREEITEM hPrev)
{
if ( ! thePerson)
return;
HTREEITEM hNewItem = m_tree->Insert( thePerson->name, hParent, hPrev);
EnumTree( thePerson->next, hParent, hNewItem);
EnumTree( thePerson->child, hNewItem, NULL);
}
调用的时候:
EnumTree( m_rootPerson, NULL, NULL);// m_rootPerson 为树的根节点
简单吧....
(编程,就这么简单)
这个结构体有点问题,
child可能不止一个,而next不必要吧。
改成这样如何:
struct Person
{
string name;
string fatherName;
string sex;
string birthday;
Person *father;
Person *child;
int nr_child; //表示child指向的地包含多少个子节点
};
遍历用EnumTree就行了,MSDN上都有。
使用递归调用,可以实现,代码很简单。
(缺点,执行速度慢,耗费系统资源——耗堆栈)。
假设 定义了控件变量 CTreeCtrl*m_tree;
( 可以通过Tree