Results 1 to 17 of 17

Thread: Get Text

  1. #1

    Thread Starter
    Hyperactive Member wasiq's Avatar
    Join Date
    Jan 2000
    Location
    Karachi, Sindh, Pakistan
    Posts
    274

    Post

    how can i get the text of any object my mouse is over, like buttons, combo boxes, text boxes, and list items.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Your own or other apps?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Hyperactive Member wasiq's Avatar
    Join Date
    Jan 2000
    Location
    Karachi, Sindh, Pakistan
    Posts
    274

    Post

    other programs

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That's a bit hard to do, you need to use a lot of api, because each control have different ways in showing it's text, basically getwindowtext. To know which window is att toplevel at that point of screen you need to enumerate all windows, and then enumerate all childwindows and then compare the rects of them (getwindowrect) for the last window that is at top in the enumeration, with the cursorposition (getcursorpos) and you have the right wnd. for getting the text, i'm not sure if you can do that with all controls, but you could try.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    Hyperactive Member wasiq's Avatar
    Join Date
    Jan 2000
    Location
    Karachi, Sindh, Pakistan
    Posts
    274
    i just want to know how can we get the text from all the controls, i can do the rest myself.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That's as i said hard but there's another way. You can get the DC of the desktopwindow and then copy the area your mouse is over with bitblt. and you will have a small box containing the text (and the background to it), i know thats probably not what you want, am i right?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7

    Thread Starter
    Hyperactive Member wasiq's Avatar
    Join Date
    Jan 2000
    Location
    Karachi, Sindh, Pakistan
    Posts
    274

    Post

    but thats not what i want, any other ideas?

  8. #8
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Question Kedaman, that would work, but what about this?

    More of my module-loving code:
    Code:
    Option Explicit
    
    Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
    
    Type POINTAPI
            x As Long
            y As Long
    End Type
    
    Function GetTextFromCursorPosition() As String
        Dim sBuffer As String, lBufferSize As Long, hWndThingie As Long, ptCursorPos As POINTAPI, lPos As Long
        
        Call GetCursorPos(ptCursorPos)
        hWndThingie = WindowFromPoint(ptCursorPos.x, ptCursorPos.y)
        ' Now, hWndThingie is the hWnd of the window where the mouse cursor is.
        
        lBufferSize = GetWindowTextLength(hWndThingie) + 1
        sBuffer = String(lBufferSize, vbNullChar)
        Call GetWindowText(hWndThingie, sBuffer, lBufferSize)
        
        ' Now, to trim the null(s)!
        lPos = InStr(sBuffer, vbNullChar)
        If lPos > 0 Then sBuffer = Left(sBuffer, lPos - 1)
        
        ' Return the text.
        GetTextFromCursorPosition = sBuffer
    End Function
    Then... Whenever... Use this:

    Call MsgBox("Why are you pointing at a TextBox/Window/Whatever whose caption/text/whatever is " & GetTextFromCursorPosition & "?")

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Thumbs up

    Wow! Yonatan, you really deserves to be Guru! Windowfrompoint, i didn't know that. Thanks!

    Also, i'm a module lover

    [Edited by kedaman on 05-28-2000 at 10:37 PM]
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    Hyperactive Member wasiq's Avatar
    Join Date
    Jan 2000
    Location
    Karachi, Sindh, Pakistan
    Posts
    274
    i tried that too, but it only gets the text from text boxes. any other ideas.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well listboxes and other controls that don't work with getwindowtext just have the text stored internally, You could try to get the DC instead and somehow extract the text from there by comparing it to different fonts in different size, but i think that's somewhat too hardcore
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Talking This should be a little check program... Not an OCR one. (Maybe)

    Extracting the text from graphics is OCR (Optical Character Recognition). It's a little too advanced for something like getting the text from ListBoxes. I haven't tested my code, but I think everything you wanted (except ListBoxes) works with the code I gave you.

    Here is some code that will identify whether it's a ListBox or not. If it is, it will get the text of the currently selected item there. If it isn't, it will run the GetWindowText code I gave you earlier.

    Try this code in a module (I haven't tested it, but it should work):
    Code:
    Option Explicit
    
    Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    
    Public Const LB_GETCURSEL = &H188
    Public Const LB_GETTEXT = &H189
    Public Const LB_GETTEXTLEN = &H18A
    Public Const LB_ERR = (-1)
    
    Type POINTAPI
            x As Long
            y As Long
    End Type
    
    Sub TrimNulls(ByRef sString As String)
        Dim lPos As Long
        
        lPos = InStr(sString, vbNullChar) ' Scan the string for nulls.
        If lPos > 0 Then sString = Left(sString, lPos - 1) ' Found one? Trim! Only leave whatever is before it.
    End Sub
    
    Function GetTextFromListBox(ByVal hWndListBox As Long) As String
        Dim sBuffer As String, lBufferSize As Long, lCurSel As Long
        
        lCurSel = SendMessage(hWndListBox, LB_GETCURSEL, 0, ByVal 0) ' Get the current selection index.
        If lCurSel = LB_ERR Then Exit Function ' No selection? Return an empty string.
        
        lBufferSize = SendMessage(hWndListBox, LB_GETTEXTLEN, lCurSel, ByVal 0) + 1 ' Get length + null place.
        sBuffer = String(lBufferSize, vbNullChar) ' Reserve the correct amount of places.
        Call SendMessage(hWndListBox, LB_GETTEXT, lCurSel, ByVal sBuffer) ' Get the text followed by a bunch of nulls.
        Call TrimNulls(sBuffer) ' Trim all the nulls!
        
        GetTextFromListBox = sBuffer
    End Function
    
    Function GetTextFromCursorPosition() As String
        Dim sBuffer As String, lBufferSize As Long, hWndThingie As Long, ptCursorPos As POINTAPI
        
        ' First we get the hWnd of the thingie where the cursor is.
        Call GetCursorPos(ptCursorPos)
        hWndThingie = WindowFromPoint(ptCursorPos.x, ptCursorPos.y)
        ' Now, hWndThingie is the hWnd of the window where the mouse cursor is.
        
        ' We need to check if it's a ListBox or not.
        sBuffer = String(255, vbNullChar) ' Reserve 255 places.
        Call GetClassName(hWndThingie, sBuffer, 255) ' Get the ClassName followed by a bunch of nulls.
        Call TrimNulls(sBuffer) ' Trim all the nulls!
        
        ' If you know any ListBox ClassNames other than "ListBox" and "ThunderListBox", add them!
        If (sBuffer = "ListBox") Or (sBuffer = "ThunderListBox") Then
            GetTextFromCursorPosition = GetTextFromListBox(hWndThingie)
            Exit Function
        End If
        
        ' If it's not a ListBox...
        lBufferSize = GetWindowTextLength(hWndThingie) + 1 ' Get length + null place.
        sBuffer = String(lBufferSize, vbNullChar) ' Reserve the correct amount of places.
        Call GetWindowText(hWndThingie, sBuffer, lBufferSize) ' Get the text followed by a bunch of nulls.
        Call TrimNulls(sString) ' Trim all the nulls!
        
        GetTextFromCursorPosition = sBuffer
    End Function
    Same syntax as before, except now it works with ListBoxes.

    If you want to get all the text of all the items in a ComboBox or a ListBox, then post a reply and tell me about it.

  13. #13
    Lively Member
    Join Date
    Apr 2000
    Posts
    110

    Wink May be of interest...

    Just thought this link might interest you. It links to
    some code for getting info on windows when you mouseover them. Check it out

    http://www.planet-source-code.com/vb...txtCodeId=5647

    Laterz

    REM

  14. #14

    Thread Starter
    Hyperactive Member wasiq's Avatar
    Join Date
    Jan 2000
    Location
    Karachi, Sindh, Pakistan
    Posts
    274

    Post

    i sure would love to have a code to get the caption from list boxes and combo boxes. thanks for all the help.

  15. #15
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Sedgefield
    Posts
    337

    Unhappy

    Kedaman posts = 1373: Fanatic member

    Yonatan posts = 1142: Guru

    Je ne comprends pas.

    ?????


    DAn

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Hehe, go complain about that to John. what does "Je ne comprends pas." mean?

    I'll test your code later Yonatan.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  17. #17
    Hyperactive Member
    Join Date
    Feb 2000
    Location
    Sedgefield
    Posts
    337

    Talking

    Its French for 'I don't understand'


    (no I'm not French, or Canadian)

    Dan

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