?
Solved

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

Posted on 2004-04-19
4
Medium Priority
?
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 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