从编辑控件中获取文本

Geo*_*Shg 6 winapi

我试过这个:

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;
}
Run Code Online (Sandbox Code Playgroud)

但我没有在消息框中看到任何文字.

Jon*_*ood 14

最后一个参数GetWindowText()是缓冲区的大小.由于您将其设置为字符串的长度,因此您告诉函数您的缓冲区太小,因为空终止符没有空间.没有任何东西被复制.

此外,您必须已分配缓冲区以保存文本的副本.有什么edittxt意义?我甚至没有看到你在哪里初始化它.

正确的用法看起来像这样:

TCHAR buff[1024];
GetWindowText(hWndCtrl, buff, 1024);
Run Code Online (Sandbox Code Playgroud)


Jim*_*ris 5

edittxt需要是一个指向获取文本的缓冲区的指针..所以试试这个......

char txt[1024];
....
GetWindowText(filedit, txt, sizeof(txt));
Run Code Online (Sandbox Code Playgroud)

你可能需要调整unicode ..对不起它已经有一段时间了,因为我做了原始的win32.