Link to home
Start Free TrialLog in
Avatar of scooter1
scooter1

asked on

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?
Avatar of kishj
kishj

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
  }
}
Avatar of scooter1

ASKER

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.?

Try GetLastError() after the GetWindowText() and find the error.Hwnd should work.
Regards
Wyn
ASKER CERTIFIED SOLUTION
Avatar of JMu
JMu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ahh, muchas gracias