FillWindowsTree – Fill a treeview with the windows hierarchy

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 LongPrivate Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal _    hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As LongPrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _    hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _    lParam As Any) As LongPrivate Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _    ByVal wCmd As Long) As LongPrivate Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As LongPrivate Declare Function GetDesktopWindow Lib "user32" () As Long'API constantsConst WM_GETTEXT = &HDConst WM_GETTEXTLENGTH = &HEConst GWL_HINSTANCE = (-6)Const GWL_HWNDPARENT = (-8)Const GWL_STYLE = (-16)Const GWL_EXSTYLE = (-20)Const GW_CHILD = 5Const GW_HWNDFIRST = 0Const GW_HWNDLAST = 1Const GW_HWNDNEXT = 2Const GW_HWNDPREV = 3Const GW_MAX = 5Const 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 TreeView1Function 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)    LoopEnd Function' return the caption of a windowFunction 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 windowFunction 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


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist