-
Hi.
I need to get the text that's contained in a specific Window. It seems like there are all kinds of API calls for a Window's handle, and a few for dealing with a Window's title, but I need to find one that will return the TEXT in the Window.
Any suggestions or, better yet, code?
Thanks,
Chris
-
What kind of windows are you thinking of?
There is an API called GetWindowText but I think this will retrieve the caption of a window.
-
Frans,
The window is an AS/400 terminal emulation program.
I've used the GetWindowText API call, but it only returns the Caption as you indicated in your response.
I need the actual data in the Window. It seems like if you can get a Window's title, you should be able to get the data that the window contains.
Please keep all ideas and suggestions coming.
Thanks,
Chris
-
Just a friendly reminder that I (or no one else where I work) still haven't figured out how to do this!
Does anybody have any suggestions.
Thanks,
Chris
-
If you have the hWnd, enum the child windows of that window and get the text from every childwindow you get, see if that works.
-
Here's an Example I've put together which retrieves any Text associated with a Window or its Children, it uses the SendMessage API with the WM_GETTEXT Constant to retrieve the Text instead of the GetWindowText API as it's used only for retrieving Caption Text associated with a Window Handle.
Place a Multiline Textbox and a Timer Control on a Form..
In a Module..
Code:
Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_GETTEXT = &HD
Public Const WM_GETTEXTLENGTH = &HE
Public sText As String
Public Function EnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim sBuffer As String
Dim lBuffLen As Long
'Return Zero to Finish Enumerating Child Windows
EnumProc = hwnd
'Get the Text Associated with this Child Window Handle..
lBuffLen = SendMessage(hwnd, WM_GETTEXTLENGTH, 0&, ByVal 0&) + 1
sBuffer = Space(lBuffLen)
Call SendMessage(hwnd, WM_GETTEXT, lBuffLen, ByVal sBuffer)
'Add the Text to our String Buffer
sText = sText & Replace(sBuffer, Chr(0), "") & vbCrLf
'Now Enumerate the Child Windows of this Child Window (Recursion)
'To do this we need to replicate the Enum Function..
Call EnumChildWindows(hwnd, AddressOf EnumChildProc, 0&)
End Function
Private Function EnumChildProc(ByVal hwnd As Long, lParam As Long) As Long
Dim sBuffer As String
Dim lBuffLen As Long
'Replica of the EnumProc Function used for Recursively Enumerating Child Windows
EnumChildProc = hwnd
lBuffLen = SendMessage(hwnd, WM_GETTEXTLENGTH, 0&, ByVal 0&) + 1
sBuffer = Space(lBuffLen)
Call SendMessage(hwnd, WM_GETTEXT, lBuffLen, ByVal sBuffer)
sText = sText & Replace(sBuffer, Chr(0), "") & vbCrLf
Call EnumChildWindows(hwnd, AddressOf EnumProc, 0&)
End Function
In the Form..
Code:
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Const VK_LBUTTON = &H1
Private Sub Form_Load()
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
Dim lHwnd As Long
Dim tPOS As POINTAPI
Dim lBuffLen As Long
If GetAsyncKeyState(VK_LBUTTON) Then
'Left Mouse Button Down, Get the Cursors Coords..
Call GetCursorPos(tPOS)
'Get the Window Handle for the Window Under the Mouse Cursor
lHwnd = WindowFromPoint(tPOS.x, tPOS.y)
'Only process if it's not our own Form..
If lHwnd <> hwnd And GetParent(lHwnd) <> hwnd Then
'Make sure we have the Parent Window Handle
If GetParent(lHwnd) Then lHwnd = GetParent(lHwnd)
'Get the Parents Text, usually a Caption..
lBuffLen = SendMessage(lHwnd, WM_GETTEXTLENGTH, 0&, ByVal 0&) + 1
sText = Space(lBuffLen)
Call SendMessage(lHwnd, WM_GETTEXT, lBuffLen, ByVal sText)
sText = Replace(sText, Chr(0), "") & vbCrLf
'Now Enumerate ALL Child Windows, ie. Command Buttons, Lists, Textboxes, etc..
Call EnumChildWindows(lHwnd, AddressOf EnumProc, 0&)
'Display the Retrieved Text
Text1 = sText
'Wait until the Mouse if Released
While GetAsyncKeyState(VK_LBUTTON)
DoEvents
Wend
End If
End If
End Sub
Point and Click on a Window to see ALL text associated with it, ie. Open Notepad, type something then Click it while Running this Example.
------------------
Aaron Young
Analyst Programmer
[email protected]
[email protected]
-
Aaron, thanks for the code. It works great with Notepad and many other windows, but it isn't returning the text from the Terminal Emulation Screen.
As an example of your code not being able to return every bit of text in a Window, click on this Window (your web browser) and see what is displayed. You'll notice that the HTML source code doesn't appear.
Maybe the "Window" that's holding the text is a Control rather than a Window? Just a thought.
I feel like with a few minor adjustments, Aaron's code will work and get the Text on the Termnal Emulation Screen.
Can anyone help me out?
Thanks.
Chris
-
Controls really are just Child Windows, anyway..
There are some Classes which Hide/restrict access to the Text within them, other than IE as you pointed out, a DOS Console is another example, in these cases you really need to look at trying to Pipe the Data to a File or maybe even use some inbuilt Logging system in the Application itself, then interpret the Logfile in your VB App.
As I don't know the particular software you're using, I'm afraid I can't be of much more help.
------------------
Aaron Young
Analyst Programmer
[email protected]
[email protected]
-
Do you get payed a lot as an Analyst Programmer coz you seem to have a good Idea what you are doing so I would guess are at the top end of you proffesion.
Just wondering
-
Aaron you are briliant.
Thank you very much.
May the force be with you forever.
------------------
Jorge Ledo
[email protected]
Portugal