Solved

CWnd of button with text and GetWindowText returns ""!!! How to get the text then??? HELP

Posted on 2001-06-20
13
412 Views
Last Modified: 2013-11-20
Hello,

I want to be able to list all the button's, edit box and widjets on a window. The example I
started with was calc.exe's calculator window. So I figured it would be easy to obtain
all the button's IDs and the associated text, such as "0" or "PI" or "+"...

Turns out that's not so simple! Many of the buttons don't return the text that they
display when GetWindowText is called!

Here is the function that fills a vector of <long, string> struct representing the <id, text>
of all the stuff that a window contains:
UINT uID;
CString tmp;
CWnd* pW= NULL;

// start Calculator before you call this!!!
HWND hwndChild  = ::GetWindow(::FindWindow("SciCalc", "Calculator"), GW_CHILD);
while (hwndChild)
{
  uID = ::GetDlgCtrlID(hwndChild);
  pW = CWnd::FromHandle(hwndChild);
  if (pW)
    {
        pW->GetWindowText(tmp);
        if (!tmp.IsEmpty())
     m_DlgIds.push_back(DialogItem(uID, tmp));
        else
        { // empty text!!! I tried this to see, but it fails also in retreiving the text of the button!!!
          pW->GetDlgItemText(uID, tmp);
          m_DlgIds.push_back(DialogItem(uID, "No text available"));
         }
    }
    hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT);
 }

I would like th remove the ""No text available" and be able to retreive the correct text such as
"9" or "0"... How could this be done????

Any hints/ideas will be appreciated!

0
Comment
Question by:aquila98
  • 4
  • 4
  • 4
  • +1
13 Comments
 
LVL 2

Author Comment

by:aquila98
ID: 6210076
And here is the struct
     class DialogItem
     {
     public:
          DialogItem(long, const char*);
          ~DialogItem();

          long GetID();
          string GetText();

     private:
          long m_ID;
          string m_Text;
     };

and vector<DialogItem> m_DlgIds;
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 6210126
Hi aquila98,

this is an excerpt from MSDN help about ::GetwindowText:
----------------------
The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application.(!)
...
To retrieve the text of a control in another process, send a WM_GETTEXT message directly instead of calling GetWindowText.(!)
----------------------

hope that helps,

ZOPPO
0
 
LVL 2

Author Comment

by:aquila98
ID: 6210171
In fact...

If you try the small bits of code you'll see that it DOES retreive some of the button's text
such as "Inv", "Grad", "Radians" but not those for the digits and operator and stuff.

The find window retreives the handle of the calculator's application and the while
loops thru its childs windows, which in our case are widgets such as radio buttons
or buttons. Just the stuff I want to retreive the text from!

sending WM_GETTEXT like this:
if (!tmp.IsEmpty())
     m_DlgIds.push_back(DialogItem(uID, tmp));
else
{
     char buf[200];
     if (::SendMessage(hwndChild,WM_GETTEXT, (WPARAM)200, (LPARAM)buf))
          m_DlgIds.push_back(DialogItem(uID, buf));
     else
          strcpy(buf, "No text available");
}

does not work :(


0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 9

Expert Comment

by:Pacman
ID: 6210194
GetWindowText() sends a WM_GETTEXT ...
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6210246
Have you thought that they may be no window text to get? - these buttons are owner draw so where they get there text from to display is anyones guess - I have made controls in the past (eg password entry controls) where the password cannot be got using GetWindowText
0
 
LVL 9

Expert Comment

by:Pacman
ID: 6210251
You can't get the text of this buttons because they are ownerdrawn and the window procedure of these controls doesn't implement a WM_GETTEXT handler.

In this case I would say it's not possible to get the text ...

If you don't have to get the text of owner drawn buttons like these then better look for a different test application.

regards
Pacman
0
 
LVL 9

Expert Comment

by:Pacman
ID: 6210261
just 1 minute too late  ... ;)
0
 
LVL 2

Author Comment

by:aquila98
ID: 6210308
discouraging words!

So there are not tricks to retreive the text from owner draw buttons?
No undocumented message hidden somewhere or anything?

:(
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 150 total points
ID: 6210594
not really - because an owner drawn button is just that - a drawing - it could be a picture.

> just 1 minute too late  ... ;)

:)
0
 
LVL 2

Author Comment

by:aquila98
ID: 6210644
Too bad there is not a IsKindOf for ownerdraw...

If you ever find out a trick to do this let me know... Turns out I picked the worst
(or best) application for my test ;-)

0
 
LVL 9

Expert Comment

by:Pacman
ID: 6210716
well, you could fetch the bitmap and do some text recognition ...  ;-)
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6211009
> Too bad there is not a IsKindOf for ownerdraw...

yes there is its called a window style (or in this a case a button style) BS_OWNERDRAW (0x0000000B)

> well, you could fetch the bitmap and do some text recognition ...  ;-)

I actually considered posting a similar comment using OCR but decided against it :)

0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6211021
aquila98 - glad to help
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

774 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