标签:des style blog class code c
>_<: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.
1 while (msg.message!=WM_QUIT) //不是窗口结束消息WM_QUIT,则继续循环 2 { 3 if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))//PeekMessage()函数来检测目前是否有消息处理(检测到0,否则1) 4 { //------------|不能用GetMassage()换,因为它只有取得WM_QUIT时才返回0,其他返回非0,错误返回-1 5 TranslateMessage(&msg);//转换伪码及字符 6 DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序; 7 8 } 9 else//当此次循环运行与上次绘图时间相差0.1秒时再进行重绘操作 10 { 11 tNow=GetTickCount();//取得从开始到现在运行的时间百万分之一秒 12 if(tNow-tPre>=100)MyPaint(hdc); //tPre前次绘图的时间;计算上次绘图到这次循环之间的时间 13 } //------------|若相差100个单位进行一次绘图操作,通过这 14 } //------------|个操作可以调整运行快慢
>_<:code
1 // stdafx.h : include file for standard system include files, 2 // or project specific include files that are used frequently, but 3 // are changed infrequently 4 // 5 6 #if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_) 7 #define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_ 8 9 #if _MSC_VER > 1000 10 #pragma once 11 #endif // _MSC_VER > 1000 12 13 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers 14 15 16 // Windows Header Files: 17 #include <windows.h> 18 19 // C RunTime Header Files 20 #include <stdlib.h> 21 #include <malloc.h> 22 #include <memory.h> 23 #include <tchar.h> 24 25 // Local Header Files 26 27 // TODO: reference additional headers your program requires here 28 29 //{{AFX_INSERT_LOCATION}} 30 // Microsoft Visual C++ will insert additional declarations immediately before the previous line. 31 32 #endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
1 //{{NO_DEPENDENCIES}} 2 // Microsoft Visual C++ generated include file. 3 // Used by FE.RC 4 // 5 #define IDR_MAINFRAME 128 6 #define IDD_FE_DIALOG 102 7 #define IDD_ABOUTBOX 103 8 #define IDS_APP_TITLE 103 9 #define IDM_ABOUT 104 10 #define IDM_EXIT 105 11 #define IDS_HELLO 106 12 #define IDI_FE 107 13 #define IDI_SMALL 108 14 #define IDC_FE 109 15 #define IDC_MYICON 2 16 #define IDC_STATIC -1 17 // Next default values for new objects 18 // 19 #ifdef APSTUDIO_INVOKED 20 #ifndef APSTUDIO_READONLY_SYMBOLS 21 22 #define _APS_NEXT_RESOURCE_VALUE 129 23 #define _APS_NEXT_COMMAND_VALUE 32771 24 #define _APS_NEXT_CONTROL_VALUE 1000 25 #define _APS_NEXT_SYMED_VALUE 110 26 #endif 27 #endif
1 #include "stdafx.h" 2 #include "resourse.h" 3 #include "stdio.h" 4 #include "time.h" 5 6 #define MAX_LOADSTRING 100 7 8 // Global Variables: 9 HINSTANCE hInst; // current instance 10 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text 11 TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text 12 HBITMAP girl[12]; 13 HDC mdc,hdc; 14 HWND hWnd; 15 DWORD tPre,tNow,tCheck;//tPre记录上一次绘图的时间,tNow记录此次绘图的时间,tCheck记录每秒开始的时间 16 int num,frame,fps;//num用来记录图号,frame累加每次画面更新的次数,fps记录每秒画面更新的次数 17 18 // Foward declarations of functions included in this code module: 19 ATOM MyRegisterClass(HINSTANCE hInstance); 20 BOOL InitInstance(HINSTANCE, int); 21 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 22 LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM); 23 void MyPaint(HDC hdc); 24 //======================================================================================== 25 int APIENTRY WinMain(HINSTANCE hInstance, 26 HINSTANCE hPrevInstance, 27 LPSTR lpCmdLine, 28 int nCmdShow) 29 { 30 // TODO: Place code here. 31 MSG msg; 32 33 MyRegisterClass(hInstance);//调用函数向系统注册窗口类别,输入参数hInstance是目前运行程序的对象代码; 34 35 // 调用InitInstance函数,进行初始化操作; 36 if (!InitInstance (hInstance, nCmdShow)) 37 { 38 return FALSE; 39 } 40 41 // 消息循环(通过消息循环来获取信息, 42 //进行必要的键盘信息转换而后将控制权交给操作系统, 43 //有操作系统决定哪个程序的消息处理函数处理消息 44 while (msg.message!=WM_QUIT) //不是窗口结束消息WM_QUIT,则继续循环 45 { 46 if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))//PeekMessage()函数来检测目前是否有消息处理(检测到0,否则1) 47 { //------------|不能用GetMassage()换,因为它只有取得WM_QUIT时才返回0,其他返回非0,错误返回-1 48 TranslateMessage(&msg);//转换伪码及字符 49 DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序; 50 51 } 52 else//当此次循环运行与上次绘图时间相差0.1秒时再进行重绘操作 53 { 54 tNow=GetTickCount();//取得从开始到现在运行的时间百万分之一秒 55 if(tNow-tPre>=100)MyPaint(hdc); //tPre前次绘图的时间;计算上次绘图到这次循环之间的时间 56 } //------------|若相差100个单位进行一次绘图操作,通过这 57 } //------------|个操作可以调整运行快慢 58 59 return msg.wParam; 60 } 61 //===================================================================================== 62 63 64 65 //============================================================================================= 66 //在建立程序窗口实体之前,必须先定义一个窗口类别,其中包含所要建立窗口的信息, 67 //并向系统注册,这里的MyRegisterClass函数就是进行定义及注册窗口类别的函数。 68 //============================================================================================== 69 ATOM MyRegisterClass(HINSTANCE hInstance) 70 { 71 WNDCLASSEX wcex; //申请一个窗口类别“WNDCLASSEX”和结构”wcex“ 72 //-------------------------------------------------------------- 73 //定义vcex结构的各项信息,其中设定信息处理函数(lpfnWndProc) 74 //为WNDPROC,类别名称为(lpszClassName)为”fe"; 75 //-------------------------------------------------------------- 76 wcex.cbSize = sizeof(WNDCLASSEX); 77 78 wcex.style = CS_HREDRAW | CS_VREDRAW; 79 wcex.lpfnWndProc = (WNDPROC)WndProc; 80 wcex.cbClsExtra = 0; 81 wcex.cbWndExtra = 0; 82 wcex.hInstance = hInstance; 83 wcex.hIcon = NULL; 84 wcex.hCursor = NULL; 85 wcex.hCursor = LoadCursor(NULL,IDC_ARROW); 86 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 87 wcex.lpszMenuName = NULL; 88 wcex.lpszClassName = "fe"; 89 wcex.hIconSm = NULL; 90 91 return RegisterClassEx(&wcex);//调用RegisterClassEx函数注册类别,返回一个“ATOM"形态的字符串 92 //此字符串即为类别名称”fe"; 93 } 94 //============================================================================================ 95 96 97 //============================================================================================ 98 //按照前面所定义的窗口类别来建立并显示实际的程序窗口 99 //============================================================================================ 100 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 101 { 102 HWND hWnd; 103 char filename[20]=""; 104 int i; 105 hInst = hInstance; // 把instance handle 储存在全局变量中; 106 107 hWnd = CreateWindow("fe","绘图窗口",WS_OVERLAPPEDWINDOW, 108 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 109 //----------------------------------------------- 110 //调用CreateWindow函数来建立一个窗口对象 111 //第一个参数就是窗口建立依据的类别名称 112 //----------------------------------------------- 113 if (!hWnd) 114 { 115 return FALSE; 116 } 117 //------------------------------------------------ 118 //设定窗口的位置及窗口的大小,然后绘制显示在设备上 119 //------------------------------------------------- 120 MoveWindow(hWnd,10,10,616,440,true);//位置及大小 121 ShowWindow(hWnd, nCmdShow);//改定窗口显示时的状态 122 UpdateWindow(hWnd);//将窗口绘制在显示设备上 123 124 hdc=GetDC(hWnd); 125 mdc=CreateCompatibleDC(hdc); 126 127 for(i=0;i<12;i++) 128 { 129 sprintf(filename,"map%d.bmp",i); 130 girl[i]=(HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,50,50,LR_LOADFROMFILE); 131 } 132 133 num=0; 134 SetTimer(hWnd,1,100,NULL); 135 srand((unsigned)time(NULL)); 136 137 MyPaint(hdc); 138 139 return TRUE; 140 } 141 //============================================================================================ 142 143 144 //============================================================================================ 145 //计算与显示每秒画面更新次数 146 //按照图号顺序进行窗口贴图 147 //============================================================================================ 148 void MyPaint(HDC hdc) 149 { 150 char str[40]=""; 151 152 frame++;//每次调用这个函数将更新次数加1 153 if(tNow-tCheck>=100)//判断此次绘图时间由前一秒算起是否已经达到1秒钟的时间间隔 154 { 155 fps=frame;//若是,将目前的frame付值给fps(这段时间内更新次数) 156 frame=0;//归零 157 tCheck=tNow;//并重设下一次起始时间 158 } 159 sprintf(str,"%d ",fps); 160 TextOut(mdc,40,0,str,strlen(str)); 161 for(int j=0;j<96;j++) 162 { 163 SelectObject(mdc,girl[ rand()%12]); 164 BitBlt(hdc,50*(j%12),j/12*50,650,450,mdc,0,0,SRCCOPY); 165 } 166 167 tPre=GetTickCount();//记录此次绘图的时间 168 } 169 //============================================================================================ 170 171 172 //============================================================================================ 173 //在前面定义类别的时候把WndProc定义为消息处理函数(当某些外部消息发生时,会按消息的类型 174 //来决定该如何进行处理。此外该函数也是一个回叫函数(CALLBACK)(windows系统函数)每一个 175 //程序都会接收信息,选择性接受、处理; 176 //============================================================================================ 177 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 178 { 179 int i; 180 181 switch (message) //判断消息类型,不是WM_QUIT,则循环结束,否则则进行循环 182 { 183 case WM_TIMER: //时间消息 184 MyPaint(hdc); 185 break; 186 case WM_DESTROY: //处理窗口结束消息 187 DeleteDC(mdc); 188 ReleaseDC(hWnd,hdc); 189 for(i=0;i<12;i++) 190 DeleteObject(girl[i]); 191 KillTimer(hWnd,1); 192 PostQuitMessage(0); 193 break; 194 default: 195 return DefWindowProc(hWnd, message, wParam, lParam); 196 } 197 return 0; 198 } 199 //============================================================================================
[游戏模版12] Win32 稳定定时,布布扣,bubuko.com
标签:des style blog class code c
原文地址:http://www.cnblogs.com/zjutlitao/p/3733966.html