VB icon

Clean & Quick: EnumerateWindows

Email
Submitted on: 1/10/2015 1:22:00 PM
By: Matthew Heydman (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 627
 
     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.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :Clean & Quick: EnumerateWindows
'**************************************
'API Declarations
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Const GW_CHILD = 5
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Clean & Quick: EnumerateWindows
' Description: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.
' By: Matthew Heydman (from psc cd)
'
' Assumes:Just paste the code into a .bas module and call the EnumerateAllWindows function. In the EnumerateChildren function there is a place where you can do whatever you need to do with each window handle (right now the function prints to the immediate window for demo purposes).
'**************************************

'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


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.