简体   繁体   English

从编辑控件中读取文本

[英]read text from edit control

I want to read the text which the user has typed in a Edit control. 我想阅读用户在“编辑”控件中键入的文本。 after entering the text and pressing the button, I want to get the text and add it as an item into a comboBox. 输入文本并按下按钮后,我要获取文本并将其作为项目添加到comboBox中。 this is what I am doing in WM_COMMAND of the parent dialog: 这是我在父对话框的WM_COMMAND中所做的事情:

case WM_COMMAND:

    if(HIWORD(wParam) == BN_CLICKED)
    {
        if ((HWND)lParam == Button[0])
        {



                int len = GetWindowTextLengthW(Button[2]) + 1;
                GetWindowTextW(Button[2], text, len);
                SendMessage(Button[1],(UINT) CB_ADDSTRING,(WPARAM) 0,(LPARAM) text);

                }
        }


    return 0;

but things goes wrong, sometime I get NULL in the "text" variable, sometimes just the first character of the string the user has entered and sometime weird ASCII like characters. 但是出了问题,有时候我在“文本”变量中得到NULL ,有时只是用户输入的字符串的第一个字符,有时又像字符一样奇怪的ASCII。 what am I doing wron? 我在做什么? any ideas ? 有任何想法吗 ?

You need to allocate memory for the string. 您需要为字符串分配内存。 Here's how one would expect to do it in C++03: 这是人们期望在C ++ 03中实现的方式:

std::vector<wchar_t> str(len);
GetWindowTextW(Button[2], &str[0], str.size());
SendMessageW(Button[1], CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(&str[0]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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