|
-
Mar 8th, 2000, 02:49 PM
#1
Thread Starter
Addicted Member
I need to have my app read the text on textboxes or labels of other app, say windows calculator or a dialog box.
How do i go about to achieve this? I think I should use API calls and find handles of the windows and move down to the childrens... I just haven't got a clue which API and where to go from there...
Any suggestion/reference will be appreciated, any code is even more appreciated
-
Mar 8th, 2000, 11:36 PM
#2
Here's an example on how to get the text out of Calculator:
Code:
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Sub Command1_Click()
Dim lCalc As Long
Dim lTextbox As Long
Dim lLen As Long
Dim strBuffer As String
lCalc = FindWindowEx(0, 0, "SciCalc", vbNullString)
lTextbox = FindWindowEx(lCalc, 0, "Static", vbNullString)
lLen = SendMessage(lTextbox, WM_GETTEXTLENGTH, 0, 0)
strBuffer = Space(lLen)
Call SendMessageStr(lTextbox, WM_GETTEXT, lLen, ByVal strBuffer)
MsgBox "Text in Calculator: " & strBuffer
End Sub
For other application you want to get the text from, you would have to know the Class name of the Application and Child object (window) on the application.
Regards,
-
Mar 9th, 2000, 05:49 PM
#3
Thread Starter
Addicted Member
thanks Serge,
Your answer brings up another question. Is there a way to get the classname of an open window of an app? I've seen this been done somewhere but can't find it back...
best regards
-
Mar 9th, 2000, 09:37 PM
#4
Sure. You can use this small routine I wrote a while back to see all windows that are running with their ClassNames, Captions and hWnds. Start a new project. Add a ListView and a module to your project. Copy this code to a module:
Module Code:
Code:
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam 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 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
Public Function EnumWinProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim strClassName As String
Dim strCaption As String
Dim lCaptionLength As Long
Dim lRet As Long
Dim lstItem As ListItem
strClassName = Space(255)
lRet = GetClassName(hwnd, strClassName, Len(strClassName))
Set lstItem = Form1.ListView1.ListItems.Add(, , hwnd)
If lRet Then
lstItem.SubItems(1) = Left(strClassName, lRet)
End If
lCaptionLength = GetWindowTextLength(hwnd)
strCaption = Space(lCaptionLength)
lRet = GetWindowText(hwnd, strCaption, lCaptionLength)
If lRet Then
lstItem.SubItems(2) = Left(strCaption, lRet)
End If
EnumWinProc = 1
End Function
Then copy this code to your Form:
Code:
Private Sub Form_Load()
Dim hdrHeader As ColumnHeader
With ListView1
.View = lvwReport
Set hdrHeader = .ColumnHeaders.Add(, , "hWnd")
Set hdrHeader = .ColumnHeaders.Add(, , "Class", 2000)
Set hdrHeader = .ColumnHeaders.Add(, , "Caption", 4000)
.FullRowSelect = True
End With
Call EnumWindows(AddressOf EnumWinProc, 0)
End Sub
Running the project will list all running windows (including hidden) with their hWnd, Class and Caption.
-
Mar 10th, 2000, 01:23 AM
#5
Thread Starter
Addicted Member
Thanks lotz Serge, you've been a great help
-
Mar 14th, 2000, 03:00 AM
#6
New Member
How can you change these texts or labels?
How do you change these texts or labels? Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|