Results 1 to 6 of 6

Thread: get text from a textbox

  1. #1

    Thread Starter
    Addicted Member pardede's Avatar
    Join Date
    Jan 2000
    Posts
    232

    Post

    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

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    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,

  3. #3

    Thread Starter
    Addicted Member pardede's Avatar
    Join Date
    Jan 2000
    Posts
    232

    Post

    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

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    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.

  5. #5

    Thread Starter
    Addicted Member pardede's Avatar
    Join Date
    Jan 2000
    Posts
    232

    Post

    Thanks lotz Serge, you've been a great help

  6. #6
    New Member
    Join Date
    Mar 2000
    Posts
    6

    Post 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
  •  



Click Here to Expand Forum to Full Width