|
|
Title | Find the text displayed by a Window's descendant given its hierarchical window path |
Description | This example shows how to find the text displayed by a Window's descendant given its hierarchical window path in Visual Basic 6. |
Keywords | window, hWnd, FindWindow, class, window class, path, window path |
Categories | API |
|
|
Enter a target string and a window class path. The program searches for a window with title containing the target string. It then searches the window's child hierarchy to find a window that matches the specified class path. It displays the text contained by that child window.
The program uses the EnumWindows API function to look for a window with title containing the target text. When it finds one, it calls FollowChildPath to get the desired child window's text.
|
|
Public Function EnumProc(ByVal app_hwnd As Long, ByVal _
lParam As Long) As Boolean
Dim buf As String * 1024
Dim title As String
Dim length As Long
Dim txt As String
' Get the window's title.
length = GetWindowText(app_hwnd, buf, Len(buf))
title = Left$(buf, length)
' See if the title contains the target text.
If InStr(title, g_Contains) > 0 Then
' This is the target. Search for the desired path.
If FollowChildPath(txt, app_hwnd, _
LCase$("IEFrame/WorkerA/ReBarWindow32/ComboBoxEx32/ComboBox/Edit/")) _
Then
frmWindowList.txtAddress.Text = txt
Else
frmWindowList.txtAddress.Text = "<not found>"
End If
' Stop searching.
EnumProc = 0
Else
' Continue searching until we find it.
EnumProc = 1
End If
End Function
|
|
Function FollowChildPath follows a path of child classes starting from a specified parent window. For example, the path IEFrame/WorkerA/ReBarWindow32 looks for an IEFrame child, that has a WorkerA child, that has a ReBarWindow32 child. The path to find an Internet Explorer address is:
IEFrame/WorkerA/ReBarWindow32/ComboBoxEx32/ComboBox/Edit
Function FollowChildPath finds the current window's class and verifies that its current path matches the target path so far. If the current and target paths match completely, then the function is done so it calls function WindowText to get the window's text value and stops searching.
If the current path matches the target path so far but is not finished, the function lists the window's children. It then calls itself recursively to search the children for the target path.
|
|
' Search this window's children for the specified path.
' If we find the path, return the final window's text
' through parameter window_text and return True.
Public Function FollowChildPath(ByRef window_text As _
String, ByVal window_hwnd As Long, ByVal target_path As _
String, Optional ByVal current_path As String = "") As _
Boolean
Dim txt As String
Dim buf As String
Dim buflen As Long
Dim child_hwnd As Long
Dim children() As Long
Dim num_children As Integer
Dim i As Integer
' Assume we will not find the target path.
FollowChildPath = False
' Get the class name.
buflen = 256
buf = Space$(buflen - 1)
buflen = GetClassName(window_hwnd, buf, buflen)
buf = Left$(buf, buflen)
' Add the class name to the current path.
current_path = current_path & LCase$(buf) & "/"
' See if we're on the right track.
If Left$(target_path, Len(current_path)) <> _
current_path Then
' We are off the path. Don't search it.
ElseIf target_path = current_path Then
' We are on the path and have reached the end.
' Set the window's text and stop searching.
If buf = "Edit" Then
window_text = WindowText(window_hwnd)
Else
window_text = "<not an Edit window>"
End If
FollowChildPath = True
Else
' We are on the path but have not reached the end.
' Make a list of the child windows.
num_children = 0
child_hwnd = GetWindow(window_hwnd, GW_CHILD)
Do While child_hwnd <> 0
num_children = num_children + 1
ReDim Preserve children(1 To num_children)
children(num_children) = child_hwnd
child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
Loop
' Search the child windows.
For i = 1 To num_children
If FollowChildPath(window_text, children(i), _
target_path, current_path) Then
' We found the result. Stop searching.
FollowChildPath = True
Exit For
End If
Next i
End If
End Function
|
|
Function WindowText uses the SendMessage API function to return a window's text.
|
|
' Return the text associated with the window.
Public Function WindowText(window_hwnd As Long) As String
Dim txtlen As Long
Dim txt As String
WindowText = ""
If window_hwnd = 0 Then Exit Function
txtlen = SendMessage(window_hwnd, WM_GETTEXTLENGTH, 0, _
0)
If txtlen = 0 Then Exit Function
txtlen = txtlen + 1
txt = Space$(txtlen)
txtlen = SendMessage(window_hwnd, WM_GETTEXT, txtlen, _
ByVal txt)
WindowText = Left$(txt, txtlen)
End Function
|
|
|
|
|
|