由TreeView Node得到对应的HItem

18 篇文章 0 订阅
9 篇文章 0 订阅

来自: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 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值