by Sephectja (1 Submission)
Category: Windows System Services
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sun 1st August 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Searches all the children of a specified hWnd and returns the matches
API Declarations
'Use this code in your own programs if you need to but please
'leave the first 6 lines of commentation in the General Declarations
' This is released under the GPL Licence which means you can modify, change
' and do what you want with the code under the condition that all credits must
' stay and be left untouched. They API calls are from the Microsoft Windows API
'
' A copy of the GPL is at http://www.gnu.org/copyleft/gpl.html
'
'Use of FindChildHwnd function:
' FindChildHwnd(RootHwnd, Caption, Classname, TargetArray())
' RootHwnd = The hWnd you want to start searching from
' Caption = The caption value of the hWnd value (wildcards are allowed)
' Classname = The classname value of the hWnd (Wildcards are allowed)
' TargetArray = The array that all the hWnd values are returned to (this is done for speed)
'
'Use of GetClass function:
' GetClass(hwnd)
' hwnd = the hWnd Identifier you want the classname from
'
'Use of GetCaption function
' GetCaption(hwnd)
' hwnd = the hWnd Identifier you want the caption from
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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
' GetWindow() Constants
Const GW_HWNDFIRST = 0
Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2
Const GW_HWNDPREV = 3
Const GW_OWNER = 4
Const GW_CHILD = 5
Const GW_MAX = 5
Public Function GetClass(ByVal hWnd As Long) As String
Dim CurWindowClassname As String
CurWindowClassname = String(255, " ")
Result = GetClassName(hWnd, CurWindowClassname, 255)
GetClass = Replace(RTrim(CurWindowClassname), Chr(0), "")
End Function
Public Function GetCaption(ByVal hWnd As Long) As String
Dim CurWindowCaption As String
Dim CurWindowCaptionLen As Long
CurWindowCaptionLen = GetWindowTextLength(hWnd) + 1 'Get how big our buffer needs to be
CurWindowCaption = String(CurWindowCaptionLen, " ") 'Create our buffer
Result = GetWindowText(hWnd, CurWindowCaption, CurWindowCaptionLen) 'CurWindowCaption = Varible to send to
GetCaption = Replace(RTrim(CurWindowCaption), Chr(0), "") 'Cut off any extra spaces and remove chr 0 (Null)
End Function
Public Function FindChildHwnd(ByVal RootHwnd As Long, ByVal Caption As String, ByVal Classname As String, ByRef TargetArray() As Long) As Long
'Declare varibles
Dim curHwnd, lngDepth, Finds As Long
Dim lvlHwnds() As Long
Dim FindResults() As Long
lngDepth = 1 'Depth 0 = RootHwnd (When we get back to this we've looped through all the hwnds!
ReDim lvlHwnds(0 To lngDepth) As Long 'So we clear the varible and set boundries
lvlHwnds(0) = RootHwnd 'The first main hwnd is the RootHwnd
Finds = 0 'Reset the Finds counter
curHwnd = RootHwnd 'Show the Hwnd we are working on at the moment
'We now need to loop through until we get back to the RootHwnd
While lngDepth > 0
If curHwnd > 0 Then 'We need to check the hwnd value to see if the window we are looking for exists
lngDepth = lngDepth + 1 'and if it does we go to the next level
If lngDepth > UBound(lvlHwnds) Then 'Since we are going to the next level we need to re-define this array
ReDim Preserve lvlHwnds(0 To lngDepth) As Long 'for speed we are only going to redefine the array
End If
lvlHwnds(lngDepth) = GetWindow(curHwnd, GW_CHILD) 'Get the the first child window of the next level
curHwnd = lvlHwnds(lngDepth) 'Set the current hwnd to the current depth of windows
End If
If curHwnd = 0 Then
curHwnd = lvlHwnds(lngDepth - 1) 'We need to go up one level since the last level doesnt exist
If curHwnd = RootHwnd Then 'When we get back to the the starting hwnd (rootHwnd)
GoTo skipLoop 'We need to finish the sub
End If
lngDepth = lngDepth - 1 'Reduce the depth by one
lvlHwnds(lngDepth) = GetWindow(curHwnd, GW_HWNDNEXT) 'Get the next window in the list
curHwnd = lvlHwnds(lngDepth) 'Change the current hwnd
End If
If curHwnd > 0 Then 'We can only have a match if the hwnd is more than 0
If GetClass(curHwnd) Like Classname And GetCaption(curHwnd) Like Caption Then 'See if our Caption and Classname we chose when calling the function matches with wildcards
Finds = Finds + 1 'Increase the finds by 1
ReDim Preserve FindResults(0 To Finds - 1) As Long
FindResults(Finds - 1) = curHwnd
End If
End If
Wend
skipLoop:
FindChildHwnd = Finds 'So we return the amount of finds that we where looking for
ReDim Preserve TargetArray(0 To UBound(FindResults)) As Long
TargetArray = FindResults 'We need to set the target array (as an array) so people can access our results
Exit Function 'and exit the function before doing any other errors
errhand:
Exit Function
End Function
No comments have been posted about Searches all the children of a specified hWnd and returns the matches. Why not be the first to post a comment about Searches all the children of a specified hWnd and returns the matches.