如何将文本从编辑框复制到字符串数组中?

How to copy the text from editbox into a string array?

我正在 visual studio 2012.I 上制作一个 win32 api window 应用程序项目,使用 LPWSTR 类型变量来存储我的字符串,如下所述。

LPWSTR MyStringList[3]={L"apple",L"orange",L"watermelon"};

我希望将文本从编辑框复制到 MyStringList 中的字符串之一。因此,我做了一个简单的编辑框和按钮。

这里是编辑框和按钮的定义。

case WM_CREATE:
    hEdit = CreateWindow(L"EDIT", 
            L"", 
            WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL, 
            10, 10, 200, 25, 
            hWnd, 
            (HMENU)ID_EDIT, 
            GetModuleHandle(NULL), 
            NULL);
    hBtn = CreateWindow(L"BUTTON", 
            L"", 
            WS_CHILD | WS_VISIBLE, 
            250, 10, 50, 30, 
            hWnd, 
            (HMENU)ID_BUTTON, 
            GetModuleHandle(NULL), 
            NULL);

这是当按钮为 pushed.The 时的动作 pushed.The 两个消息框用于在调用函数 GetWindowText 后查看字符串是否已更改。

case WM_COMMAND:
    wmId    = LOWORD(wParam);
    wmEvent = HIWORD(wParam);
    switch (wmId)
    {
    case ID_BUTTON:

        MessageBox(hWnd,MystringList[1],L"Before_Pusing",MB_OK);

        GetWindowText(hEdit,MystringList[1],sizeof(MystringList[1]));

        MessageBox(hWnd,MystringList[1],L"After_Pushing",MB_OK);

        InvalidateRect(hWnd,NULL,TRUE);
        break;

现在我尝试在编辑框中输入 "banana" 后按下按钮。第二个字符串 "orange" 应替换为 "banana"。然而,事实证明什么都没有改变。第二个消息框显示 "orange" 和第一个消息框一样。我的代码有什么问题?请帮忙!非常感谢!

您可以试试这个,添加 Windows 需要的任何编程怪癖。

#define MYLEN 20
...
char MyStringList[3][MYLEN+1] = {"apple", "orange", "watermelon"};

然后您可以使用

从编辑框中获取字符串
GetWindowText(hEdit,MystringList[1],MYLEN);

尽管 GetWindowText() 文档说

"Copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application."