Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

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

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
aquila98
Asked:
aquila98
  • 4
  • 4
  • 4
  • +1
1 Solution
 
aquila98Author Commented:
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
 
ZoppoCommented:
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
 
aquila98Author Commented:
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!

 
PacmanCommented:
GetWindowText() sends a WM_GETTEXT ...
0
 
ShaunWildeCommented:
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
 
PacmanCommented:
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
 
PacmanCommented:
just 1 minute too late  ... ;)
0
 
aquila98Author Commented:
discouraging words!

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

:(
0
 
ShaunWildeCommented:
not really - because an owner drawn button is just that - a drawing - it could be a picture.

> just 1 minute too late  ... ;)

:)
0
 
aquila98Author Commented:
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
 
PacmanCommented:
well, you could fetch the bitmap and do some text recognition ...  ;-)
0
 
ShaunWildeCommented:
> 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
 
ShaunWildeCommented:
aquila98 - glad to help
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now