#include "stdafx.h"
#include <stdio.h>
//全局变量声明
HINSTANCE hInst;
HBITMAP girl[4],bg;
HDC hdc,mdc,bufdc;
HWND hWnd;
DWORD tPre,tNow;
int num,dir,x,y; //x,y变量为人物贴图坐标,dir为人物移动方向,这里我们中以0,1,2,3代表人物上,下,左,右方向上的移动:num为连续贴图中的小图编号
//全局函数声明
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
//****WinMain函数,程序入口点函数***********************
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
//初始化
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
GetMessage(&msg,NULL,NULL,NULL); //初始化msg
//消息循环
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
{
40. TranslateMessage( &msg );
41. DispatchMessage( &msg );
42. }
43. else
44. {
45. tNow = GetTickCount();
46. if(tNow-tPre >= 40)
47. MyPaint(hdc);
}
}
return msg.wParam;
}
//****设计一个窗口类,类似填空题,使用窗口结构体*******************
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
61. wcex.lpfnWndProc = (WNDPROC)WndProc;
62. wcex.cbClsExtra = 0;
63. wcex.cbWndExtra = 0;
64. wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
68. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
69. wcex.lpszMenuName = NULL;
70. wcex.lpszClassName = "canvas";
71. wcex.hIconSm = NULL;
72.
73. return RegisterClassEx(&wcex);
}
//****初始化函数*************************************
// 加载位图并设定各种初始值
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HBITMAP bmp;
hInst = hInstance;
hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
86. if (!hWnd)
87. {
88. return FALSE;
89. }
90.
91. MoveWindow(hWnd,10,10,640,480,true);
92. ShowWindow(hWnd, nCmdShow);
93. UpdateWindow(hWnd);
94.
95. hdc = GetDC(hWnd);
96. mdc = CreateCompatibleDC(hdc);
97. bufdc = CreateCompatibleDC(hdc);
98.
99.
100. //建立空的位图并置入mdc中
101. bmp = CreateCompatibleBitmap(hdc,640,480);
102. SelectObject(mdc,bmp);
103.
104.
105. //设定人物贴图初始位置和移动方向
106. x = 300;
107. y = 250;
108. dir = 0;
109. num = 0;
110.
111. //载入各连续移动位图及背景图
112. girl[0] = (HBITMAP)LoadImage(NULL,"girl0.bmp",IMAGE_BITMAP,440,148,LR_LOADFROMFILE);
113. girl[1] = (HBITMAP)LoadImage(NULL,"girl1.bmp",IMAGE_BITMAP,424,154,LR_LOADFROMFILE);
114. girl[2] = (HBITMAP)LoadImage(NULL,"girl2.bmp",IMAGE_BITMAP,480,148,LR_LOADFROMFILE);
115. girl[3] = (HBITMAP)LoadImage(NULL,"girl3.bmp",IMAGE_BITMAP,480,148,LR_LOADFROMFILE);
116. bg = (HBITMAP)LoadImage(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);
117.
118. MyPaint(hdc);
119.
120. return TRUE;
121.}
122.
123.//****自定义绘图函数*********************************
124.// 人物贴图坐标修正及窗口贴图
void MyPaint(HDC hdc)
{
int w,h;
128.
129. //先在mdc中贴上背景图
130. SelectObject(bufdc,bg);
131. BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);
132.
133. //按照目前的移动方向取出对应人物的连续走动图,并确定截取人物图的宽度与高度
134. SelectObject(bufdc,girl[dir]);
135. switch(dir)
136. {
137. case 0:
138. w = 55;
139. h = 74;
140. break;
141. case 1:
142. w = 53;
143. h = 77;
144. break;
145. case 2:
146. w = 60;
147. h = 74;
148. break;
149. case 3:
150. w = 60;
151. h = 74;
152. break;
153. }
154. //按照目前的X,Y的值在mdc上进行透明贴图,然后显示在窗口画面上
155. BitBlt(mdc,x,y,w,h,bufdc,num*w,h,SRCAND);
156. BitBlt(mdc,x,y,w,h,bufdc,num*w,0,SRCPAINT);
157.
158. BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);
159.
160. tPre = GetTickCount(); //记录此次绘图时间
161.
162. num++;
163. if(num == 8)
164. num = 0;
165.
166.}
//****消息处理函数***********************************
// 1.按下【Esc】键结束程序
// 2.按下方向键重设贴图坐标
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
175. case WM_KEYDOWN: //按下键盘消息
176. //判断按键的虚拟键码
177. switch (wParam)
178. {
179. case VK_ESCAPE: //按下【Esc】键
180. PostQuitMessage( 0 ); //结束程序
181. break;
182. case VK_UP: //按下【↑】键
183. //先按照目前的移动方向来进行贴图坐标修正,并加入人物往上移动的量(每次按下一次按键移动10个单位),来决定人物贴图坐标的X与Y值,接着判断坐标是否超出窗口区域,若有则再次修正
184. switch(dir)
185. {
186. case 0:
187. y -= 10;
188. break;
189. case 1:
190. x -= 1;
191. y -= 8;
192. break;
193. case 2:
194. x += 2;
195. y -= 10;
196. break;
197. case 3:
198. x += 2;
199. y -= 10;
200. break;
201. }
202. if(y < 0)
203. y = 0;
204. dir = 0;
205. break;
206. case VK_DOWN: //按下【↓】键
207. switch(dir)
208. {
209. case 0:
210. x += 1;
211. y += 8;
212. break;
213. case 1:
214. y += 10;
215. break;
216. case 2:
217. x += 3;
218. y += 6;
219. break;
220. case 3:
221. x += 3;
222. y += 6;
223. break;
224. }
225.
226. if(y > 375)
227. y = 375;
228. dir = 1;
229. break;
230. case VK_LEFT: //按下【←】键
231. switch(dir)
232. {
233. case 0:
234. x -= 12;
235. break;
236. case 1:
237. x -= 13;
238. y += 4;
239. break;
240. case 2:
241. x -= 10;
242. break;
243. case 3:
244. x -= 10;
245. break;
246. }
247. if(x < 0)
248. x = 0;
249. dir = 2;
250. break;
251. case VK_RIGHT: //按下【→】键
252. switch(dir)
253. {
case 0:
x += 8;
break;
case 1:
x += 7;
y += 4;
break;
case 2:
x += 10;
break;
case 3:
x += 10;
break;
}
if(x > 575)
x = 575;
dir = 3;
break;
}
break;
case WM_DESTROY: //窗口结束消息
int i;
DeleteDC(mdc);
DeleteDC(bufdc);
for(i=0;i<4;i++)
DeleteObject(girl[i]);
DeleteObject(bg);
ReleaseDC(hWnd,hdc);
PostQuitMessage(0);
break;
default: //其他消息
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}