Solved

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

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 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