今天学着写了一段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
基本都是字符串无法转换问题,并且添加强制转换后,程序运行出现乱码。
解决方法:项目-->属性-->配置属性-->常规-->字符集-->使用多字节字符集,并且不用加强制转换。
|