Results 1 to 9 of 9

Thread: Grabbing text

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 1999
    Location
    Gloucester, UK
    Posts
    33
    Is it possible to find out whether a textbox currently has the users focus? If so how can my application grab the text out of it.

    (I am not trying to make a password sniffer, just add spellchecking facilty to programs such as notepad.)
    VB / GIS Consultant
    VB6 SP4, VC++6

  2. #2
    Fanatic Member Dim's Avatar
    Join Date
    Jul 2000
    Posts
    620
    I THINK under the Text1_GotFocus sub, place a call focus onto the form and not the textbox...

    hope that helped cause API isn't my area.
    D!m
    Dim

  3. #3
    Guest
    I think he means out of other programs, like he mentioned, Notepad,
    you would still have to use GetWindowText...
    I think this should work....


    Code:
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Type POINTAPI
            x As Long
            y As Long
    End Type
    
    Private Function WinFromXY()
    Dim CurPos As POINTAPI
    Call GetCursorPos(CurPos)
    WinFromXY = WindowFromPoint(CurPos.x, CurPos.y)
    End Function

    that gets the hWnd of where the cursor is, so, you could get the text by using that, I guess

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 1999
    Location
    Gloucester, UK
    Posts
    33
    Yep, got it working!

    Many thanks for your help
    VB / GIS Consultant
    VB6 SP4, VC++6

  5. #5
    Addicted Member
    Join Date
    Apr 2000
    Posts
    215
    I thought the code looked intresting so I attempted to use it:

    Code:
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Public Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Public Type POINTAPI
            x As Long
            y As Long
    End Type
    
    Public Function WinFromXY()
    Dim CurPos As POINTAPI
    Call GetCursorPos(CurPos)
    WinFromXY = WindowFromPoint(CurPos.x, CurPos.y)
    End Function
    
    
    Private Sub Timer1_Timer()
        
        Dim TextGotten As String
        TextGotten = String(10000, Chr$(0))
        GetWindowText WinFromXY, TextGotten, 10000
        TextGotten = Left$(TextGotten, InStr(TextGotten, Chr$(0)) - 1)
        Text2 = TextGotten
      
    End Sub
    it will get the caption of other windows, the text of command buttons, it will get text out of another text box if it is on my form, but it won't get it out of another text box in another program for me for some reason, I am on windows 98, what am I doing wrong?.

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 1999
    Location
    Gloucester, UK
    Posts
    33
    You can only grab text from controls in other applications if that control has no edit control. (according to the amazing VB programmers guide to the API by Dan Appleman)

    I guess thats to stop u stealing passwords etc
    VB / GIS Consultant
    VB6 SP4, VC++6

  7. #7
    Guest
    Try this almost like Megatron's code:

    Code:
    Needed: Form, Module, Textbox, and Timer
    
    Type POINTAPI
    X As Long
    Y As Long
    End Type
    
    Declare Function GetCursorPos Lib "user32" _
    (lpPoint As POINTAPI) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd 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
    Public Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    
    Function GetCaption(hWnd)
    hwndLength% = GetWindowTextLength(hWnd)
    hwndTitle$ = String$(hwndLength%, 0)
    a% = GetWindowText(hWnd, hwndTitle$, (hwndLength% + 1))
    GetCaption = hwndTitle$
    End Function
    
    Private Sub Timer1_Timer()
    Dim pnt As POINTAPI
    MousePointer = 0
    GetCursorPos pnt
    yhwnd% = WindowFromPointXY(pnt.X, pnt.Y)
    Text1.text = GetCaption(yhwnd%)
    End Sub




    [Edited by Matthew Gates on 07-18-2000 at 11:41 PM]

  8. #8
    Member
    Join Date
    Mar 2002
    Posts
    60

    Exclamation

    Hi All !!
    I am using Crypts Code and it works. But as said before it doesnt work for other apps than my own. So.... here it goes.

    I am using DDE with this 3rd party app and it is working so I suppose I could use some API to grab text from it right ?????
    maybe WM_COPYDATA.... I dont know.....

    Any thoughts ?!?!?!?! please ?
    Thanks !!
    Martin

  9. #9
    Member
    Join Date
    Mar 2002
    Posts
    60

    Exclamation Grabbing Text *Not resolved**

    Hey !
    And I tell you more...
    How can I grab text from a ListBox ? on another app I made.

    Ideas ?!?!!
    Thanks
    )
    Martin

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