Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the Text of a Window in Another Application

Status
Not open for further replies.

gandapanda

Programmer
Dec 4, 2001
19
0
0
US
I want to get the text from the edit box of IBM Viavoice. I want to transfer the text from ViaVoice to C++. I tried using the following code but i receive errors:
CWnd* pWnd = GetOtherAppWindow();
TCHAR buf[512];
pWnd->SendMessage(WM_GETTEXT,sizeof(buf)/sizeof(TCHAR),
(LPARAM)(void*)buf);
I dont quite understand what the top line does in this code. Anyone with windows programming experience, please help!
 
The first line must get the Window of interest, but it can't. To find a Window, You should use :

EnumWindows( EnumWindowsProc, (LPARAM) 0);

The Handle of window of interest will be in the EnumWindowsProc(), if it is a top-level Window. If not (for example, You look for Edit Box), call the EnumChildWindows() in the EnumWindowsProc again until You find it.

#define TEXT_LENGTH 1024 //The max length of text You wish read
char text[TEXT_LENGTH+1];

BOOL CALLBACK EnumWindowsProc( HWND hWnd, LPARAM lParam )
{

long leng = ::SendMessage( hWnd, WM_GETTEXT, (WPARAM)TEXT_LENGTH, (LPARAM)text );
if(...) {
//It is Your Window - for example, You check the text
return FALSE; //Stop enum
}
...
return TRUE; //Continue enum
}

If You wish easy control all Windows, get rtheir texts etc, use this link:
 
Thanks, tchouch...
One question tho: where does this text get stored after it is fetched from the window???
Is there any way I can store it with a variable name?
Sonny
 
The text is copied in the text - Array.
You can do so to change text buffer:

#define TEXT_LENGTH 1024 //The max length of text You wish read
char text1[TEXT_LENGTH+1];
char text2[TEXT_LENGTH+1];
char text3[TEXT_LENGTH+1];

EnumWindows( EnumWindowsProc, (LPARAM) text1);
EnumWindows( EnumWindowsProc, (LPARAM) text2);
HWND hWndParent = ...; //Parent of interest
EnumChildWindows(hWndParent, EnumWindowsProc, (LPARAM) text3);

BOOL CALLBACK EnumWindowsProc( HWND hWnd, LPARAM lParam )
{

long leng = ::SendMessage( hWnd, WM_GETTEXT,
(WPARAM)TEXT_LENGTH, lParam );
if(...) {
//It is Your Window - for example, You check the text
return FALSE; //Stop enum
}
...
return TRUE; //Continue enum
}
 
first of all, thanks so much, tchouch...
one more question... the parent HWND hWndParent would be...? c++ or ViaVoice(the program i want to get text from)??? and the EnumWindowsProc(HWND hWnd, LPARAM lParam) <- what goes in the parameters... or do i type your code EXACTLY the way its shown in this line...
also, im getting errors because im not using all the appropriate header files... HELP!!
thanks for all your help
Sonny
 
The Function EnumWindows( EnumWindowsProc, (LPARAM) text1); enums all top-Level Windows and for that calls the callback EnumWindowsProc() automatically many times with all valid Arguments, so You don't need to think about Arguments of EnumWindowsProc()! You only must select in EnumWindowsProc() (with if(...)) all Windows of interest.
Edit Box is not a top-level window, it is probably a child of ViaVoice - Application, so You need to find the handle of top-level Window of ViaVoice - Application (for example, with FindWindow( ) or with call EnumWindows()) - it will now be hWndParent - and call EnumChildWindows(hWndParent, ...); - it will enum all child Windows, and You can select in EnumWindowsProc() what You needs. You can call EnumChildWindows() within EnumWindowsProc() with hWnd as Parameter too.

You needs this header:

#define <winuser.h>

Generally, You can include all common headers - they contain all You needs (<winuser.h> too):
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxole.h> // MFC OLE classes
#include <afxodlgs.h> // MFC OLE dialog classes
#include <afxcmn.h> // MFC common controls
#include <afxrich.h> // MFC Rich edit controls
#include <afxpriv.h>
#include <afxtempl.h> //Some MFC-Templates
#include <afxdisp.h> // MFC OLE automation classes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top