Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

FillWindowsTree - Fill a treeview with the windows hierarchy
[ All Languages » VB »  Windows]

Total Hit ( 2670)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
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


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.