今天學著寫了一段C++程式,有一些問題,下面將問題與解決方案記錄如下:#include <windows.h> #include <stdio.h>LRESULT CALLBACK CallWindowProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { WNDCLASS wndcls; wndcls.cbClsExtra = 0; wndcls.cbWndExtra = 0; wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wndcls.hCursor = LoadCursor(NULL, IDC_ARROW); wndcls.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndcls.hInstance = hInstance; wndcls.lpfnWndProc = CallWindowProc; wndcls.lpszClassName = "ZhengZhixin1983"; wndcls.lpszMenuName = NULL; wndcls.style = CS_HREDRAW | CS_VREDRAW; RegisterClass(&wndcls);HWND hwnd; hwnd = CreateWindow("ZhengZhixin1983","ZhengZhixin1983",WS_OVERLAPPEDWINDOW, 0,0,600,600,NULL,NULL,hInstance,NULL);ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd);MSG msg; while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); }return 0; }LRESULT CALLBACK CallWindowProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ) { switch(Msg) { case WM_CHAR: char szChar[20]; sprintf(szChar,"Char is %d",wParam); MessageBox(hWnd,szChar,"ZhengZhiXin1983",0); break; case WM_LBUTTONDOWN: MessageBox(hWnd,"Mouse Click","ZhengZhiXin1983",0); HDC hdc; hdc = GetDC(hWnd); TextOut(hdc,0,50,"ZhengZhixin1983",strlen("ZhengZhixin1983")); ReleaseDC(hWnd,hdc); break; case WM_PAINT: HDC hDC; PAINTSTRUCT ps; hDC = BeginPaint(hWnd,&ps); TextOut(hdc,0,0,"ZhengZhiXin1983",strlen("ZhengZhiXin1983")); EndPaint(hWnd,&ps); break; case WM_CLOSE: if(IDYES == MessageBox(hWnd,"是否退出?","ZhengZhiXin1983",MB_YESNO)) DestroyWindow(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,Msg,wParam,lParam);} return 0; }編譯後出現問題:d:\vc++_lesson1\winmain\winmain\winmain.cpp(28) : error C2440: '=' : cannot convert from 'const char [16]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\vc++_lesson1\winmain\winmain\winmain.cpp(35) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [16]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\vc++_lesson1\winmain\winmain\winmain.cpp(63) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char [20]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\vc++_lesson1\winmain\winmain\winmain.cpp(66) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [12]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\vc++_lesson1\winmain\winmain\winmain.cpp(69) : error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char [16]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\vc++_lesson1\winmain\winmain\winmain.cpp(76) : error C2664: 'TextOutW' : cannot convert parameter 4 from 'const char [16]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\vc++_lesson1\winmain\winmain\winmain.cpp(80) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [11]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast基本都是字串無法轉換問題,並且添加強制轉換後,程式運行出現亂碼。解決方案:項目-->屬性-->配置屬性-->常規-->字元集-->使用多位元組字元集,並且不用加強制轉換。 |