从编辑控件获取文本 [英] Get text from an edit control

查看:27
本文介绍了从编辑控件获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过了:

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.

推荐答案

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

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.

此外,您必须已经分配缓冲区来保存文本副本.edittxt 指向什么?我什至看不到你在哪里初始化它.

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);

这篇关于从编辑控件获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆