我无法从主窗口上的编辑控件中检索任何文本。我可以设置在绘制窗口时显示的文本,但我无法获取我想在 MessageBox 中显示的文本。我尝试了“SendMessage()”和“GetWindowText()”,但都做了同样的事情。看来我正在检索的文本长度也是无效的,因此即使我可以看到其中的文本,编辑也没有任何价值。
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;
}