一个WINDOWS程序的错误(求助)
在VC的控制台“HELLO”程序中,天加了一些代码,如下:简单的测试键盘的一些功能。但运行时出现下面的错误,我实在改不出了,请大家指点:
:\MyProjects\test4\test4.cpp(190) : error C2043: illegal break
D:\MyProjects\test4\test4.cpp(192) : error C2046: illegal case
D:\MyProjects\test4\test4.cpp(207) : error C2043: illegal break
D:\MyProjects\test4\test4.cpp(209) : error C2046: illegal case
D:\MyProjects\test4\test4.cpp(218) : error C2043: illegal break
D:\MyProjects\test4\test4.cpp(219) : error C2046: illegal case
D:\MyProjects\test4\test4.cpp(225) : error C2043: illegal break
D:\MyProjects\test4\test4.cpp(226) : error C2046: illegal case
D:\MyProjects\test4\test4.cpp(228) : error C2043: illegal break
D:\MyProjects\test4\test4.cpp(229) : error C2046: illegal case
D:\MyProjects\test4\test4.cpp(272) : error C2043: illegal break
D:\MyProjects\test4\test4.cpp(274) : error C2046: illegal case
D:\MyProjects\test4\test4.cpp(285) : error C2043: illegal break
D:\MyProjects\test4\test4.cpp(286) : error C2046: illegal case
D:\MyProjects\test4\test4.cpp(288) : error C2043: illegal break
D:\MyProjects\test4\test4.cpp(289) : error C2047: illegal default
D:\MyProjects\test4\test4.cpp(292) : error C2143: syntax error : missing ';' before 'return'
D:\MyProjects\test4\test4.cpp(293) : error C2143: syntax error : missing ';' before '}'
D:\MyProjects\test4\test4.cpp(293) : error C2143: syntax error : missing ';' before '}'
D:\MyProjects\test4\test4.cpp(293) : error C2143: syntax error : missing ';' before '}'
D:\MyProjects\test4\test4.cpp(296) : error C2143: syntax error : missing ';' before '{'
D:\MyProjects\test4\test4.cpp(296) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.
test4.exe - 22 error(s), 0 warning(s)
long WINAPI WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
const char BufSize=15;
static char cCharBuf[BufSize];
static int nNumChar=0;
static int nArrayPos=0;
static int nLnHeight;
static int nCharWidth;
static int xCaret,yCaret;
int x;
TEXTMETRIC tm;
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_CHAR:
if(wParam==VK_BACK)
{
if(nArrayPos==0)
MessageBox(hWnd,"当前位置是文本的起 始位置,不能会退",NULL,MB_OK);
else
{
nArrayPos=nArrayPos-1;
CaretPos(hWnd,nArrayPos,cCharBuf,&xCaret,&yCaret,nCharWidth);
for(x=nArrayPos;x<nNumChar;x=x+1)
cCharBuf[x]=cCharBuf[x+1];
nNumChar=nNumChar-1; //对现有字符总数进行计数
InvalidateRect(hWnd,NULL,TRUE); //刷新用户区,并向应用程序发送WM_PAINT消息
}
break;
}
if(wParam==VK_ESCAPE)
{
MessageBox(hWnd,"您现在不能按ESC键,请继续其它操作",NULL,MB_OK);
break;
}
if(nNumChar>=BufSize) //如果写入的字符数超过缓冲区大小,则报警
{
MessageBox(hWnd,"缓冲区已满,不能再输入字符了\n若需要删除字符,请用BackSpace键",NULL,MB_OK);
break;
}
for(x=nNumChar;x>nArrayPos;x=x-1)
cCharBuf[x]=cCharBuf[x-1];
cCharBuf[nArrayPos]=(unsigned char)wParam;
nArrayPos=nArrayPos+1;
nNumChar=nNumChar+1;
CaretPos(hWnd,nArrayPos,cCharBuf,&xCaret,&yCaret,nCharWidth);
InvalidateRect(hWnd,NULL,TRUE);
}
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE: //处理窗口创建消息
{
hdc=GetDC(hWnd);
GetTextMetrics(hdc,&tm); //获取字体信息
nLnHeight=tm.tmHeight+tm.tmExternalLeading;
nCharWidth=tm.tmAveCharWidth;
yCaret=nLnHeight;
ReleaseDC(hWnd,hdc);
}
break;
case WM_SETFOCUS: //处理活动焦点消息
{
CreateCaret(hWnd,0,0,nLnHeight);
CaretPos(hWnd,nArrayPos,cCharBuf,&xCaret,&yCaret,nCharWidth);
ShowCaret(hWnd); //在活动焦点的窗口中显示插字符
}
break;
case WM_KILLFOCUS: //处理失去焦点消息
DestroyCaret();
break;
case WM_KEYDOWN: //处理按下键消息
{
switch(wParam)
{
case VK_END: //处理按下键为End时的消息
nArrayPos=nNumChar; //输入位置从本行的末尾开始
CaretPos(hWnd,nArrayPos,cCharBuf,&xCaret,&yCaret,nCharWidth);
break;
case VK_HOME: //处理按下键为Home时的消息
nArrayPos=0; //输入位置为本行的起始位置
CaretPos(hWnd,nArrayPos,cCharBuf,&xCaret,&yCaret,nCharWidth);
break;
case VK_DELETE: //处理按下键为Delete时的消息
if(nArrayPos==nNumChar) //输入位置处于本行的末尾
MessageBox(hWnd,"缓冲区已空,没有字符可供删除",NULL,MB_OK);
else
{
for(x=nArrayPos;x<nNumChar;x=x+1)
cCharBuf[x]=cCharBuf[x+1]; //每删除一个字符,缓冲区中总字符数减1
nNumChar=nNumChar-1;
InvalidateRect(hWnd,NULL,TRUE); //用户区刷新
}
break;
case VK_LEFT: //处理按下左方向键时的消息
if(nArrayPos>0)
{
nArrayPos=nArrayPos-1; //当前输入位置往前移一个位置,再输入字符时,等于插入字符
CaretPos(hWnd,nArrayPos,cCharBuf,&xCaret,&yCaret,nCharWidth);
}
else //已经移到起始输入位置,不能再往前了
MessageBox(hWnd,"您已经移动到起始位置,不能再往左移动了",NULL,MB_OK);
break;
case VK_RIGHT: //处理按下右方向键时的消息
if(nArrayPos<nNumChar) //如果当前位置没有到缓冲区的最后位置,还能向右移动
{
nArrayPos=nArrayPos+1;
CaretPos(hWnd,nArrayPos,cCharBuf,&xCaret,&yCaret,nCharWidth);
}
else
MessageBox(hWnd,"已经到缓冲区的末尾,不能再向右移动了",NULL,MB_OK);
break;
}
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
TextOut(hdc,nCharWidth,nLnHeight,cCharBuf,nNumChar);
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}