>_<:compared with the previous article,this one only adds key-message listener.

>_<:up down left right control the roal movement.

 1 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 2 {
 3     int wmId, wmEvent;
 4     PAINTSTRUCT ps;
 5 
 6     switch (message)
 7     {
 8     case WM_KEYDOWN:
 9         switch(wParam)
10         {
11              case VK_UP:
12                  y-=20;
13                  if(y<0)y+=480;
14                  break;
15              case VK_DOWN:
16                  y=(y+20)%480;
17                  break;
18              case VK_LEFT:
19                  speed-=10;
20                  if(speed<0)speed=0;
21                  SetTimer(hWnd,1,speed,NULL);
22                  break;
23              case VK_RIGHT:
24                  speed+=10;
25                  if(speed>300)speed=300;
26                  SetTimer(hWnd,1,speed,NULL);
27                  break;
28         }
29         break;
30     case WM_TIMER:
31         MyPaint(hdc);
32         break;
33     case WM_COMMAND:
34         wmId    = LOWORD(wParam);
35         wmEvent = HIWORD(wParam);
36         // 分析菜单选择:
37         switch (wmId)
38         {
39         case IDM_EXIT:
40             DestroyWindow(hWnd);
41             break;
42         default:
43             return DefWindowProc(hWnd, message, wParam, lParam);
44         }
45         break;
46     case WM_PAINT:
47         hdc = BeginPaint(hWnd, &ps);
48         // TODO: 在此添加任意绘图代码...
49         EndPaint(hWnd, &ps);
50         break;
51     case WM_DESTROY:
52         DeleteDC(mdc);
53         ReleaseDC(hWnd,hdc);
54         DeleteObject(bg);
55         DeleteObject(dra);
56         KillTimer(hWnd,1);
57         PostQuitMessage(0);
58         break;
59     default:
60         return DefWindowProc(hWnd, message, wParam, lParam);
61     }
62     return 0;
63 }

[游戏模版14] Win32 键盘控制

 1 // stdafx.h : include file for standard system include files,
 2 //  or project specific include files that are used frequently, but
 3 //      are changed infrequently
 4 //
 5 
 6 #if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
 7 #define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_
 8 
 9 #if _MSC_VER > 1000
10 #pragma once
11 #endif // _MSC_VER > 1000
12 
13 #define WIN32_LEAN_AND_MEAN        // Exclude rarely-used stuff from Windows headers
14 
15 
16 // Windows Header Files:
17 #include <windows.h>
18 
19 // C RunTime Header Files
20 #include <stdlib.h>
21 #include <malloc.h>
22 #include <memory.h>
23 #include <tchar.h>
24 
25 // Local Header Files
26 
27 // TODO: reference additional headers your program requires here
28 
29 //{{AFX_INSERT_LOCATION}}
30 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
31 
32 #endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
StdAfx.h

相关文章: