Solved

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

Posted on 2004-04-19
4
334 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…

790 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