Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-04-19
4
Medium Priority
?
404 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
1 Comment
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 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