看孙鑫的视频写的代码,可是怎么编译出错啊??
我用的vs2010
代码如下:
#include <Windows.h>#include <tchar.h>#include <wchar.h>#include <stdio.h>LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam);int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd ){ WNDCLASS wndclass; static TCHAR szWndName[]=TEXT("MyClass"); wndclass.style=CS_VREDRAW|CS_HREDRAW; wndclass.lpszClassName=szWndName; wndclass.lpszMenuName=NULL; wndclass.lpfnWndProc=WinProc; wndclass.hInstance=hInstance; wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndclass.hCursor=LoadCursor(NULL,IDI_APPLICATION); wndclass.hbrBackground=(HBRUSH)(COLOR_WINDOW+1); wndclass.cbClsExtra=0; wndclass.cbWndExtra=0; //if (!RegisterClass(&wndclass)) //{ // MessageBox(NULL,_T("Register Class Failed!"),_T("Waring!"),MB_OKCANCEL); // return 1; //} RegisterClass(&wndclass); HWND hwnd=CreateWindow(szWndName,_T("The Hello World Application"),WS_OVERLAPPEDWINDOW/*&~WS_MAXIMIZEBOX*/,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,hInstance,0); ShowWindow(hwnd,SW_SHOWNORMAL); UpdateWindow(hwnd); MSG msg; while (GetMessage(&msg,0,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam;}LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam){ HDC hdc; PAINTSTRUCT ps; switch(message) { case WM_PAINT: hdc=BeginPaint(hwnd,&ps); LPCWSTR szText=TEXT("Hello World"); TextOut(hdc,0,0,szText,wcslen(szText)); EndPaint(hwnd,&ps); break; case WM_CHAR: char buffer[10]; sprintf(buffer,"char is %d",wparam); MessageBox(hwnd,LPCWSTR(buffer),TEXT("haolly"),MB_OKCANCEL); break; case WM_LBUTTONDOWN: MessageBox(hwnd,TEXT("Mouse Clicked"),TEXT("Message"),0); HDC hDc=GetDC(hwnd); TextOut(hDc,0,50,TEXT("Hello World2"),strlen("Hello World2")); ReleaseDC(hwnd,hDc); break; case WM_CLOSE: if (IDYES==MessageBox(hwnd,TEXT("Are you sure?"),TEXT("message"),0)) { DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,message,wparam,lparam); } return 0;}
1> d:\visual studio\project\sdk1_0\sdk1_0\winmain.cpp(57) : 参见“szText”的声明
1>d:\visual studio\project\sdk1_0\sdk1_0\winmain.cpp(81): error C2361: “default”标签跳过“hDc”的初始化操作
1> d:\visual studio\project\sdk1_0\sdk1_0\winmain.cpp(68) : 参见“hDc”的声明
1>d:\visual studio\project\sdk1_0\sdk1_0\winmain.cpp(81): error C2361: “default”标签跳过“szText”的初始化操作
1> d:\visual studio\project\sdk1_0\sdk1_0\winmain.cpp(57) : 参见“szText”的声明
1>
1>生成失败。
1>
1>已用时间 00:00:00.97
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
[解决办法]
case 语句中包含变量的声明时,应该用大括号
case 1:
{
char szLine[10] = {0};
}
break;
这种形式的
[解决办法]
case语句后面加上{}
[解决办法]
你的设置不对。
WinMain应该设置:
"Preprocessor Definition-> WIN32,_DEBUG,_WINDOWS,_AFXDLL,_MBCS"
link:
/subsystem:windows
[解决办法]
在Setting中的Link标签页中的Project Options中找到/subsystem:console,将console改为windows