Advertisement
Java_Volume1 Windows API Call/ Explanation #93322

Clean & Quick: EnumerateWindows

This code will enumerate all windows on the desktop including child windows, and children of children. There are several other entries here on PSC that perform a similar task, but they add functionality and interfaces that I didn't need... And so this is a more straightforward barebones approach; lighter & cleaner without worrying about treeviews or complicated code- A few API declarations, about a dozen lines of code and you're good to go.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
'Call this function to begin the process of getting every window on the desktop
Public Sub EnumerateAllWindows()
Dim hWndDesktop As Long
 hWndDesktop = GetDesktopWindow()
 EnumerateChildren hWndDesktop
End Sub
Private Sub EnumerateChildren(hWndParent As Long)
Dim hWndChild As Long
 
 'Get the first child of hWndParent
 hWndChild = GetWindow(hWndParent, GW_CHILD Or GW_HWNDFIRST)
 
 Do While hWndChild <> 0
  ' At this point, hWndChild contains a child window handle of hWndParent.
  ' You could use GetWindowText here, for instance, to retrieve the title of the window.
  Debug.Print hWndParent, hWndChild
  
  'Now get any children for hWndChild
  EnumerateChildren hWndChild
  
  'And move on to the next window
  hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
 Loop
 
End Sub
Original Comments (3)
Recovered from Wayback Machine