📄 mod_treeview.bas
字号:
End If
End Function
' If successful, returns the very first node in the specified treeview,
' returns Nothing otherwise.
' Equates to retrieving the hItem of the treeview's first item with
' TVM_GETNEXTITEM/TVGN_ROOT. (Treeview1.Nodes(1)
' or Treeview1.Nodes(1).Root do *not* necessarily return the
' treeview's very first node!)
Public Function GetFirstTVNode(objTV As TreeView) As Node
Dim nod As Node
On Error GoTo NoNodes
' Will err here if there are no treeview nodes
Set nod = objTV.Nodes(1)
' Get the first node's highest parent node
Do While (nod.Parent Is Nothing) = False
Set nod = nod.Parent
Loop
' Return the highest parent node's first sibling
Set GetFirstTVNode = nod.FirstSibling
NoNodes:
End Function
' 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 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 position 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
' Returns the one-base position of the specified node
' with respect to it's sibling order.
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
' ===========================================================================
' Treeview macros defined in Commctrl.h
' Determines the location of the specified point relative to the client area of a tree-view control.
' Returns the handle to the tree-view item that occupies the specified point or NULL if no item
' occupies the point.
Public Function TreeView_HitTest(hWnd As Long, lpht As TVHITTESTINFO) As Long
TreeView_HitTest = SendMessage(hWnd, TVM_HITTEST, ByVal 0, lpht)
End Function
' Retrieves some or all of a tree-view item's attributes.
' Returns TRUE if successful or FALSE otherwise.
Public Function TreeView_GetItem(hWnd As Long, pitem As TVITEM) As Boolean
TreeView_GetItem = SendMessage(hWnd, TVM_GETITEM, 0, pitem)
End Function
' Sets some or all of a tree-view item's attributes.
' Old docs say returns zero if successful or - 1 otherwise.
' New docs say returns TRUE if successful, or FALSE otherwise
Public Function TreeView_SetItem(hWnd As Long, pitem As TVITEM) As Boolean
TreeView_SetItem = SendMessage(hWnd, TVM_SETITEM, 0, pitem)
End Function
' Prepares the index of a state image so that a tree view control or list
' view control can use the index to retrieve the state image for an item.
' Rtns the one-based index of the state image shifted left twelve bits.
' A common control utility macro.
Public Function INDEXTOSTATEIMAGEMASK(iState As Long) As Long
' #define INDEXTOSTATEIMAGEMASK(i) ((i) << 12)
INDEXTOSTATEIMAGEMASK = iState * (2 ^ 12)
End Function
' TreeView_GetNextItem
' 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.
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
' Retrieves the first child item. The hItem parameter must be NULL.
' Returns the handle to the item if successful or 0 otherwise.
Public Function TreeView_GetChild(hWnd As Long, hItem As Long) As Long
TreeView_GetChild = TreeView_GetNextItem(hWnd, hItem, TVGN_CHILD)
End Function
' Retrieves the next sibling item.
' Returns the handle to the item if successful or 0 otherwise.
Public Function TreeView_GetNextSibling(hWnd As Long, hItem As Long) As Long
TreeView_GetNextSibling = TreeView_GetNextItem(hWnd, hItem, TVGN_NEXT)
End Function
' Retrieves the previous sibling item.
' Returns the handle to the item if successful or 0 otherwise.
Public Function TreeView_GetPrevSibling(hWnd As Long, hItem As Long) As Long
TreeView_GetPrevSibling = TreeView_GetNextItem(hWnd, hItem, TVGN_PREVIOUS)
End Function
' Retrieves the parent of the specified item.
' Returns the handle to the item if successful or 0 otherwise.
Public Function TreeView_GetParent(hWnd As Long, hItem As Long) As Long
TreeView_GetParent = TreeView_GetNextItem(hWnd, hItem, TVGN_PARENT)
End Function
' Retrieves the first visible item.
' Returns the handle to the item if successful or 0 otherwise.
Public Function TreeView_GetFirstVisible(hWnd As Long) As Long
TreeView_GetFirstVisible = TreeView_GetNextItem(hWnd, 0, TVGN_FIRSTVISIBLE)
End Function
' Retrieves the next visible item that follows the specified item. The specified item must be visible.
' Use the TVM_GETITEMRECT message to determine whether an item is visible.
' Returns the handle to the item if successful or 0 otherwise.
Public Function TreeView_GetNextVisible(hWnd As Long, hItem As Long) As Long
TreeView_GetNextVisible = TreeView_GetNextItem(hWnd, hItem, TVGN_NEXTVISIBLE)
End Function
' Retrieves the first visible item that precedes the specified item. The specified item must be visible.
' Use the TVM_GETITEMRECT message to determine whether an item is visible.
' Returns the handle to the item if successful or 0 otherwise.
Public Function TreeView_GetPrevVisible(hWnd As Long, hItem As Long) As Long
TreeView_GetPrevVisible = TreeView_GetNextItem(hWnd, hItem, TVGN_PREVIOUSVISIBLE)
End Function
' Retrieves the currently selected item.
' Returns the handle to the item if successful or 0 otherwise.
Public Function TreeView_GetSelection(hWnd As Long) As Long
TreeView_GetSelection = TreeView_GetNextItem(hWnd, 0, TVGN_CARET)
End Function
' Retrieves the item that is the target of a drag-and-drop operation.
' Returns the handle to the item if successful or 0 otherwise.
Public Function TreeView_GetDropHilight(hWnd As Long) As Long
TreeView_GetDropHilight = TreeView_GetNextItem(hWnd, 0, TVGN_DROPHILITE)
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.
Public Function TreeView_GetRoot(hWnd As Long) As Long
TreeView_GetRoot = TreeView_GetNextItem(hWnd, 0, TVGN_ROOT)
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -