From: Hans-J. Ude on
To avoid using fixed size buffers I want to read the text of an unknown
window into a CString. All I know about is it's HWND. To do that I wrote
this code:

CWnd *pWndTemp;
CWnd *pWndClone;
CString strText;

pWndTemp = new CWnd;
pWndClone = pWndTemp->FromHandle(hwndUnknown);
pWndClone->GetWindowText(strText);
delete pWndTemp;

That works, but I have a feeling that there's a more elegant way. Any ideas?

Hans

From: AliR (VC++ MVP) on
I think the elegent way would be:

CString strText;
CWnd *pWnd = CWnd::FromHandle(hwndUnknown);
pWnd->GetWindowText(strText);

AliR.



"Hans-J. Ude" <news(a)wolptec.de> wrote in message
news:69ea58F32ifqoU1(a)mid.individual.net...
> To avoid using fixed size buffers I want to read the text of an unknown
> window into a CString. All I know about is it's HWND. To do that I wrote
> this code:
>
> CWnd *pWndTemp;
> CWnd *pWndClone;
> CString strText;
>
> pWndTemp = new CWnd;
> pWndClone = pWndTemp->FromHandle(hwndUnknown);
> pWndClone->GetWindowText(strText);
> delete pWndTemp;
>
> That works, but I have a feeling that there's a more elegant way. Any
> ideas?
>
> Hans


From: Hans-J. Ude on
"AliR (VC++ MVP)" <AliR(a)online.nospam> wrote

>I think the elegent way would be:
>
> CString strText;
> CWnd *pWnd = CWnd::FromHandle(hwndUnknown);
> pWnd->GetWindowText(strText);

Thanks Ali,
that was quick! I really didn't see that, too late here now. Since i
validated the HWND before one can even write.

CWnd::FromHandle(hwndUnknown)->GetWindowText(strText);

thanks,
Hans

From: Giovanni Dicanio on

"Hans-J. Ude" <news(a)wolptec.de> ha scritto nel messaggio
news:69ea58F32ifqoU1(a)mid.individual.net...

> To avoid using fixed size buffers I want to read the text of an unknown
> window into a CString. All I know about is it's HWND. To do that I wrote
> this code:

AliR already showed you the elegant way.

Of course you can wrap AliR's solution in a function:

<code>

inline CString GetWndText( HWND hWnd )
{
CString strText;
CWnd *pWnd = CWnd::FromHandle( hWnd );
pWnd->GetWindowText( strText );
return strText;
}

</code>

Some more comments:

> pWndTemp = new CWnd;
> pWndClone = pWndTemp->FromHandle(hwndUnknown);

The above 'new' is useless, because CWnd::FromHandle is a *static* method.
So you don't need to create an instance of CWnd to call it.

> delete pWndTemp;

This delete is uselss, because the window object pointer (CWnd *) returned
by FromHandle is *temporary* :

CWnd::FromHandle
http://msdn.microsoft.com/en-us/library/e547yfza(VS.80).aspx

There is a static method (CWnd::DeleteTempMap) that deletes temporary
objects created from CWnd::FromHandle.
This method is automatically called by the MFC framework (you don't need to
explicitly call it).

Giovanni


From: Joseph M. Newcomer on
See below,,,
On Mon, 19 May 2008 23:36:09 +0200, "Hans-J. Ude" <news(a)wolptec.de> wrote:

>To avoid using fixed size buffers I want to read the text of an unknown
>window into a CString. All I know about is it's HWND. To do that I wrote
>this code:
>
>CWnd *pWndTemp;
>CWnd *pWndClone;
>CString strText;
>
>pWndTemp = new CWnd;
****
And what role does pWndTemp serve here, other than taking up space, forcing a meaningless
storage allocation, and cluttering up your code with some pointless variable that serves
no useful purpose?
****
>pWndClone = pWndTemp->FromHandle(hwndUnknown);
>pWndClone->GetWindowText(strText);
>delete pWndTemp;
>
>That works, but I have a feeling that there's a more elegant way. Any ideas?
****
No, the code I would write would be

CString text;
CWnd * wnd = CWnd::FromHandle(hwndUnknown);
wnd->GetWindowText(text);

Note that, if you RTFM, CWnd::FromHandle is a static method, and therefore you do not need
any object to reference it. So you needed 7 lines to accomplish what 3 lines will do
joe
****
>
>Hans
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm