1、南京信息工程大学 实验(实习)报告实验(实习)名称 基于键盘与鼠标应用的程序设计 实验(实习)日期 一、实验目的1掌握键盘与鼠标在应用程序中的消息响应机制。二、实验内容及步骤实验任务1.熟悉键盘的消息响应;2.熟悉鼠标的消息响应;实验内容设计一个窗口,再该窗口中练习键盘的响应,要求如下:(1)单机键盘上的向上箭头时,窗口中显示“You had hitted the up key”;(2)单击 Shift 键时,窗口中显示“You had hitted the Shift key”;(3)单击 Ctrl 键时,窗口中显示“You had hitted the Ctrl key”;(4)单击 Ct
2、rl+A 键时,窗口中显示“You had hitted the Ctrl A key”;(5)单击 Shift+B 键时,窗口中显示 “You had hitted the Shift B key”;程序代码:#include#include#include#includelong WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam);BOOL InitWindowsClass(HINSTANCE hInstance);BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
3、;HWND hWndMain;int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)MSG Message;if(!InitWindowsClass(hInstance) return FALSE;if(!InitWindows(hInstance,nCmdShow)return FALSE;while(GetMessage(DispatchMessage(return Message.wParam;long WINAPI WndProc(HWND hWnd,UIN
4、T iMessage,UINT wParam,LONG lParam)HDC hDC; /定义指向设备上下文的句柄PAINTSTRUCT ps; /定义指向包含绘图信息的结构体变量HPEN hPen; /定义画笔句柄HBRUSH hBrush; /定义画刷句柄 /以下定义输出的字符串static char cUp=“You had hitted the UP key“;static char cCtrl=“You had hitted the Ctrl key“;static char cShift=“You had hitted the SHIFT key“;static char cCtr
5、l_A=“You had hitted the CTRL A key“;static char cShift_B=“You had hitted the SHIFT B key“;/下面定义并初始化按键标志变量static BOOL nUpKeyDown=FALSE,nShiftKeyDown=FALSE, nCtrlKeyDown=FALSE,nCtrlAKeyDown=FALSE, nShiftBKeyDown=FALSE; switch(iMessage) case WM_KEYDOWN:switch(wParam) case VK_UP: /当按上箭头键时,变量置为真nUpKeyDow
6、n = TRUE; break;case VK_SHIFT: /当按 Shift 键时,变量置为真nShiftKeyDown = TRUE; break;case VK_CONTROL: /当按 Control 键时,变量置为真nCtrlKeyDown = TRUE; break;default: break;break;case WM_KEYUP:InvalidateRect(hWnd,NULL,FALSE);break;case WM_CHAR:if(wParam=(65nCtrlKeyDown = FALSE;else if(wParam=98|wParam=66) /当按下 b 键时i
7、f(nShiftKeyDown = TRUE) /检查 Shift 键是否处于按下状态。nShiftBKeyDown = TRUE; /当 Shift 键按下时,变量置为真nShiftKeyDown = FALSE;break;case WM_PAINT: /处理绘图消息.hDC=BeginPaint(hWnd,hBrush = (HBRUSH)GetStockObject(WHITE_BRUSH); /创建 白画刷。hPen = (HPEN)GetStockObject(WHITE_PEN); /创建白画笔。SelectObject(hDC,hPen); /选入白画刷SelectObject
8、(hDC,hBrush); /选入白画笔SetTextColor(hDC,RGB(255,0,0); /设置字体颜色为红色。/输出信息。if(nUpKeyDown = TRUE) Rectangle(hDC,0,0,300,200);TextOut(hDC,0,0,cUp,strlen(cUp);nUpKeyDown = FALSE; else if(nCtrlAKeyDown = TRUE) Rectangle(hDC,0,0,300,200);TextOut(hDC,0,100,cCtrl_A,strlen(cCtrl_A);nCtrlAKeyDown = FALSE;nCtrlKeyDo
9、wn = FALSE;else if(nCtrlKeyDown = TRUETextOut(hDC,0,60,cCtrl,strlen(cCtrl);nCtrlKeyDown = FALSE;else if(nShiftBKeyDown = TRUE) Rectangle(hDC,0,0,300,200);TextOut(hDC,0,0,cShift_B,strlen(cShift_B);nShiftBKeyDown = FALSE;nShiftKeyDown = FALSE; else if(nShiftBKeyDown = FALSETextOut(hDC,0,0,cShift,strle
10、n(cShift);nShiftKeyDown = FALSE;else /删除画笔和画刷printf(“Input Error !n“);break;DeleteObject(hPen);DeleteObject(hBrush);EndPaint(hWnd,break;case WM_DESTROY:PostQuitMessage(0);return 0;default:return(DefWindowProc(hWnd,iMessage,wParam,lParam);return 0;BOOL InitWindows(HINSTANCE hInstance,int nCmdShow) HW
11、ND hWnd;hWnd=CreateWindow(“6_3“, “键盘实例程序 “,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);if(!hWnd)return FALSE;hWndMain=hWnd;ShowWindow(hWnd,nCmdShow);UpdateWindow(hWnd);return TRUE;BOOL InitWindowsClass(HINSTANCE hInstance)WNDCLASS WndClass;WndClass.cbClsExtra=0;WndC
12、lass.cbWndExtra=0;WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH); WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); WndClass.hIcon=LoadIcon(NULL,“END“); WndClass.hInstance=hInstance; WndClass.lpfnWndProc=WndProc; WndClass.lpszClassName=“6_3“; WndClass.lpszMenuName=NULL;WndClass.style=CS_HREDRAW|CS_VREDRAW; return RegisterClass(WndClass.style=0;程序运行结果:三、实验结果通过本次试验我了解了基于键盘与鼠标应用的程序设计,熟悉了键盘的消息响应以及鼠标的消息响应,了解了关于窗口的相关操作。