VB and VBA Users Source Code: Finding matching window handles using wildcards
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Finding matching window handles using wildcards
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, February 26, 2002
Hits:
956
Category:
Windows API
Article:
The following code finds an array of matching window handles using the Like comparison operation, by performing a recursive search. Option Explicit 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 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 GetParent Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function GetWindowWord Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Integer 'Purpose : Finds the window handles of the windows matching the specified ' parameters. 'Inputs : WindowText The pattern used with the Like operator to compare window's text. ' Classname The pattern used with the Like operator to compare window's class ' name. ' alMatchingHwnds Output array of longs, used to return the matching window handles. ' [lStartHwnd] The handle of the first window to start the search from. ' The routine searches through all of this window's children and their ' children recursively. If lStartHwnd = 0 then the routine searches ' through all windows. ' [lChildID] A child ID number used to identify a window. ' If -1 parameter is ignored. 'Outputs : The number of windows that matched the parameters. ' Also returns the window handles in the one based 1d array "alMatchingHwnds" 'Author : Andrew Baker (copyright www.vbusers.com) 'Date : 03/05/2001 13:50 'Notes : 'Revisions : Function FindWindowLike(WindowText As String, Classname As String, alMatchingHwnds() As Long, Optional ByVal lStartHwnd As Long = 0, Optional lChildID As Long = -1) As Integer Const GW_HWNDNEXT As Long = 2 Const GW_CHILD As Long = 5 Const GWW_ID As Long = (-12) Dim sWindowText As String, sClassname As String Dim lWinID As Long, lHwnd As Long Dim lRetVal As Long 'Hold the Level of recursion Static slLevel As Long 'Hold the number of matching windows Static slFound As Long 'Initialize if necessary If slLevel = 0 Then slFound = 0 ReDim alMatchingHwnds(1 To 1) If lStartHwnd = 0 Then lStartHwnd = GetDesktopWindow() End If End If 'Increase recursion counter slLevel = slLevel + 1 'Get first child window lHwnd = GetWindow(lStartHwnd, GW_CHILD) Do Until lHwnd = 0 'Search children by recursion lRetVal = FindWindowLike(WindowText, Classname, alMatchingHwnds(), lHwnd, lChildID) 'Get the window text and class name sWindowText = Space(255) lRetVal = GetWindowText(lHwnd, sWindowText, 255) sWindowText = Left$(sWindowText, lRetVal) sClassname = Space(255) lRetVal = GetClassName(lHwnd, sClassname, 255) sClassname = Left$(sClassname, lRetVal) 'If window is a child get the ID If GetParent(lHwnd) <> 0 Then lRetVal = GetWindowWord(lHwnd, GWW_ID) lWinID = CLng("&H" & Hex(lRetVal)) Else lWinID = -1 End If 'Check that window matches the search parameters If (sWindowText Like WindowText Or WindowText = "") And (sClassname Like Classname Or Classname = "") Then If lChildID = -1 Then 'Found a match, increment counter and add handle to array slFound = slFound + 1 ReDim Preserve alMatchingHwnds(1 To slFound) alMatchingHwnds(slFound) = lHwnd ElseIf lWinID <> -1 Then If lWinID = lChildID Then 'Found a match, increment counter and add handle to array slFound = slFound + 1 ReDim Preserve alMatchingHwnds(1 To slFound) alMatchingHwnds(slFound) = lHwnd End If End If End If 'Get next child window lHwnd = GetWindow(lHwnd, GW_HWNDNEXT) Loop 'Decrement recursion counter slLevel = slLevel - 1 'Return the number of windows found FindWindowLike = slFound End Function 'Demonstration routine Sub Test() Dim alHwnds() As Long, count As Long, vHwnd As Variant count = FindWindowLike("Microsoft*", vbNullString, alHwnds) For Each vHwnd In alHwnds Debug.Print "Found matching window " & vHwnd Next End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder