I was wondering how I could retrive the text from any label?
-Thanks
Printable View
I was wondering how I could retrive the text from any label?
-Thanks
What's wrong with [lblName].Caption?
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.
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.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
Thanks! I'm new with API functions and heh, how would I put this to use?
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.
Yea, I would really appreciate that, do it when you have the time.:)
Thanks!
I'm guessing your using Vb, so here it is in vb. Notice the timer control and also the getText().
I tried this code but it didn't work. The text i'm trying to get from is static.
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.