• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Beginner GetWindowText

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
scooter1
Asked:
scooter1
1 Solution
 
kishjCommented:
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
 
scooter1Author Commented:
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
 
WynCommented:
Try GetLastError() after the GetWindowText() and find the error.Hwnd should work.
Regards
Wyn
0
 
JMuCommented:
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
 
scooter1Author Commented:
ahh, muchas gracias
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now