|
|
Title | Grab text and other window information from another application |
Keywords | grab text, application, window information |
Categories | Software Engineering, API |
|
|
The program uses FindWindow to find the other application.
Subroutine WindowInfo uses GetClassName and the WindowText function to get information about a window. It then uses GetWindow to list the window's children and calls itself recursively to describe the children.
|
|
' Return information about this window and its
' children.
Public Function WindowInfo(window_hwnd As Long)
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
Dim lvi As LVITEM
' Get the class name.
buflen = 256
buf = Space$(buflen - 1)
buflen = GetClassName(window_hwnd, buf, buflen)
buf = Left$(buf, buflen)
txt = "Class: " & buf & vbCrLf
' hWnd.
txt = txt & " hWnd: " & _
Format$(window_hwnd) & vbCrLf
' Associated text.
txt = txt & " Text: [" & _
WindowText(window_hwnd) & "]" & vbCrLf
' 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
' Get information on the child windows.
For i = 1 To num_children
txt = txt & WindowInfo(children(i))
Next i
WindowInfo = txt
End Function
|
|
The WindowText function uses the SendMessage API function to ask the window how long its text is. It then allocates enough space and uses SendMessage again to get the 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
|
|
|
|
|
|