Solved

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

Posted on 2001-06-20
13
406 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 30

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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now