Solved

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

Posted on 2001-06-20
13
430 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
[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
  • 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
Independent Software Vendors: 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!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

634 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