勾成VBE窗口的子类SysTreeView32 [英] Hook into a child class SysTreeView32 of VBE window

查看:355
本文介绍了勾成VBE窗口的子类SysTreeView32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新来的WINAPI调用虽然熟悉VBA。我所试图做的是勾VBE窗口的子类SysTreeView32(Project Explorer中的TreeView)。我想扩大/(虽然我preFER第一个选项或交替发送鼠标点击(mouse_event))通过修改注册表键折叠树视图元素。结果
我可以用这个code找到Excel的主窗口:结果

I am pretty new to the WinApi calls although familiar with VBA. What I am trying to do is to hook to a child class SysTreeView32 of VBE window (Project Explorer TreeView). I would like to expand/collapse the tree view elements by modifying the registry keys (or alternatively sending the mouse clicks (mouse_event) although I prefer the first option).
I can find the Excel Main Window by using this code:

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
              (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Sub Find_Window()
    Dim hWndExcel As Long
    hWndExcel = FindWindow("XLMAIN", Application.Caption)
    MsgBox hWndExcel
End Sub

使用窗口侦探的帮助下我可以访问的名称,特性,等等。子类。结果

结果但我不能工作了如何进入/启动(甚至退货的HWID)的 SysTreeView32 子类的折叠/展开元素(文件夹)。我不知道如何通过迭代的元素,但我以后会研究这个。这里的问题是访问 SysTreeView32 类。我怎样才能实现呢?结果
结果
当我尝试MSGBOX这个结果
FindWindow函数(wndclass_desked_gsk,Application.Caption)结果
或结果
FindWindow函数(SysTreeView32,Application.Caption)结果
一个 0 返回所以我明明做错了什么:/结果
感谢您的时间。
结果

With the help of Window Detective I can access the names, properties, etc. of the child classes.

But I cannot work it out how to access/activate(or even return the HWID of) the SysTreeView32 child class to collapse/expand elements(folders). I am not sure how to iterate over the elements yet, but I will research this afterwards. The problem here is accessing the SysTreeView32 class. How can I achieve it?

When I try to msgbox this
FindWindow("wndclass_desked_gsk", Application.Caption)
or
FindWindow("SysTreeView32", Application.Caption)
a 0 is returned so I am obviously doing something wrong :/
Thank you for your time.

推荐答案

你应该使用:

application.vbe.mainwindow.caption

下面是一些示例崩溃code

here's some sample collapse code

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

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)
   hNode = SendMessage(hWndTvw, TVM_GETNEXTITEM, TVGN_ROOT, 0&)
   Do While hNode <> 0
      varReturn = SendMessage(hWndTvw, TVM_EXPAND, TVE_COLLAPSE, hNode)
      hNode = SendMessage(hWndTvw, TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, hNode)
   Loop
End Sub

进一步您的评论,这里的code崩溃只有微软Excel对象节点

further to your comment, here's the code to collapse only the 'Microsoft Excel Objects' nodes

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
Public Declare Function SendMessageB Lib "user32" Alias "SendMessageA" _
                                     (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long

Private Const MAX_ITEM        As Long = 256
Private Const TV_FIRST        As Long = &H1100
Private Const TVM_EXPAND      As Long = (TV_FIRST + 2)
Private Const TVM_GETNEXTITEM As Long = (TV_FIRST + 10)
Private Const TVM_GETITEM     As Long = (TV_FIRST + 12)
Const TVE_COLLAPSE            As Long = &H1
Const TVE_EXPAND              As Long = &H2
Private Const TVGN_ROOT       As Long = &H0
Private Const TVGN_NEXT       As Long = &H1
Private Const TVIF_TEXT       As Long = &H1
Private Const TVGN_NEXTVISIBLE = &H6

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


Sub CollapseXLObjects()
   Dim hWndVBE                As Long
   Dim hWndPE                 As Long
   Dim hWndTvw                As Long
   Dim hNode                  As Long
   Dim tvi                    As TVITEM
   Dim nChild                 As Long
   Dim sText                  As String
   Dim varReturn

   hWndVBE = FindWindowEx(0, 0, "wndclass_desked_gsk", Application.VBE.MainWindow.Caption)
   hWndPE = FindWindowEx(hWndVBE, 0, "PROJECT", vbNullString)
   hWndTvw = FindWindowEx(hWndPE, 0, "SysTreeView32", vbNullString)

   hNode = SendMessage(hWndTvw, TVM_GETNEXTITEM, TVGN_ROOT, 0&)

   Do While hNode <> 0
      tvi.hItem = hNode
      tvi.mask = TVIF_TEXT
      tvi.cchTextMax = MAX_ITEM
      tvi.pszText = String(MAX_ITEM, 0)
      nChild = SendMessageB(hWndTvw, TVM_GETITEM, 0&, tvi)
      If InStr(1, tvi.pszText, "Microsoft Excel Objects", vbTextCompare) > 0 Then
         varReturn = SendMessage(hWndTvw, TVM_EXPAND, TVE_COLLAPSE, hNode)
      Else
         varReturn = SendMessage(hWndTvw, TVM_EXPAND, TVE_EXPAND, hNode)
      End If
      hNode = SendMessage(hWndTvw, TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, hNode)
   Loop
End Sub

这篇关于勾成VBE窗口的子类SysTreeView32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆