我的问题很简单(至少我认为是这样)。我想从文本输入中捕获一个值并将其存储在txt文件中。现在我正在使用此代码,操作按钮用于在单击时将内容写入文件。但我得到了在文件中注册的按钮的值。
case WM_CREATE:{
CreateWindow(
TEXT("EDIT"), TEXT("value"),
WS_VISIBLE | WS_CHILD | WS_BORDER,
190, 50, 50, 20,
hwnd, (HMENU) NULL, NULL, NULL
);
CreateWindow(
TEXT("BUTTON"), TEXT("Ok"),
WS_VISIBLE | WS_CHILD,
250, 10, 30, 20,
hwnd, (HMENU) ID_BTN, NULL, NULL
);
break;
}
case WM_COMMAND: {
if (LOWORD(wParam) == ID_BTN) {
std::ofstream outfile;
outfile.open("C:\\file.txt", std::ios_base::app);
outfile << ID_BTN;
outfile.close();
MessageBox(hwnd, "Done!", "Title", MB_ICONINFORMATION);
return 0;
}
break;
}
感谢。
编辑:@ ZanLynx,我试着做你所说的,但编译器一直说hwndText没有声明,当它是。
107 `hwndText' undeclared (first use this function)
这是代码
#define ID_BTN 1
#define ID_TXT 2
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
HWND hwndText = CreateWindow(
TEXT("Edit"), TEXT("Write here"),
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
190, 10, 100, 20,
hwnd, (HMENU) ID_TXT, NULL, NULL
);
CreateWindow(
TEXT("BUTTON"), TEXT("OK"),
WS_VISIBLE | WS_CHILD,
250, 10, 30, 20,
hwnd, (HMENU) ID_BTN, NULL, NULL
);
break;
}
case WM_COMMAND:
{
if (LOWORD(wParam) == ID_BTN)
{
LRESULT iTextSize = SendMessage(hwndText, EM_GETLIMITTEXT, 0, 0);
char *szText = new char[iTextSize];
SendMessage(hwndText, WM_GETTEXT, iTextSize, (LPARAM)szText);
std::ofstream outfile;
outfile.open("C:\\f.txt", std::ios_base::app);
outfile << szText;
outfile.close();
MessageBox(hwnd, "Done!", "Title", MB_ICONINFORMATION);
return 0;
};
break;
}
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}