所以基本上我正在尝试从文本字段中获取文本,因为我知道你从 GetWindowText 得到指针我是对的吗?我无法将该文本放入long char中,所以我不得不使用int而消息框不会占用整数,因此我必须转换为long char然后反转指针(&)?在消息框中显示文本,这只是打印了一堆废话
案例WM_CREATE:
textbox1 = CreateWindow(L"EDIT",
L"X0", WS_BORDER | WS_CHILD | WS_VISIBLE,
50, 120, 50, 20,
hwnd, NULL, NULL, NULL);
案例WM_COMMAND:
case 111:{
TCHAR buff[1024];
int text = GetWindowText(textbox1, buff, 1024);
TCHAR textS = (TCHAR)text;
MessageBox(hwnd, &textS, &textS, MB_OKCANCEL | MB_ICONEXCLAMATION);}
答案 0 :(得分:1)
仅使用(wchar_t / TCHAR)缓冲区:
int cTextLength; // text length
cTextLength = GetWindowTextLength(hWndEdit);// get text length
wchar_t * textS = new wchar_t[cTextLength + 1]; //dynamically allocate buffer
// get text from an edit and store it into a buffer variable
GetWindowText(hWndEdit, textS, cTextLength + 1);
// display the message
MessageBox(NULL, textS, textS, MB_OKCANCEL | MB_ICONEXCLAMATION);
delete[] textS; // free the allocated memory
答案 1 :(得分:1)
GetWindowText()
的返回值是复制文本的长度(不包括空终止符)。您将该值类型转换为单个TCHAR
字符,然后将该单个字符的内存地址传递给MessageBox()
。这是完全错误的。 MessageBox()
期望指向以空字符结尾的字符串,因此您应该传递复制的TCHAR[]
缓冲区:
TCHAR buff[1024] = {0};
GetWindowText(textbox1, buff, 1024);
MessageBox(hwnd, buff, TEXT("text"), MB_OKCANCEL | MB_ICONEXCLAMATION);
或者,使用动态分配的缓冲区:
int len = GetWindowTextLength(textbox1) + 1;
TCHAR *buff = new TCHAR[len];
len = GetWindowText(textbox1, buff, len);
buff[len] = 0;
MessageBox(hwnd, buff, TEXT("text"), MB_OKCANCEL | MB_ICONEXCLAMATION);
delete[] buff;