>_<: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. WebForm---登陆状态保持(Cookies内置对象)

    登录状态保持: 首先做一个登录界面,点击 登录按钮 protected void Page_Load(object sender, EventArgs e) { Button1.Click += Bu ...

  2. c 数据拼接

    char buf1[] = {0x31,0x32,0x33,0x00,0x51,0x52,0x53,0xaa,0xbb,0xcc,0x00}; int a=0xabcd6799; int b=0x88 ...

  3. DIOCP之EchoServer分析

    constructor TfrmMain.Create(AOwner: TComponent);begin inherited Create(AOwner); sfLogger.setAppender ...

  4. Sum All Odd Fibonacci Numbers

    function sumFibs(num) { //return num; var arr = [1,1]; var add = 2; while(true){ var item = arr[0] + ...

  5. 自己随意写了个简单的依赖jquery的轮播图

    //轮播图 function Switcher(obj){ this.box = $(obj.box); this.width = this.box.width(); this.banner = $( ...

  6. asp.net MVC 源码分析

    先上一张图吧 asp.net请求机制的图  by传智播客邹华栋老师 然后是 邹老师添加MVC请求过程的图 其实MVC 是在.netframework上加了一个过滤器  HttpModule 在C:\W ...

  7. python dict

    ###字典的基本结构 info = { "k1" : "v1", "k2" : "v2" } ###字典的valus可以 ...

  8. three.js 之旅 (二)

    three.js中各种场景的使用方法: 当然首先要先引入three.js库:其次,手动定义一个 canvas 标签. <script type="text/javascript&quo ...

  9. SignalR一个集成的客户端与服务器库。内部的两个对象类:PersistentConnection和Hub

    SignalR 将整个交换信息的行为封装得非常漂亮,客户端和服务器全部都使用 JSON 来沟通,在服务器端声明的所有 hub 的信息,都会一般生成 JavaScript 输出到客户端. 它是基于浏览器 ...

  10. 使用Nexus搭建本地Maven私服

    搭建了好几天这个还是不大好使,今天看了一篇文章是讲这个的,然后根据其情况,加上自己的更改最后搭建成功了 1.下载nexus, 下载地址:http://www.sonatype.org/nexus/go ...