The Artima Developer Community
Sponsored Link

C# Answers Forum
SendMessage with WM_GETTEXT

3 replies on 1 page. Most recent reply: Dec 18, 2003 6:49 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Chaim

Posts: 1
Nickname: sonic1
Registered: Oct, 2003

SendMessage with WM_GETTEXT Posted: Oct 22, 2003 1:49 PM
Reply to this message Reply
Advertisement
Hi,
i want to recieve text from a combo on a different process. So I'm trying to use a SendMessage API function with the parameter of WM_GETTEXT, but i don't know how to assign the char buffer to the last parameter of the function.
and what sort of SendMessage declaration i should use ?
I'll be happy if someone would show a code example with his reply.
Thanks in advance.
Irena


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Why not GetWindowText()? Posted: Dec 18, 2003 5:03 PM
Reply to this message Reply
Do you know there is a GetWindowText() API call?

In any event, here is a homemade GetWindowText(), which uses SendMessage() and WM_GETTEXT, as you requested (it is also using the string object from STL:

std::string GetWindowText( HWND wnd )
{
std::string text;
WPARAM length = SendMessage( wnd, WM_GETTEXTLENGTH, 0, 0 );
if( length > 0 )
{
char *buffy = new char [length+1];

LRESULT got = SendMessage( wnd, WM_GETTEXT, length+1, (LPARAM)buffy );
if( (LRESULT)length == got )
text = buffy;

delete [] buffy;
}
return text;
}

(sorry about the lack of formatting; it looks like the tags support in the forum is temporarily not working)

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Why not GetWindowText()? Posted: Dec 18, 2003 6:47 PM
Reply to this message Reply
Here it is with nicer (java) formatting:

std::string GetWindowText( HWND wnd )
{
   std::string text;
WPARAM length = SendMessage( wnd, WM_GETTEXTLENGTH, 0,
0, 0 );
   if( length > 0 )
   {
      char *buffy = new char [length+1];
 
LRESULT got = SendMessage( wnd, WM_GETTEXT,
TTEXT, length+1, (LPARAM)buffy );
      if( (LRESULT)length == got )
         text = buffy;
 
      delete [] buffy;
   }
   return text;
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Why not GetWindowText()? Posted: Dec 18, 2003 6:49 PM
Reply to this message Reply
Oops, that came out even funkier! (the old bug in the Quote Original that does strange stuff on line wraps). Okay, I think this will do the trick:
std::string GetWindowText( HWND wnd )
{
   std::string text;
   WPARAM length = SendMessage( wnd, WM_GETTEXTLENGTH, 0, 0 );
   if( length > 0 )
   {
      char *buffy = new char [length+1];
 
      LRESULT got = SendMessage( wnd, WM_GETTEXT, length+1, (LPARAM)buffy );
      if( (LRESULT)length == got )
         text = buffy;
 
      delete [] buffy;
   }
   return text;
}

Flat View: This topic has 3 replies on 1 page
Topic: Any not banded report engine for C#? Previous Topic   Next Topic Topic: Fourm design

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use