# Re: How to attach the system image list handle to a tree view control ?

Posted by Sun Jack on September 21, 1999 at 23:41:29:

In Reply to: Re: How to attach the system image list handle to a tree view control ? posted by 吴海文 on September 20, 1999 at 09:01:21:

: 对不起！麻烦先进再次解说GetTVItemFromNode，谢谢！

Public Function GetTVItemFromNode(hwndTV As Long, _
nod As Node) As Long
Dim nod1 As Node
Dim anSiblingPos() As Integer ' contains the sibling position of the Node and all it's parents
Dim nLevel As Integer ' hierarchical level of the Node
Dim hItem As Long
Dim i As Integer
Dim nPos As Integer

Set nod1 = nod

' Continually work backwards from the current Node to the current Node's
' first sibling, caching the current Node's sibling position in the one-based
' array. Then get the first sibling's parent Node and start over. Keep going
' until the postion of the specified Node's top level parent item is obtained...
Do While (nod1 Is Nothing) = False
nLevel = nLevel + 1
ReDim Preserve anSiblingPos(nLevel)
anSiblingPos(nLevel) = GetNodeSiblingPos(nod1)
Set nod1 = nod1.Parent
Loop

' Get the hItem of the first item in the treeview
hItem = TreeView_GetRoot(hwndTV)
If hItem Then

' Now work backwards through the cached Node positions in the array
' (from the first treeview Node to the specified Node), obtaining the respective
' item handle for each Node at the cached position. When we get to the
' specified Node's position (the value of the first element in the array), we
' got it's hItem...
For i = nLevel To 1 Step -1
nPos = anSiblingPos(i)
Do While nPos > 1
hItem = TreeView_GetNextSibling(hwndTV, hItem)
nPos = nPos - 1
Loop
If (i > 1) Then hItem = TreeView_GetChild(hwndTV, hItem)
Next

GetTVItemFromNode = hItem

End If ' hItem

End Function

Public Function TreeView_GetChild(hWnd As Long, hItem As Long) As Long
TreeView_GetChild = TreeView_GetNextItem(hWnd, hItem, TVGN_CHILD)
End Function

Public Function TreeView_GetNextItem(hWnd As Long, hItem As Long, flag As Long) As Long
TreeView_GetNextItem = SendMessage(hWnd, TVM_GETNEXTITEM, ByVal flag, ByVal hItem)
End Function

Public Function TreeView_GetNextSibling(hWnd As Long, hItem As Long) As Long
TreeView_GetNextSibling = TreeView_GetNextItem(hWnd, hItem, TVGN_NEXT)
End Function

Public Function TreeView_GetRoot(hWnd As Long) As Long
TreeView_GetRoot = TreeView_GetNextItem(hWnd, 0, TVGN_ROOT)
End Function

Public Function GetNodeSiblingPos(nod As Node) As Integer
Dim nod1 As Node
Dim nPos As Integer
Set nod1 = nod
' Keep counting up from one until the Node has no more previous siblings
Do While (nod1 Is Nothing) = False
nPos = nPos + 1
Set nod1 = nod1.Previous
Loop
GetNodeSiblingPos = nPos
End Function

Follow Ups: