Solved

How to read a Label/Text box on a separate program

Posted on 2004-04-19
4
308 Views
Last Modified: 2010-05-02
Hello

I need to read a Label/Textbox on another program. I can get the handle to the window containing the Label/Textbox using FindWindowLike API and SPY++ but can't get a reference to the actual Label/Textbox. Do these controls have handles and if so how do you find them? I am wanting to get the Label/Textbox's caption/text, forecolour properties etc.

thanks for your help

0
Comment
Question by:c4mar
4 Comments
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 125 total points
ID: 10885793
The "controls" themselves are just child-windows to a parent window. You can use GetWindowText() (or your own WM_GETTEXT) on the control's window-handle and you should be able to get it's caption/text. As for it's colors, I'm not sure. You could use GetWindowRect() to get the dimensions of the "control" (window) and use GetPixel() to get the RGB values of the x/y positions of the window. There's probably a better method but I'm not aware of it.

FindWindow() can get the parent window and FindWindowEx can get a child-window of the parent. Start a new project with Form1 (caption of 'Form1') and add Text1 (caption of 'Text1') and try the below within Command1.

Form1:
--------------

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Sub Command1_Click()
    Dim lHandle As Long, sBuff As String, iLen As Integer
    'Find the handle of our form by searching for it's caption 'Form1'
    lHandle = FindWindow(vbNullString, "Form1")
    If lHandle <> 0 Then
        'Search the parent window (lHandle) for a child window with the
        'caption of 'Label1'
        lHandle = FindWindowEx(lHandle, 0, vbNullString, "Text1")
        If lHandle <> 0 Then
            sBuff = Space(1024)
            iLen = GetWindowText(lHandle, sBuff, Len(sBuff))
            sBuff = Left(sBuff, iLen)
            MsgBox sBuff
        End If
    End If
End Sub


........

I just tested and edited out my example of Labels since VB doesn't seem to create a window for them. Their handles don't show up in Spy++ nor can I use FindWindowEx() on it so I'm guessing they use a different method. Other application's labels have a window with a classname of "STATIC" and possibly self-registered classes.


0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now