无法从Winapi中的编辑控件获取文本 [英] unable to get text from edit control in winapi

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

问题描述

我无法从主窗口中的编辑控件中检索任何文本。我可以设置文本,该文本在绘制窗口时会显示出来,但是我无法获取要在MessageBox中显示的文本。我尝试了 SendMessage()和 GetWindowText(),但是两者都做同样的事情。看来我正在检索的文本长度也是无效的,因此即使我可以看到其中的文本,编辑也没有任何价值。

I'm not able to retrieve any text from the edit control I have on my main window. I can set the text, which does show up when the window is drawn, but I can not get the text, which I would like to display in a MessageBox. I tried both "SendMessage()" and "GetWindowText()" but both do the same thing. It appears the length of text I'm retrieving is also invalid, so the edit has no value even though I can see text in it.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
HWND addCust, editCust1, editCust2;

switch (message) {
case WM_CREATE: {
    addCust = CreateWindow(L"BUTTON",L"addCust",
                   WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
                   140,70,100,25,hWnd,(HMENU)IDC_ADDCUST,NULL,NULL);
    editCust1 = CreateWindowEx(WS_EX_CLIENTEDGE,L"EDIT",L"",
                    WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,
                    50,100,200,20,hWnd,(HMENU)IDC_EDITCUST1,NULL,NULL);
    editCust2 = CreateWindowEx(WS_EX_CLIENTEDGE,L"EDIT",L"",
                    WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,
                    50,130,200,20,hWnd,(HMENU)IDC_EDITCUST2,NULL,NULL);
    SendMessage(editCust1,WM_SETTEXT,NULL,(LPARAM)L"first name");
    SendMessage(editCust2,WM_SETTEXT,NULL,(LPARAM)L"last name");
    break;
}
case WM_COMMAND:
    wmId    = LOWORD(wParam);
    wmEvent = HIWORD(wParam);
    case IDC_ADDCUST: {
        TCHAR buff[64] = { '\0' };
        int len = SendMessage(editCust1, WM_GETTEXTLENGTH, 0, 0);
        SendMessage(editCust1, WM_GETTEXT, len+1, (LPARAM)buff);
        GetWindowText(editCust1, buff, len+1);
        MessageBox(NULL, buff, L"Information", MB_ICONINFORMATION);
        break;
        }
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
default:
    return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}


推荐答案

 HWND addCust, editCust1, editCust2;

这是不可行的,它们是局部变量。在WM_CREATE消息处理程序运行并且WndProc()方法退出之后,它们将失去其值。当您在WM_COMMAND处理程序中再次使用editCust1时,变量包含垃圾。使用调试器btw易于查看。您需要使它们成为全局变量,以便它们保持其值。

That cannot work, these are local variables. They lose their value after the WM_CREATE message handler runs and the WndProc() method exits. The variables contain garbage when you use editCust1 again in the WM_COMMAND handler. Easy to see with a debugger btw. You need to make them global variables so they maintain their values.

buff 声明也是错误的,它只能包含63个字符。当编辑控件实际包含64个字符或更多时,您将破坏堆栈框架并(希望)使程序崩溃。使用malloc()创建足够大的缓冲区。

The buff declaration is wrong as well, it can only contain 63 characters. When the edit control actually contains 64 characters or more you'll corrupt the stack frame and (hopefully) crash your program. Use malloc() to create a buffer that's large enough.

这些是C语言编程陷阱,与Windows api无关。除了慢一点之外,很难给出建议,您确实需要知道C的工作方式,然后Windows才能停止让您头痛的事情。

These are C language programming traps, they have little to do with the Windows api. Hard to give advice beyond "go slower", you do need to know how C works before Windows stops giving you headaches like this.

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

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