3

这是我在挂钩到 VBE 窗口的子类 SysTreeView32 之后的

下一个问题, 我现在可以访问 SysTreeView32,但我无法访问 hNode 的子节点。在过去的 2 个小时里,我尝试了很多变体并一直在阅读它,但我无法解决。这甚至可能吗?由于窗口尺寸和位置不同,我真的想避免 mouse_event 和单击,但如果这是唯一的方法,那么我将尝试实现它。

继承人的代码:

Option Explicit

Private Const TVE_COLLAPSE = &H1
Private Const TVE_COLLAPSERESET = &H8000
Private Const TVE_EXPAND = &H2
Private Const TVE_EXPANDPARTIAL = &H4000
Private Const TVE_TOGGLE = &H3
Private Const TV_FIRST = &H1100
Private Const TVM_EXPAND = (TV_FIRST + 2)
Private Const TVM_GETNEXTITEM = (TV_FIRST + 10)
Private Const TVGN_ROOT = &H0
Private Const TVGN_NEXTVISIBLE = &H6
Private Const TVGN_CHILD = 4

Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
                              (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                                    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Sub CollapseProjects()
   Dim hWndVBE As Long, hWndPE As Long, hWndTvw As Long, hNode As Long, varReturn
   hWndVBE = FindWindowEx(0, 0, "wndclass_desked_gsk", Application.VBE.MainWindow.Caption)
   hWndPE = FindWindowEx(hWndVBE, 0, "PROJECT", vbNullString)
   hWndTvw = FindWindowEx(hWndPE, 0, "SysTreeView32", vbNullString)

   Dim childNode As Long
   hNode = SendMessage(hWndTvw, TVM_GETNEXTITEM, TVGN_ROOT, 0&)
   childNode = SendMessage(hNode, TVM_GETNEXTITEM, 0&, 0&)
   Debug.Print "childNode " & childNode

   Do While hNode <> 0
      Debug.Print hNode
      varReturn = SendMessage(hWndTvw, TVM_EXPAND, TVE_COLLAPSE, hNode)
      hNode = SendMessage(hWndTvw, TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, hNode)
   Loop
End Sub

为什么

childNode = SendMessage(hNode, TVM_GETNEXTITEM, 0&, 0&)
Debug.Print "childNode " & childNode

它总是返回0?

4

1 回答 1

1

这:

childNode = SendMessage(hNode, TVM_GETNEXTITEM, 0&, 0&)

不要求子节点。首先,您将消息发送到hNode,而不是树控件,这根本没有意义。然后,要获取子节点,需要TVGN_CHILDwParam. 您还需要传递您想要 in 的子项的项目lParam

所以它可能看起来像这样:

childNode = SendMessage(hWndTvw, TVM_GETNEXTITEM, TVGN_CHILD, hNode)

有关更多信息,请参阅消息的文档TVM_GETNEXTITEM

于 2013-06-11T13:24:39.063 回答