用VC++实现键盘响应功能
功能:设计一窗口,单击键盘上的向上箭头时,窗口中显示"YouhadhittedtheUPkey";单击Shift键时,窗口中显示"YouhadhittedtheSHIFT...
功能:设计一窗口,
单击键盘上的向上箭头时,窗口中显示"You had hitted the UP key";
单击Shift键时,窗口中显示"You had hitted the SHIFT key";
单击Ctrl键时,窗口中显示"You had hitted the CTRL key";
可程序总是出错,希望大侠们帮帮忙啊~~~~~
现程序WndProc部分如下:
long WINAPI WndProc(HWND hWnd,UINT message,UINT wParam,LONG lParam)
{
#define BufSize 30
static char lp_paint[BufSize];
static int nNumChar=0;
static int nArrayPos=0;
static int nLnHeight;
static int nCharWidth;
int x;
TEXTMETRIC tm;
PAINTSTRUCT PtStr;
HDC hDC;
char lp_1[]="You had hitted the UP key";
char lp_2[]="You had hitted the SHIFT key";
char lp_3[]="You had hitted the CTRL key";
switch(message)
{
case WM_CHAR:
{
if(wParam==VK_UP)
{ lp_paint=lp_1;
nNumChar=nNumChar-1;
InvalidateRect(hWnd,NULL,TRUE);
break;
}
if(wParam==VK_SHIFT)
{ lp_paint=lp_2;
nNumChar=nNumChar-1;
InvalidateRect(hWnd,NULL,TRUE);
break;
}
if(wParam==VK_CONTROL)
{ lp_paint=lp_2;
nNumChar=nNumChar-1;
InvalidateRect(hWnd,NULL,TRUE);
break;
}
for(x=nNumChar;x>nArrayPos;x=x-1)
lp_paint[x]=lp_paint[x-1];
lp_paint[nArrayPos]=(unsigned char)wParam;
nArrayPos=nArrayPos+1;
nNumChar=nNumChar+1;
InvalidateRect(hWnd,NULL,TRUE);
}
break;
case WM_CREATE:
{
hDC=GetDC(hWnd);
GetTextMetrics(hDC,&tm);
nLnHeight=tm.tmHeight+tm.tmExternalLeading;
ReleaseDC(hWnd,hDC);
}
break;
case WM_PAINT:
hDC=BeginPaint(hWnd,&PtStr);
TextOut(hDC,0,nLnHeight,lp_paint,strlen(lp_paint));
EndPaint(hWnd,&PtStr);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return(0);
} 展开
单击键盘上的向上箭头时,窗口中显示"You had hitted the UP key";
单击Shift键时,窗口中显示"You had hitted the SHIFT key";
单击Ctrl键时,窗口中显示"You had hitted the CTRL key";
可程序总是出错,希望大侠们帮帮忙啊~~~~~
现程序WndProc部分如下:
long WINAPI WndProc(HWND hWnd,UINT message,UINT wParam,LONG lParam)
{
#define BufSize 30
static char lp_paint[BufSize];
static int nNumChar=0;
static int nArrayPos=0;
static int nLnHeight;
static int nCharWidth;
int x;
TEXTMETRIC tm;
PAINTSTRUCT PtStr;
HDC hDC;
char lp_1[]="You had hitted the UP key";
char lp_2[]="You had hitted the SHIFT key";
char lp_3[]="You had hitted the CTRL key";
switch(message)
{
case WM_CHAR:
{
if(wParam==VK_UP)
{ lp_paint=lp_1;
nNumChar=nNumChar-1;
InvalidateRect(hWnd,NULL,TRUE);
break;
}
if(wParam==VK_SHIFT)
{ lp_paint=lp_2;
nNumChar=nNumChar-1;
InvalidateRect(hWnd,NULL,TRUE);
break;
}
if(wParam==VK_CONTROL)
{ lp_paint=lp_2;
nNumChar=nNumChar-1;
InvalidateRect(hWnd,NULL,TRUE);
break;
}
for(x=nNumChar;x>nArrayPos;x=x-1)
lp_paint[x]=lp_paint[x-1];
lp_paint[nArrayPos]=(unsigned char)wParam;
nArrayPos=nArrayPos+1;
nNumChar=nNumChar+1;
InvalidateRect(hWnd,NULL,TRUE);
}
break;
case WM_CREATE:
{
hDC=GetDC(hWnd);
GetTextMetrics(hDC,&tm);
nLnHeight=tm.tmHeight+tm.tmExternalLeading;
ReleaseDC(hWnd,hDC);
}
break;
case WM_PAINT:
hDC=BeginPaint(hWnd,&PtStr);
TextOut(hDC,0,nLnHeight,lp_paint,strlen(lp_paint));
EndPaint(hWnd,&PtStr);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return(0);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询