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
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
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
If hWnd = 0 Then tvw.Nodes.Clear hWnd = GetDesktopWindow() End If sText = CStr(hwnd) & " """ & GetWindowCaption(hwnd) & """ " & _ GetWindowClass(hwnd)
Set nNode = tvw.Nodes("H" & GetParent(hwnd)) If nNode Is Nothing Then Set nNode = tvw.Nodes.Add(, , "H" & CStr(hwnd), sText) Else Set nNode = tvw.Nodes.Add(nNode, tvwChild, "H" & CStr(hwnd), sText) End If nNode.Tag = GetWindowCaption(hwnd) lChild = GetWindow(hwnd, GW_CHILD) Do While lChild <> 0 FillWindowsTree tvw, lChild lChild = GetWindow(lChild, GW_HWNDNEXT) Loop End Function
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
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 |