Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
May 19, 2001

FillWindowsTree - Fill a treeview with the windows hierarchy

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal _
    hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _
    hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
    lParam As Any) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
    ByVal wCmd As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long

'API constants
Const WM_GETTEXT = &HD
Const WM_GETTEXTLENGTH = &HE
Const GWL_HINSTANCE = (-6)
Const GWL_HWNDPARENT = (-8)
Const GWL_STYLE = (-16)
Const GWL_EXSTYLE = (-20)

Const GW_CHILD = 5
Const GW_HWNDFIRST = 0
Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2
Const GW_HWNDPREV = 3
Const GW_MAX = 5
Const GW_OWNER = 4


' Fill a treeview with the complete windows hierarchy
' The text of each node will be in the format: handle caption class_name
' The tag of each node is the window's caption, useful if you want
' to perform a simple search through the windows loaded
'
' The first parameter is the TreeView to fill, the second is the handle
' of the window to start with (or zero for the desktop window)
'
' Example: fill the treeview control with ALL the windows
'   starting with the desktop window
'   FillWindowsTree TreeView1

Function FillWindowsTree(tvw As TreeView, Optional ByVal hwnd As Long) As Long
    Dim nNode As Node
    Dim lChild As Long
    Dim sText As String
    
    On Error Resume Next

    ' provide a default for hWnd argument
    If hWnd = 0 Then 
        tvw.Nodes.Clear
        hWnd = GetDesktopWindow()
    End If
    
    ' build the node's text: hwnd "caption" class
    sText = CStr(hwnd) & " """ & GetWindowCaption(hwnd) & """ " & _
        GetWindowClass(hwnd)

    ' look if the treeview has a node for the parent window
    Set nNode = tvw.Nodes("H" & GetParent(hwnd))
    ' if not, add a root node
    If nNode Is Nothing Then
        Set nNode = tvw.Nodes.Add(, , "H" & CStr(hwnd), sText)
    Else
        ' if so, add this node as child of that node
        Set nNode = tvw.Nodes.Add(nNode, tvwChild, "H" & CStr(hwnd), sText)
    End If
    nNode.Tag = GetWindowCaption(hwnd)
    
    ' find the first child window
    lChild = GetWindow(hwnd, GW_CHILD)
    Do While lChild <> 0
        ' call recursively this function
        FillWindowsTree tvw, lChild
        ' until there are no more child windows
        lChild = GetWindow(lChild, GW_HWNDNEXT)
    Loop
End Function


' return the caption of a window
Function GetWindowCaption(ByVal hwnd As Long) As String
    Dim length As Long
    Dim text As String
    
    length = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, ByVal 0&)
    text = Space$(length + 1)
    SendMessage hwnd, WM_GETTEXT, length + 1, ByVal text
    GetWindowCaption = Left$(text, length)
End Function


' return the class name of the specified window
Function GetWindowClass(ByVal hwnd As Long) As String
    Dim sClass As String

    sClass = Space$(256)
    GetClassName hwnd, sClass, 255
    GetWindowClass = Left$(sClass, InStr(sClass, vbNullChar) - 1)
End Function
Marco Bellinaso
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date