Results 1 to 10 of 10

Thread: Getting text from any label?

  1. #1

    Thread Starter
    Lively Member DeBauer's Avatar
    Join Date
    May 2001
    Location
    Mako Reactor #7
    Posts
    123

    Question Getting text from any label?

    I was wondering how I could retrive the text from any label?

    -Thanks

  2. #2
    What's wrong with [lblName].Caption?

  3. #3

    Thread Starter
    Lively Member DeBauer's Avatar
    Join Date
    May 2001
    Location
    Mako Reactor #7
    Posts
    123
    I'm talking about compiled programs, Like lets say I get a pop up msg and I want to get that text out of a label. This code I need to work for everything... a universal code to get the text out of any kind of label.

  4. #4
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    Wink I got you covered

    Code:
    Option Explicit
    
    Declare Function FindWindow _
    Lib "user32" Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String _
    ) 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 FindWindowEx _
    Lib "user32" Alias "FindWindowExA" ( _
        ByVal hWnd1 As Long, _
        ByVal hWnd2 As Long, _
        ByVal lpsz1 As String, _
        ByVal lpsz2 As String _
    ) 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
    
    
    Public Const WM_GETTEXT = &HD
    Public Const WM_GETTEXTLENGTH = &HE
    
    Private Function GetText() as long
    
    Dim hwndParent As Long, hwndChild As Long, hwndChildPrev As Long
        Dim hwndChildTextLength As Long, hwndChildText As String, lCharactersReturned As Long
        hwndParent = FindWindow("WindowClass", vbNullString)
        hwndChildPrev = FindWindowEx(hwndParent, 0, "Static", vbNullString)
        hwndChild = FindWindowEx(hwndParent, hwndChildPrev, "Edit", vbNullString)
        hwndChildTextLength = SendMessage(hwndChild, WM_GETTEXTLENGTH, 0, ByVal 0&) + 1
        hwndChildText = Space(hwndChildTextLength)
        Call SendMessage(hwndChild, WM_GETTEXT, hwndChildTextLength, ByVal hwndChildText)
        GetText = hwndChildText
    
    End Function
    Some labels have the class 'static' other are just 'edit' classes that are made to look like labels, and other labels, are similar to the DrawText or TextOut Functions.
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  5. #5

    Thread Starter
    Lively Member DeBauer's Avatar
    Join Date
    May 2001
    Location
    Mako Reactor #7
    Posts
    123
    Thanks! I'm new with API functions and heh, how would I put this to use?

  6. #6
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    ok

    Well to start off u need a Window Spy similar to Spy++ or any other home brand ones.

    Once you have the class of the child window(label) and the paren window, andor the previous control in the order to the child window(label) just stick their classes names is. I'll post a working example 2moro if you want. I haven't any time now. sorry.
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  7. #7

    Thread Starter
    Lively Member DeBauer's Avatar
    Join Date
    May 2001
    Location
    Mako Reactor #7
    Posts
    123
    Yea, I would really appreciate that, do it when you have the time.


    Thanks!

  8. #8
    Hyperactive Member Wak's Avatar
    Join Date
    Nov 2000
    Location
    Brisbane, Queensland
    Posts
    298

    Here's my example

    I'm guessing your using Vb, so here it is in vb. Notice the timer control and also the getText().
    Visual Basic 6.0 Enterprise
    Visual C++ 6.0 Professional

    Wak

  9. #9

    Thread Starter
    Lively Member DeBauer's Avatar
    Join Date
    May 2001
    Location
    Mako Reactor #7
    Posts
    123
    I tried this code but it didn't work. The text i'm trying to get from is static.
    It's better to burn out than fade away...


  10. #10
    Megatron
    Guest
    Note: You can only get the text of an "actual window", i.e. trying to get the caption of an external VB "Label" will not work, because a "Label" is not actually a window.

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