【问题标题】:Get text from an edit control从编辑控件中获取文本
【发布时间】:2011-04-17 16:14:42
【问题描述】:

我试过这个:

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

但我在消息框中看不到任何文字。

【问题讨论】:

    标签: winapi


    【解决方案1】:

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

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

    正确的用法应该是这样的:

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

    【讨论】:

      【解决方案2】:

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

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

      您可能需要针对 unicode 进行调整。抱歉,我已经有一段时间没有使用原始 win32 了。

      【讨论】:

      • 谢谢,但我认为接受的答案应该交给@jonathan,因为他先进来了:)
      猜你喜欢
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多