来自:http://topic.csdn.net/t/20041008/11/3433163.html,感谢Tiger_Zhao提供链接。代码注释得很清晰,就不废话了,如下:
' If successful, returns the treeview item handle represented by ' the specified Node, returns 0 otherwise. Public Function GetTVItemFromNode(hwndTV As Long, nod As Node) As Long Dim nod1 As MSComctlLib.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 ' Retrieves the first child item. The hitem parameter must be NULL. ' Returns the handle to the item if successful or 0 otherwise. Private Function TreeView_GetChild(hWnd As Long, hItem As Long) As Long TreeView_GetChild = TreeView_GetNextItem(hWnd, hItem, TVGN_CHILD) End Function ' Retrieves the tree-view item that bears the specified relationship to a specified item. ' Returns the handle to the item if successful or 0 otherwise. Private 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 ' Retrieves the next sibling item. ' Returns the handle to the item if successful or 0 otherwise. Private Function TreeView_GetNextSibling(hWnd As Long, hItem As Long) As Long TreeView_GetNextSibling = TreeView_GetNextItem(hWnd, hItem, TVGN_NEXT) End Function ' Retrieves the topmost or very first item of the tree-view control. ' Returns the handle to the item if successful or 0 otherwise. Private Function TreeView_GetRoot(hWnd As Long) As Long TreeView_GetRoot = TreeView_GetNextItem(hWnd, 0, TVGN_ROOT) End Function ' Returns the one-base position of the specified node ' with respect to it's sibling order. Private 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 |