Solved

Beginner    GetWindowText

Posted on 2000-02-18
5
250 Views
Last Modified: 2008-03-10
I created an edit control with this:

HWND hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"Edit","",(WS_CHILD | WS_VISIBLE ),90, 20,120,20,hWnd,(HMENU)(106),hInstance,NULL);

Is this the best way to create it and how do i get the text from it?
0
Comment
Question by:scooter1
5 Comments
 

Expert Comment

by:kishj
ID: 2537483
scooter1,

it depends on what your goals are and what development tools you are using to create a window. Usually your development ide (MS, Borland, etc.) supply a frameworks which let you create a window by doing a "new" on some type of object that manages and organizes the interface for you.

I would (using OWL from borland) do a new on a TWindow object. MS VC++ or CBuilder would be different in particulars. See your examples that come with your tools.

You can get text from the caption of a window, or from child controls of a window.

To get the caption from a window you can use bare bones windows call like:

int GetWindowText(

    HWND hWnd,      // handle of window or control with text
    LPTSTR lpString,      // address of buffer for text
    int nMaxCount       // maximum number of characters to copy
   );
Parameters

hWnd

Identifies the window or control containing the text.

lpString

Points to the buffer that will receive the text.

nMaxCount

Specifies the maximum number of characters to copy to the buffer. If the text exceeds this limit, it is truncated.

 

Return Values

If the function succeeds, the return value is the length, in characters, of the copied string, not including the terminating null character. If the window has no title bar or text, if the title bar is empty, or if the window or control handle is invalid, the return value is zero. To get extended error information, call GetLastError.
This function cannot retrieve the text of an edit control in another application.

So for your window above you would have:

if (hwnd)// if memory was allocated
{
  if (::IsWindow(hwnd))// if the window was created successfully
  {
  int nCharsReceived = 0;
  int nMaxCharsToReceive=256;
  char szWindowText[257];
    memset(szWindowText,0,257);// initialize your string
    nCharsReceived = ::GetWindowText(hwnd,szWindowText,nMaxCharsToReceive);
    if (nCharsReceived < 1) // error handling or no text was there
    {
      return;
    }
    // handle text here
  }
}
0
 

Author Comment

by:scooter1
ID: 2537515
i'm using VC++ 6.0 but not MFC
lets say i wan't to check like a password.

char buffer[40];
GetWindowText(hwnd,buffer,39);
if (buffer == "thepassword")
{
///dosomething here
}

using hwnd from the edit control above does not seem to work, what's the prob.?

0
 
LVL 5

Expert Comment

by:Wyn
ID: 2537580
Try GetLastError() after the GetWindowText() and find the error.Hwnd should work.
Regards
Wyn
0
 
LVL 1

Accepted Solution

by:
JMu earned 20 total points
ID: 2537807
scooter1, you are making the beginners mistake in comparison. You compare pointers.

Do it this way.

if ( lstrcmp( buffer, "thepassword" ) == 0 )
{
  // OK
}

When you start using C++ and CString class, you can compare strings the way you just did.

And, please call GetWindowText( hwnd, buffer, sizeof(buffer) );

Windows functions usually "wants" the whole buffer size. You are telling that there are 40 bytes available. So, you get maximum of 39 characters. In your example, you only get max. of 38 chars.

JMu
0
 

Author Comment

by:scooter1
ID: 2539108
ahh, muchas gracias
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 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