孙鑫《vc++教程》代码在vs2005中无法运行
里面的源代码直接运行没有错误。。
但我把源代码复制。然后新建win32控制台程序-windows程序-空项目
确无法编译:
=============================================================
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName= "Weixin2003 ";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd=CreateWindow( "Weixin2003 ", "北京维新科学技术培训中心 ",WS_OVERLAPPEDWINDOW,
0,0,600,400,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 WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar, "char is %d ",wParam);
MessageBox(hwnd,szChar, "weixin ",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd, "mouse clicked ", "weixin ",0);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,0,50, "计算机编程语言培训 ",strlen( "计算机编程语言培训 "));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,0,0, "维新培训 ",strlen( "维新培训 "));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd, "是否真的结束? ", "weixin ",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
------ 已启动生成: 项目: fsfs, 配置: Debug Win32 ------
正在编译...
saf.cpp
c:\documents and settings\hzq\my documents\visual studio 2005\projects\fsfs\fsfs\saf.cpp(26) : error C2440: '= ' : cannot convert from 'const char [11] ' to 'LPCWSTR '
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\hzq\my documents\visual studio 2005\projects\fsfs\fsfs\saf.cpp(33) : error C2664: 'CreateWindowExW ' : 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
c:\documents and settings\hzq\my documents\visual studio 2005\projects\fsfs\fsfs\saf.cpp(59) : 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
c:\documents and settings\hzq\my documents\visual studio 2005\projects\fsfs\fsfs\saf.cpp(62) : error C2664: 'MessageBoxW ' : cannot convert parameter 2 from 'const char [14] ' to 'LPCWSTR '
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\hzq\my documents\visual studio 2005\projects\fsfs\fsfs\saf.cpp(65) : error C2664: 'TextOutW ' : cannot convert parameter 4 from 'const char [19] ' to 'LPCWSTR '
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\hzq\my documents\visual studio 2005\projects\fsfs\fsfs\saf.cpp(72) : error C2664: 'TextOutW ' : cannot convert parameter 4 from 'const char [9] ' to 'LPCWSTR '
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\hzq\my documents\visual studio 2005\projects\fsfs\fsfs\saf.cpp(76) : error C2664: 'MessageBoxW ' : cannot convert parameter 2 from 'const char [15] ' to 'LPCWSTR '
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
生成日志保存在“file://c:\Documents and Settings\Hzq\My Documents\Visual Studio 2005\Projects\fsfs\fsfs\Debug\BuildLog.htm”
fsfs - 7 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
[解决办法]
代码在VC6中没问题.
因为它的代码没有按可编译为UNICODE版本的方式写,而VS2005默认是UNICODE编码的.所以所有使用到字串的地方都会出错.
方法1:修改VS2005的设置
方法2:修改代码.字串前加TEXT宏.如
hwnd=CreateWindow(TEXT( "Weixin2003 "),
TEXT( "北京维新科学技术培训中心 "),WS_OVERLAPPEDWINDOW,
0,0,600,400,NULL,NULL,hInstance,NULL);
WinMain改为_tWinMain等.
[解决办法]
选择你的工程-》属性-》常规-》设置为不使用unicode