?
Solved

Beginner    GetWindowText

Posted on 2000-02-18
5
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 80 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

741 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