一个windows程序,对字符编码不熟悉,求指教
程序代码:
#include<windows.h> #include<tchar.h> LRESULT CALLBACK WndProc(HWND hWnd , UINT msg , WPARAM wParam , LPARAM lParam); //声明回调函数; const TCHAR szWindowClass[] = _T("第一个窗口"); const TCHAR szWindowTitle[] = _T("主窗口标题"); int WINAPI _tWinMain (HINSTANCE hInstance , HINSTANCE hPreInstance , LPTSTR lpCmdLine , int nCmdShow) { WNDCLASSEX wc = {0}; //初始化; wc.cbSize = sizeof(wc); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WndProc; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL , IDI_APPLICATION); //使用系统默认的图标; wc.hCursor = LoadCursor(NULL , IDC_ARROW); //使用系统默认的光标; wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //白色画刷; wc.lpszClassName = szWindowClass; RegisterClassEx(&wc); //注册窗口; HWND hWnd = CreateWindow(szWindowClass , szWindowTitle , WS_OVERLAPPEDWINDOW , 100 , 200 , 500 , 500 , HWND_DESKTOP , NULL , hInstance , NULL); if(!hWnd) return FALSE; //如果窗口创建失败则退出; //显示并更新窗口 ShowWindow(hWnd , nCmdShow); //nCmdShow是显示方式; UpdateWindow(hWnd); MSG msg; while(GetMessage(&msg , NULL , 0 , 0)) { TranslateMessage(&msg); //转换消息; DispatchMessage(&msg); //将消息发送到窗口函数; } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd , UINT msg , WPARAM wParam , LPARAM lParam) { static wstring str; //这里出错了,求解决; switch(msg) { case WM_CHAR: { switch(wParam) //虚拟键码; { case 8: //处理backspace if(str.size() == 0) return 0; //如果字符串为空,则直接返回; str.erase(str.size() - 1 , 1); //删除string的最后一个字符; break; default : str + = TCHAR(wParam); } InvalidateRect(hWnd , NULL , TRUE); //刷新窗口;NULL代表刷新整个区域; return 0; } case WM_DESTROY : //销毁窗口; PostQuitMessage(0); return 0; case WM_PAINT: { //TCHAR *str = _T("Hello World!"); PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd , &ps); //把ps中的无效区域清空; RECT r; GetClientRect(hWnd , &r); //获取客户端的矩形框 SetTextColor(hdc , RGB(255 , 0 , 0)); //hdc是图形设备上下文,str是要输出的字符串,-1代表输出整个字符串,&r代表输出的矩形区域, DrawText(hdc , str.c_str() , -1 , &r , DT_WORDBREAK | DT_LEFT | DT_TOP); EndPaint(hWnd , &ps); return 0; } default : return DefWindowProc(hWnd , msg , wParam , lParam); //系统处理这些消息 } }
错误信息:
程序代码:
1>e:\程序设计\win32project1\win32project1\源.cpp(39): error C2146: 语法错误: 缺少“;”(在标识符“str”的前面) 1>e:\程序设计\win32project1\win32project1\源.cpp(39): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int 1>e:\程序设计\win32project1\win32project1\源.cpp(39): error C2065: “str”: 未声明的标识符 1>e:\程序设计\win32project1\win32project1\源.cpp(47): error C2065: “str”: 未声明的标识符 1>e:\程序设计\win32project1\win32project1\源.cpp(47): error C2228: “.size”的左边必须有类/结构/联合 1> 类型是“'unknown-type'” 1>e:\程序设计\win32project1\win32project1\源.cpp(48): error C2065: “str”: 未声明的标识符 1>e:\程序设计\win32project1\win32project1\源.cpp(48): error C2228: “.erase”的左边必须有类/结构/联合 1> 类型是“'unknown-type'” 1>e:\程序设计\win32project1\win32project1\源.cpp(48): error C2228: “.size”的左边必须有类/结构/联合 1> 类型是“'unknown-type'” 1>e:\程序设计\win32project1\win32project1\源.cpp(51): error C2065: “str”: 未声明的标识符 1>e:\程序设计\win32project1\win32project1\源.cpp(68): error C2065: “str”: 未声明的标识符 1>e:\程序设计\win32project1\win32project1\源.cpp(68): error C2228: “.c_str”的左边必须有类/结构/联合 1> 类型是“'unknown-type'”
求指教一下我该如何修改,实在不知该怎么做了!谢谢了
[ 本帖最后由 笑傲 于 2013-6-23 18:16 编辑 ]