简体   繁体   中英

Get text from an edit control

I tried this:

int editlength;
int buttonid = 3324; // id to button, the numbers dont mean anything
int editid = 5652; // id to edit

LPTSTR  edittxt;

HWND button; // created in wWinmain as a button
HWND edit; // created in wWinMain as an edit control

// LRESULT CALLBACK WindowProc

switch(uMsg)
{
    case WM_COMMAND:
        if(wParam == buttonid)
        {
            filedit = GetDlgItem(hwnd, editid); // I tried with and without this
            editlength = GetWindowTextLength(filedit);
            GetWindowText(filedit, edittxt, editlength);

            MessageBox(hwnd, edittxt, L"edit text", 0);
        }
        break;
}

But I get don't see any text in the message box.

The last argument to GetWindowText() is the size of your buffer. Since you set it to the length of the string, you are telling the function that your buffer is too small because there's no room for the null terminator. And nothing gets copied.

In addition, you must already allocate the buffer to hold the copy of the text. What does edittxt point to? I don't even see where you initialize it.

Correct usage would look something like this:

TCHAR buff[1024];
GetWindowText(hWndCtrl, buff, 1024);

edittxt needs to be a pointer to a buffer that gets the text.. so try this...

char txt[1024];
....
GetWindowText(filedit, txt, sizeof(txt));

You may have to adjust for unicode.. sorry its been a while since I did raw win32.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM