主题:求助
我刚开始学API,遇到了一个问题
#include<windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
MSG Msg;
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName="WinKeyboard";
if(!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
}
hWnd=CreateWindow(
"WinKeyboard",
"键盘操作例程",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&Msg, NULL, 0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
#define BufSize 15 //数组长度
LONG WINAPI WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
static char cCharBuf[BufSize]; //字符数组
static int nNumChar = 0; //字符个数
static int nArrayPos = 0; //脱字符号位置
static int nLnHeight; //行高
static int nCharWidth; //字符平均宽度
int x;
HDC hDC;
TEXTMETRIC tm; //字体信息的结构
PAINTSTRUCT PtStr;
switch (iMessage)
{
case WM_LBUTTONDOWN:
MessageBox(hWnd, "鼠标已坏,请更换", NULL, MB_ABORTRETRYIGNORE);
break;
case WM_CHAR:
switch(wParam)
{
case 0x08: //退格
if(nArrayPos == 0) //脱字符号已处于最前面
MessageBox(hWnd, "当前位置是文本的起始位置,不能回退", NULL, MB_OK);
else
{
nArrayPos--; //脱字符号前移一位
nNumChar--; //字符数减1
InvalidateRect(hWnd, NULL, TRUE);
}
break;
case 0x1B: //Esc
MessageBox(hWnd, "您现在不能按Esc键,请继续其他操作", NULL, MB_OK);
break;
/*
case 0x0A: ...; break; //换行
case 0x09: ...; break; //Tab
case 0x0D: ...; break; //回车
*/
default: //其他可显示字符
if(nNumChar >= BufSize) //字符数>=数组长度
{
MessageBox(hWnd, "缓冲区已满,不能再输入字符了\n若需要删除字符,请用BackSpace键", NULL, MB_OK);
break;
}
//如果脱字符号不在串尾,脱字符号后面的字符都向后移一位
for(x=nNumChar; x>nArrayPos; x--)
cCharBuf[nArrayPos] = cCharBuf[x-1];
//插入字符
cCharBuf[nArrayPos] = (unsigned char)wParam;
nArrayPos++; //脱字符号后移
nNumChar++; //字符数+1
InvalidateRect(hWnd, NULL, TRUE);
}
break;
case WM_CREATE:
hDC = GetDC(hWnd);
GetTextMetrics(hDC, &tm);
nLnHeight = tm.tmHeight+tm.tmExternalLeading;
nCharWidth = tm.tmAveCharWidth;
ReleaseDC(hWnd, hDC);
break;
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_END:
nArrayPos = nNumChar;
break;
case VK_HOME:
nArrayPos = 0;
break;
case VK_DELETE:
if(nArrayPos == nNumChar) //脱字符号在串尾
MessageBox(hWnd, "缓冲区已空,没有字符可供删除", NULL, MB_OK);
else
{
//从脱字符号后面第2个字符开始依次前移一位
for(x=nArrayPos; x<nNumChar; x++)
cCharBuf[x] = cCharBuf[x+1];
nNumChar--; //字符数减1
InvalidateRect(hWnd, NULL, TRUE);
}
break;
case VK_LEFT:
if(nArrayPos>0)
nArrayPos--;
else
MessageBox(hWnd, "您已经移动到起始位置,不能再往左移动了", NULL, MB_OK);
break;
case VK_RIGHT:
if(nArrayPos < nNumChar)
nArrayPos++;
else
MessageBox(hWnd, "已经到缓冲区的末尾,不能再向右移动了", NULL, MB_OK);
break;
}
}
break;
case WM_PAINT:
hDC = BeginPaint(hWnd, &PtStr);
TextOut(hDC, nCharWidth, nLnHeight, cCharBuf, nNumChar);
EndPaint(hWnd, &PtStr);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, iMessage, wParam, lParam);
}
return NULL;
}
这个程序在运行后,出现了这样一个问题:在WM_LBUTTONDOWN消息处理时如果不加BREAK就会在运行时输出一个英镑符号,但是加了BREAK后就 没 这个问题了,这是为什么呢?
#include<windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
MSG Msg;
WNDCLASS wndclass;
wndclass.style=CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName="WinKeyboard";
if(!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
}
hWnd=CreateWindow(
"WinKeyboard",
"键盘操作例程",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&Msg, NULL, 0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
#define BufSize 15 //数组长度
LONG WINAPI WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
static char cCharBuf[BufSize]; //字符数组
static int nNumChar = 0; //字符个数
static int nArrayPos = 0; //脱字符号位置
static int nLnHeight; //行高
static int nCharWidth; //字符平均宽度
int x;
HDC hDC;
TEXTMETRIC tm; //字体信息的结构
PAINTSTRUCT PtStr;
switch (iMessage)
{
case WM_LBUTTONDOWN:
MessageBox(hWnd, "鼠标已坏,请更换", NULL, MB_ABORTRETRYIGNORE);
break;
case WM_CHAR:
switch(wParam)
{
case 0x08: //退格
if(nArrayPos == 0) //脱字符号已处于最前面
MessageBox(hWnd, "当前位置是文本的起始位置,不能回退", NULL, MB_OK);
else
{
nArrayPos--; //脱字符号前移一位
nNumChar--; //字符数减1
InvalidateRect(hWnd, NULL, TRUE);
}
break;
case 0x1B: //Esc
MessageBox(hWnd, "您现在不能按Esc键,请继续其他操作", NULL, MB_OK);
break;
/*
case 0x0A: ...; break; //换行
case 0x09: ...; break; //Tab
case 0x0D: ...; break; //回车
*/
default: //其他可显示字符
if(nNumChar >= BufSize) //字符数>=数组长度
{
MessageBox(hWnd, "缓冲区已满,不能再输入字符了\n若需要删除字符,请用BackSpace键", NULL, MB_OK);
break;
}
//如果脱字符号不在串尾,脱字符号后面的字符都向后移一位
for(x=nNumChar; x>nArrayPos; x--)
cCharBuf[nArrayPos] = cCharBuf[x-1];
//插入字符
cCharBuf[nArrayPos] = (unsigned char)wParam;
nArrayPos++; //脱字符号后移
nNumChar++; //字符数+1
InvalidateRect(hWnd, NULL, TRUE);
}
break;
case WM_CREATE:
hDC = GetDC(hWnd);
GetTextMetrics(hDC, &tm);
nLnHeight = tm.tmHeight+tm.tmExternalLeading;
nCharWidth = tm.tmAveCharWidth;
ReleaseDC(hWnd, hDC);
break;
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_END:
nArrayPos = nNumChar;
break;
case VK_HOME:
nArrayPos = 0;
break;
case VK_DELETE:
if(nArrayPos == nNumChar) //脱字符号在串尾
MessageBox(hWnd, "缓冲区已空,没有字符可供删除", NULL, MB_OK);
else
{
//从脱字符号后面第2个字符开始依次前移一位
for(x=nArrayPos; x<nNumChar; x++)
cCharBuf[x] = cCharBuf[x+1];
nNumChar--; //字符数减1
InvalidateRect(hWnd, NULL, TRUE);
}
break;
case VK_LEFT:
if(nArrayPos>0)
nArrayPos--;
else
MessageBox(hWnd, "您已经移动到起始位置,不能再往左移动了", NULL, MB_OK);
break;
case VK_RIGHT:
if(nArrayPos < nNumChar)
nArrayPos++;
else
MessageBox(hWnd, "已经到缓冲区的末尾,不能再向右移动了", NULL, MB_OK);
break;
}
}
break;
case WM_PAINT:
hDC = BeginPaint(hWnd, &PtStr);
TextOut(hDC, nCharWidth, nLnHeight, cCharBuf, nNumChar);
EndPaint(hWnd, &PtStr);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, iMessage, wParam, lParam);
}
return NULL;
}
这个程序在运行后,出现了这样一个问题:在WM_LBUTTONDOWN消息处理时如果不加BREAK就会在运行时输出一个英镑符号,但是加了BREAK后就 没 这个问题了,这是为什么呢?