>_<:The last time,we learned how to use timer to make the picture run and change show,but sometimes the time may have a little change,for example when you move the window or do other things the frequency may be effected.And now we will talk about how to get a stable timer.

>_<:Actually it's not difficult,only need to calculate the difference between successive two times painting.If find the difference bigger than a seted time value,do a new painting.

 while (msg.message!=WM_QUIT) //不是窗口结束消息WM_QUIT,则继续循环
{
if(PeekMessage(&msg,NULL,,,PM_REMOVE))//PeekMessage()函数来检测目前是否有消息处理(检测到0,否则1)
{ //------------|不能用GetMassage()换,因为它只有取得WM_QUIT时才返回0,其他返回非0,错误返回-1
TranslateMessage(&msg);//转换伪码及字符
DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序; }
else//当此次循环运行与上次绘图时间相差0.1秒时再进行重绘操作
{
tNow=GetTickCount();//取得从开始到现在运行的时间百万分之一秒
if(tNow-tPre>=)MyPaint(hdc); //tPre前次绘图的时间;计算上次绘图到这次循环之间的时间
} //------------|若相差100个单位进行一次绘图操作,通过这
} //------------|个操作可以调整运行快慢

>_<:code

 // stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
// #if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_ #if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers // Windows Header Files:
#include <windows.h> // C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h> // Local Header Files // TODO: reference additional headers your program requires here //{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)

StdAfx.h

 //{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by FE.RC
//
#define IDR_MAINFRAME 128
#define IDD_FE_DIALOG 102
#define IDD_ABOUTBOX 103
#define IDS_APP_TITLE 103
#define IDM_ABOUT 104
#define IDM_EXIT 105
#define IDS_HELLO 106
#define IDI_FE 107
#define IDI_SMALL 108
#define IDC_FE 109
#define IDC_MYICON 2
#define IDC_STATIC -1
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 129
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 110
#endif
#endif

resourse.h

 #include "stdafx.h"
#include "resourse.h"
#include "stdio.h"
#include "time.h" #define MAX_LOADSTRING 100 // Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
HBITMAP girl[];
HDC mdc,hdc;
HWND hWnd;
DWORD tPre,tNow,tCheck;//tPre记录上一次绘图的时间,tNow记录此次绘图的时间,tCheck记录每秒开始的时间
int num,frame,fps;//num用来记录图号,frame累加每次画面更新的次数,fps记录每秒画面更新的次数 // Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
//========================================================================================
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg; MyRegisterClass(hInstance);//调用函数向系统注册窗口类别,输入参数hInstance是目前运行程序的对象代码; // 调用InitInstance函数,进行初始化操作;
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
} // 消息循环(通过消息循环来获取信息,
//进行必要的键盘信息转换而后将控制权交给操作系统,
//有操作系统决定哪个程序的消息处理函数处理消息
while (msg.message!=WM_QUIT) //不是窗口结束消息WM_QUIT,则继续循环
{
if(PeekMessage(&msg,NULL,,,PM_REMOVE))//PeekMessage()函数来检测目前是否有消息处理(检测到0,否则1)
{ //------------|不能用GetMassage()换,因为它只有取得WM_QUIT时才返回0,其他返回非0,错误返回-1
TranslateMessage(&msg);//转换伪码及字符
DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序; }
else//当此次循环运行与上次绘图时间相差0.1秒时再进行重绘操作
{
tNow=GetTickCount();//取得从开始到现在运行的时间百万分之一秒
if(tNow-tPre>=)MyPaint(hdc); //tPre前次绘图的时间;计算上次绘图到这次循环之间的时间
} //------------|若相差100个单位进行一次绘图操作,通过这
} //------------|个操作可以调整运行快慢 return msg.wParam;
}
//===================================================================================== //=============================================================================================
//在建立程序窗口实体之前,必须先定义一个窗口类别,其中包含所要建立窗口的信息,
//并向系统注册,这里的MyRegisterClass函数就是进行定义及注册窗口类别的函数。
//==============================================================================================
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex; //申请一个窗口类别“WNDCLASSEX”和结构”wcex“
//--------------------------------------------------------------
//定义vcex结构的各项信息,其中设定信息处理函数(lpfnWndProc)
//为WNDPROC,类别名称为(lpszClassName)为”fe";
//--------------------------------------------------------------
wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = ;
wcex.cbWndExtra = ;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL,IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "fe";
wcex.hIconSm = NULL; return RegisterClassEx(&wcex);//调用RegisterClassEx函数注册类别,返回一个“ATOM"形态的字符串
//此字符串即为类别名称”fe";
}
//============================================================================================ //============================================================================================
//按照前面所定义的窗口类别来建立并显示实际的程序窗口
//============================================================================================
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
char filename[]="";
int i;
hInst = hInstance; // 把instance handle 储存在全局变量中; hWnd = CreateWindow("fe","绘图窗口",WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, , CW_USEDEFAULT, , NULL, NULL, hInstance, NULL);
//-----------------------------------------------
//调用CreateWindow函数来建立一个窗口对象
//第一个参数就是窗口建立依据的类别名称
//-----------------------------------------------
if (!hWnd)
{
return FALSE;
}
//------------------------------------------------
//设定窗口的位置及窗口的大小,然后绘制显示在设备上
//-------------------------------------------------
MoveWindow(hWnd,,,,,true);//位置及大小
ShowWindow(hWnd, nCmdShow);//改定窗口显示时的状态
UpdateWindow(hWnd);//将窗口绘制在显示设备上 hdc=GetDC(hWnd);
mdc=CreateCompatibleDC(hdc); for(i=;i<;i++)
{
sprintf(filename,"map%d.bmp",i);
girl[i]=(HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,,,LR_LOADFROMFILE);
} num=;
SetTimer(hWnd,,,NULL);
srand((unsigned)time(NULL)); MyPaint(hdc); return TRUE;
}
//============================================================================================ //============================================================================================
//计算与显示每秒画面更新次数
//按照图号顺序进行窗口贴图
//============================================================================================
void MyPaint(HDC hdc)
{
char str[]=""; frame++;//每次调用这个函数将更新次数加1
if(tNow-tCheck>=)//判断此次绘图时间由前一秒算起是否已经达到1秒钟的时间间隔
{
fps=frame;//若是,将目前的frame付值给fps(这段时间内更新次数)
frame=;//归零
tCheck=tNow;//并重设下一次起始时间
}
sprintf(str,"%d ",fps);
TextOut(mdc,,,str,strlen(str));
for(int j=;j<;j++)
{
SelectObject(mdc,girl[ rand()%]);
BitBlt(hdc,*(j%),j/*,,,mdc,,,SRCCOPY);
} tPre=GetTickCount();//记录此次绘图的时间
}
//============================================================================================ //============================================================================================
//在前面定义类别的时候把WndProc定义为消息处理函数(当某些外部消息发生时,会按消息的类型
//来决定该如何进行处理。此外该函数也是一个回叫函数(CALLBACK)(windows系统函数)每一个
//程序都会接收信息,选择性接受、处理;
//============================================================================================
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i; switch (message) //判断消息类型,不是WM_QUIT,则循环结束,否则则进行循环
{
case WM_TIMER: //时间消息
MyPaint(hdc);
break;
case WM_DESTROY: //处理窗口结束消息
DeleteDC(mdc);
ReleaseDC(hWnd,hdc);
for(i=;i<;i++)
DeleteObject(girl[i]);
KillTimer(hWnd,);
PostQuitMessage();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return ;
}
//============================================================================================

main.cpp

[游戏模版12] Win32 稳定定时的更多相关文章

  1. [游戏模版2] Win32最小框架

    >_<:Just the minimum Win32  frame don't have any other special function. //{{NO_DEPENDENCIES}} ...

  2. [游戏模版18] Win32 五子棋

    >_<:Learning its AI logic. >_<:resource >_<:code: #include <windows.h> // C ...

  3. [游戏模版3] Win32 画笔 画刷 图形

    >_<:introduce the functions of define\create\use pen and brush to draw all kinds of line and s ...

  4. [游戏模版4] Win32 显示鼠标位置

    >_<:use MOUSE_MOVE message refresh the position information. >_<:use LOWORD(lParam) get ...

  5. [游戏模版5] Win32 折线 弧线

    >_<:first build some points put in poly1[],poly2[] and poly3[] in the function of InitInstance ...

  6. [游戏模版6] Win32 graph

    >_<:there in the MyPaint(...) function respectively use Ellipse(...) draw ellipse, use RoundRe ...

  7. [游戏模版7] Win32 最简单贴图

    >_<:this is the first using mapping. >_<:There will be introducing how to do: First load ...

  8. [游戏模版8] Win32 透明贴图

    >_<:The same with previous introduction. In the InitInstance fanction make a little change: &g ...

  9. [游戏模版9] Win32 半透明 图像处理

    >_<:Previous part we talk about how to map a transparent picture, and this time we will solve ...

随机推荐

  1. MySQL5.7.10免安装版配置

     最新版的 Mysql 不提供图形界面的安装了, 下载下来是一个压缩包的形式, 那么我们如何来使用它呢, 让它为我们工作呢? 环境: mysql-5.7.10-winx64 + win7(64位) 一 ...

  2. 论velocity在不同后台语言下的不同

    第一家公司使用asp.net开发的,本人从事前端工作.当时用velocity写模板程序记得也没配置啥,我就记得写了rewrite,html页面里头直接写的velocity. 现在公司用的java开发的 ...

  3. Jquery Mobile 动态添加元素然后刷新 转

    Jquery Mobile 动态添加元素然后刷新 (2013-05-09 12:39:05) 转载▼ 标签: it 分类: Mobile jquery 表单元素每一个元素都有自己刷新的方法,每当改变它 ...

  4. UIScrollViewDelegate

    一.执行顺序:(scrollView加后面的)willBeginDragging         // 将要开始拖拽,手指已经放在view上并准备拖动的那一刻 DidScroll           ...

  5. POST 和GET传输的最大容量分别是多少?

    get 是通过URL提交数据,因此GET可提交的数据量就跟URL所能达到的最大长度有直接关系.很多文章都说GET方式提交的数据最多只能是1024字节,而 实际上,URL不存在参数上限的问题,HTTP协 ...

  6. Oracle 10g -- 修改DB的编码

    修改DB的原因是:因为我的DB不支持中文,所以每当我向数据库表中插入一条数据的时候,中文就都变了类似于“?(是反问号)”的乱码,为了能顺利插入成功,故做了此次修改; 系统:windows XP 英文版 ...

  7. javascript 的事件--阻止冒泡

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  8. [R] 回归拟合

    如下示例 > fit <- lm(y~x, data = data01) > summary(fit) Call: lm(formula = data01$P ~ data01$M, ...

  9. MySQL~ IN , FIND_IN_SET , LIKE

    MySQL- IN , FIND_IN_SET , LIKE SELECT * FROM test where area IN (1, 2, 3); SELECT * FROM test where ...

  10. img图片放大控件 lightbox.js

    1.引入相关js,css jquery-1.7.2.min.js,lightbox.css,lightbox.js   2 图片增加<a href="大图片地址" rel=& ...