⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mod_treeview.bas

📁 ArcEngine 这是基于AE组件的源代码
💻 BAS
📖 第 1 页 / 共 2 页
字号:
Attribute VB_Name = "Mod_TreeView"
Option Explicit
'
' Brad Martinez, http://www.mvps.org/ccrp/
'

' The are the indices of the treeview's checkbox state images
' when the treeview's TVS_CHECKBOXES style bit is set.
Public Const IIL_UNCHECKED = 1
Public Const IIL_CHECKED = 2

Public Const GWL_STYLE = (-16)

Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                            (ByVal hWnd As Long, _
                            ByVal wMsg As Long, _
                            wParam As Any, _
                            lParam As Any) As Long   ' <---

Public Type POINTAPI   ' pt
  x As Long
  y As Long
End Type

Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

' ===========================================================================
' treeview definitions defined in Commctrl.h at:
' http://premium.microsoft.com/msdn/library/sdkdoc/c67_4c8m.htm

' style
Public Const TVS_CHECKBOXES = &H100   ' >= IE3

Public Type TVITEM   ' was TV_ITEM
  mask As Long
  hItem As Long
  state As Long
  stateMask As Long
  pszText As String   ' Long   ' pointer
  cchTextMax As Long
  iImage As Long
  iSelectedImage As Long
  cChildren As Long
  lParam As Long
End Type

' TVITEM.mask flags
Public Const TVIF_TEXT = &H1
Public Const TVIF_STATE = &H8
Public Const TVIF_HANDLE = &H10

' TVITEM.state bit value
Public Const TVIS_STATEIMAGEMASK = &HF000

Public Type TVHITTESTINFO   ' was TV_HITTESTINFO
  pt As POINTAPI
  flags As Long
  hItem As Long
End Type

' TVHITTESTINFO.flags value
Public Const TVHT_ONITEMSTATEICON = &H40

' User-defined as the maximum treeview item text length.
' If an items text exceeds this value when calling GetTVItemText
' there could be problems...
Public Const MAX_ITEM = 256

' messages
Public Const TV_FIRST = &H1100
Public Const TVM_GETNEXTITEM = (TV_FIRST + 10)
Public Const TVM_GETITEM = (TV_FIRST + 12)
Public Const TVM_SETITEM = (TV_FIRST + 13)
Public Const TVM_HITTEST = (TV_FIRST + 17)

' TVM_GETNEXTITEM wParam values
Public Enum TVGN_Flags
    TVGN_ROOT = &H0
    TVGN_NEXT = &H1
    TVGN_PREVIOUS = &H2
    TVGN_PARENT = &H3
    TVGN_CHILD = &H4
    TVGN_FIRSTVISIBLE = &H5
    TVGN_NEXTVISIBLE = &H6
    TVGN_PREVIOUSVISIBLE = &H7
    TVGN_DROPHILITE = &H8
    TVGN_CARET = &H9
#If (WIN32_IE >= &H400) Then   ' >= Comctl32.dll v4.71
    TVGN_LASTVISIBLE = &HA
#End If
End Enum
'

' Determines if the current state image of the specified treeview
' item is set to the checked checkbox image index.

'   hwndTV      - treeview's window handle
'   hItem          - item's handle whose checkbox state is to be to returned

' Return True if the item's state image is set to the
' checked checkbox index, returns False otherwise.

Public Function IsTVItemChecked(hwndTV As Long, _
                                                       hItem As Long) As Boolean
  Dim tvi As TVITEM

  ' Initialize the struct and get the item's state value.
  ' (TVIF_HANDLE does not need to be specified, it's use is implied...)
  tvi.mask = TVIF_HANDLE Or TVIF_STATE
  tvi.hItem = hItem
  tvi.stateMask = TVIS_STATEIMAGEMASK
  Call TreeView_GetItem(hwndTV, tvi)

'Debug.Print "&H" & Hex(tvi.state)

  ' We have to test to see if the treeview's checked state image *is*
  ' set since the logical And test on the unchecked image (1) will
  ' evaluate to True when either checkbox image is set...
  IsTVItemChecked = (tvi.state And INDEXTOSTATEIMAGEMASK(IIL_CHECKED))
  
End Function

' Determines if the current state image of the item under the specified
' point (if any) is set to the checked checkbox image index.

'   hwndTV      - treeview's window handle
'   x, y             - treeview coordinates in which to retrieve the item from

' Return True if the item's state image is set to the
' checked checkbox index, returns False otherwise.

Public Function IsTVItemCheckedFromClick(hwndTV As Long, _
                                                                      x As Long, _
                                                                      y As Long) As Boolean
  Dim tvhti As TVHITTESTINFO
  Dim fChecked As Boolean
    
  tvhti.pt.x = x
  tvhti.pt.y = y
  If TreeView_HitTest(hwndTV, tvhti) Then   ' rtns an hItem also
    
    fChecked = IsTVItemChecked(hwndTV, tvhti.hItem)
    
    ' Since we retrieved the item's handle from a treeview coordinate as a
    ' result of a NodeClick event (or MouseUp event, both are invoked from
    ' an NM_CLICK notification), if this coordinate is within the area of the
    ' item's state icon, then the item's checkbox image is *in the process* of
    ' being toggled, but *not yet* toggled. So we'll toggle the return value
    ' reflecting the soon to be actual state value.
    If (tvhti.flags And TVHT_ONITEMSTATEICON) Then fChecked = Not fChecked
    
    IsTVItemCheckedFromClick = fChecked
  
  End If
  
End Function

' Set the specified checkbox state for the specified item.
' Returns True if successful, returns False otherwise.

'   hwndTV      - treeview's window handle
'   hItem          - item's handle whose checkbox state is to be to set
'   fCheck       - If True, sets the checkbox state to the checked image,
'                       if False, sets the unchecked image.

Public Function SetTVItemCheckImage(hwndTV As Long, _
                                                               hItem As Long, _
                                                               fCheck As Boolean) As Boolean
  Dim tvi As TVITEM
  
  tvi.mask = TVIF_HANDLE Or TVIF_STATE
  tvi.hItem = hItem
  tvi.stateMask = TVIS_STATEIMAGEMASK
  
  If fCheck Then
    tvi.state = INDEXTOSTATEIMAGEMASK(IIL_CHECKED)
  Else
    tvi.state = INDEXTOSTATEIMAGEMASK(IIL_UNCHECKED)
  End If
  
  SetTVItemCheckImage = TreeView_SetItem(hwndTV, tvi)
  
End Function

' Returns the text of the specified treeview item if successful,
' returns an empty string otherwise.

'   hwndTV      - treeview's window handle
'   hItem          - item's handle whose text is to be to returned
'   cbItem        - length of the specified item's text.

Public Function GetTVItemText(hwndTV As Long, _
                                                  hItem As Long, _
                                                  Optional cbItem As Long = MAX_ITEM) As String
  Dim tvi As TVITEM
  tvi.mask = TVIF_TEXT
  tvi.hItem = hItem
  tvi.pszText = String$(cbItem, 0)
  tvi.cchTextMax = cbItem
  If TreeView_GetItem(hwndTV, tvi) Then
    GetTVItemText = GetStrFromBufferA(tvi.pszText)
  End If
End Function

' Returns the string before first null char encountered (if any) from an ANSII string.

Public Function GetStrFromBufferA(sz As String) As String
  If InStr(sz, vbNullChar) Then
    GetStrFromBufferA = Left$(sz, InStr(sz, vbNullChar) - 1)
  Else
    ' If sz had no null char, the Left$ function
    ' above would return a zero length string ("").
    GetStrFromBufferA = sz

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -