一、重要函数
1.函数原型
BOOL PeekMessage(LPMSG IpMsg,HWND hWnd,UINT wMSGfilterMin,UINT wMsgFilterMax,UINT wRemoveMsg);
功 能:用于检测消息,检测到了就返回非0值,否则返回0;
参数一:接收消息信息的MSG结构指针;
参数二:其消息被检查的窗口句柄;
参数三:指定被检查的消息范围里的第一个消息,这里设为0;
参数四:指定被检查的消息范围里的最后一个消息,也设为0;
参数五:根据参数值确定消息如何处理,这里使用PM_REMOVE,意思是使用后将消息从消息队列中除掉;
2.函数原型
DWORD GetTickCount(void);
功 能:返回系统从开始到现在经过的时间,毫秒级
二、基本思路
1.定时器适用显示简易动画及小游戏程序,当游戏整合多种消息处理、数学运算、大量音效时,定时器达不到标准,因此采用游 戏循环,通过判断是否有消息,有消息处理消息,没有则每隔一段时间重绘画面;
2.利用tPre表示上一次开始绘图的时间;tNow表示当前时间;tNow-tPre表示从上一次绘图到现在经过了多长时间,通过它来控 制游戏的速度;
三、代码实现
#include "stdafx.h"
#include <stdio.h>
HINSTANCE hInst;
HBITMAP man[10];
HDC hdc,mdc;
HWND hWnd;
DWORD tPre,tNow,tCheck;//tPre表示前次绘图的时间;tNow记录此次准备绘图的时间;tCheck记录每秒开始的时间?????
int num,frame,fps;//图号、累加画面更新次数、每秒画面更新次数
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
while( msg.message!=WM_QUIT )//当收到的消息不是窗口结束消息时,进入循环,message是一个消息类型的代号
{
if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )//检测是否有消息,有就返回非零值;没有返回零值
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
tNow = GetTickCount();//取得系统到目前所用的时间,目的是搭配判断式,调整游戏运行速度
if(tNow-tPre >= 50)
MyPaint(hdc);
}
}
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);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
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,440,280,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
for(i=0;i<8;i++)
{
sprintf(filename,"man%d.bmp",i);
man[i] = (HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,426,240,LR_LOADFROMFILE);
}
num = 0;
frame = 0;
MyPaint(hdc);
return TRUE;
}
void MyPaint(HDC hdc)
{
char str[40] = "";
if(num == 8)
num = 0;
frame++;
if(tNow - tCheck >= 1000)//现在绘图开始的时间-每秒开始的时间,来判断是否够1秒,目的是为了计算fps
{
fps = frame;
frame = 0;
tCheck = tNow;
}
SelectObject(mdc,man[num]);
sprintf(str,"每秒钟显示%d 个画面",fps);
TextOut(mdc,0,0,str,strlen(str));//将str所指向的内容和长度输出至窗口文本框
BitBlt(hdc,0,0,426,240,mdc,0,0,SRCCOPY);
tPre = GetTickCount(); //记录此次绘图时间
num++;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i;
switch (message)
{
case WM_DESTROY:
DeleteDC(mdc);
for(i=0;i<8;i++)
DeleteObject(man[i]);
ReleaseDC(hWnd,hdc);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
四、效果