游戏编程基础006---定时器动画
代码地址:/2012/0118/20120118075805571.rar/************************************************************************/
/*定时器使用
1. 建立定时器
SetTimer()函数为窗口建立一定时器,并每隔一段时间发出WM_TIMER消息
UINT SetTime( HWND 接受定时器消息的窗口
UINT 定时器代号
UINT 时间间隔
TIMERPROC 处理响应函数);
2. 删除定时器
BOOL KillTimer( int 定时器代号)
*/
/************************************************************************//
#include "stdafx.h"
//全局变量声明 www.zzzyk.com
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);
&n
补充:综合编程 , 其他综合 ,