游戏编程基础006---定时器动画

代码地址:http://download.csdn.net/detail/liu_liu213/4013232

 

/************************************************************************/
/*定时器使用
1. 建立定时器
SetTimer()函数为窗口建立一定时器,并每隔一段时间发出WM_TIMER消息
UINT SetTime(	HWND		接受定时器消息的窗口
				UINT		定时器代号
				UINT		时间间隔
				TIMERPROC	处理响应函数);
2. 删除定时器
BOOL KillTimer( int 定时器代号)

*/
/************************************************************************//


#include "stdafx.h"

//全局变量声明
HINSTANCE hInst;
HBITMAP girl[7];  //存储各张人物位图
HDC mdc, hdc;	
int num;	//记录目前显示图号

//全局函数
void MyPaint(HDC);
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);

//****程序入口**************************************
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	MSG msg;

	MyRegisterClass(hInstance);

	//运行初始化函数
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	//消息循环
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

//****定义及注册窗口类别函数*************************
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= NULL;
	wcex.hCursor		= NULL;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "canvas";  //类别名称
	wcex.hIconSm		= NULL;

	return RegisterClassEx(&wcex);
}

//****初始化*************************************
// 1.从文件中加载位图
// 2.建立定时器
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;
	char filename[20] = "";
	int i;

	hInst = hInstance;

	hWnd = CreateWindow("canvas", "绘图窗口" , WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{
		return FALSE;
	}

	MoveWindow(hWnd,10,10,600,450,true);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	hdc = GetDC(hWnd);
	mdc = CreateCompatibleDC(hdc);

	//载入各人物位图
	for (i=0; i<7; i++){
		sprintf(filename, "girl%d.bmp", i);
		girl[i] = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 640, 480, LR_LOADFROMFILE);
	}

	num = 0;
	SetTimer(hWnd, 1, 500, NULL);
	MyPaint(hdc);
	return TRUE;
}

VOID MyPaint(HDC hdc){
	if (num > 6){
		num = 0;
	}

	SelectObject(mdc, girl[num]);
	BitBlt(hdc, 0, 0, 600, 450, mdc, 0, 0, SRCCOPY);

	num++;
}

//****回调函数***********************************
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	//PAINTSTRUCT ps;
	//HDC hdc;

	switch (message) 
	{
		//case WM_PAINT:						
		//	hdc = BeginPaint(hWnd, &ps);
		//	EndPaint(hWnd, &ps);
		//	break;
		case WM_TIMER:
			MyPaint(hdc);
			break;
		case WM_DESTROY:	
			DeleteDC(mdc);
			ReleaseDC(hWnd, hdc);
			for (int i=0; i<7; i++)
			{
				DeleteObject(girl[i]);
			}

			KillTimer(hWnd, 1);
			PostQuitMessage(0);
			break;
		default:							
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值