//f_tree_browse_resursive //递归函数
// tv_1 treeview param1
// in_tvnode long param2
Integer li_Ret
Long ll_tvTmp, ll_tvCurrent, ll_tvRoot
TreeViewItem l_tv_item
法一:
ll_tvRoot=tv_1.FindItem(RootTreeItem!,0)
ll_tvCurrent=in_tvNode
li_Ret=tv_1.GetItem(ll_tvCurrent,l_tv_item)
If li_Ret = -1 Then
MessageBox(“信息”,”树的遍历结束!”) //遍历结束
Return
Else
MessageBox(“信息”,”当前遍历项为:”+l_tv_item.Label) //处理遍历当前项目
End If
If l_tv_item.Children=True Then
ll_tvTmp=tv_1.FindItem(ChildTreeItem!,ll_tvCurrent) //当前项存在叶子
,遍历其叶子
f_tree_browse_recursive(tv_1,ll_tvTmp)
Else
ll_tvTmp=tv_1.FindItem(NextTreeItem!,ll_tvCurrent) //当前项没有叶子
,取得其下一项
If ll_tvTmp <> -1 Then
f_tree_browse_recursive(tv_1,ll_tvTmp) //下一项存在,遍历其
下一项
Else
Do While True //下一项不存在,查找其
父项的下一项
ll_tvCurrent=tv_1.FindItem(ParentTreeItem!,ll_tvCurrent)
ll_tvTmp=tv_1.FindItem(NextTreeItem!,ll_tvCurrent)
//MessageBox(“Debug: “, “ll_tvTmp: “+String(ll_tvTmp)+ &
// “ll_tvRoot: “+String(ll_tvRoot) )
If ll_tvTmp <> -1 Then Exit //找
到了父项的下一项
If ll_tvCurrent = ll_tvRoot And ll_tvTmp=-1 Then Exit //找到
树根
Loop
f_tree_browse_recursive(tv_1,ll_tvTmp) //遍历其查找其父项
的下一项
End If
End If
//调用f_tree_browse_recursive
// tv_1 treeview param1
Long ll_tvRoot
ll_tvRoot=tv_1.FindItem(RootTreeItem!,0) //取得树根
f_tree_browse_recursive(tv_1,ll_tvRoot) //执行数的遍历递归调用
注:本函数只对树的遍历即非森林的treeview有效.
法二:
函数TreeList(long handle)
long ll_Handle
Treeviewitem ltvi_Item
ll_Handle = Tv_Tree.finditem(ChildTreeItem!,handle)
do while ll_Handle <> -1
Tv_Tree.Getitem(ll_Handle,ltvi_Item)
//根据ltvi_Item做相应的操作
ltvi_Item即取出的节点,
ltvi_Item.statepictureindex 节点状态,一般用来区分复选框
ltvi_Item.label 节点标题
ltvi_Item.data 节点创建时存放的数据
TreeList(ll_Handle)
ll_Handle = Tv_Tree.finditem(NextTreeItem!,ll_handle)
loop
调用TreeList(0)即可完成整颗树的遍历