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
Solved

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

Posted on 2001-06-20
13
414 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 140
countEvens challenge 2 111
canBalance challenge 34 96
ASP.NET C# MessageBox.Show Showing a modal dialog box or form when the application ... 2 160
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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. …
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.

808 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