ance); BOOL InitInstance (HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void MyPaint(HDC hdc); //***WinMain函数,程序入口点函数************************************** int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; MyRegisterClass(hInstance); //运行初始化函数 if (!InitInstance (hInstance, nCmdShow)) { return FALSE; }[cpp] GetMessage(&msg,NULL,NULL,NULL); //感谢xiaoxiangp的提醒,需要在进入消息循环之前初始化msg,避免了死循环发生的可能性。 //游戏循环 while( msg.message!=WM_QUIT ) { if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { tNow = GetTickCount(); if(tNow-tPre >= 100) //当此次循环运行与上次绘图时间相差0.1秒时再进行重绘操作 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,600,450,true); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); hdc = GetDC(hWnd); mdc = CreateCompatibleDC(hdc); //载入各个人物位图 for(i=0;i<7;i++) { sprintf(filename,"man%d.bmp",i); man[i] = (HBITMAP)LoadImage (NULL,filename,IMAGE_BITMAP,640,480,LR_LOADFROMFILE); } num = 0; frame = 0; MyPaint(hdc); return TRUE; } //****自定义绘图函数********************************* // 1.计算与显示每秒画面更新次数 // 2.按照图号顺序进行窗口贴图 void MyPaint(HDC hdc) { char str[40] = ""; if(num == 7) num = 0; frame++; //画面更新次数加1 if(tNow - tCheck >= 1000) //判断此次绘图时间由前一秒算起是否已经达到1秒钟的时间间隔。若是,则将目前的'frame'值赋给"fps",表示这一秒内所更新的画面次数,然后将“frame”值回0,并重设下次计算每秒画面数的起始时间"iCheck"。 { fps = frame; frame = 0; tCheck = tNow; } SelectObject(mdc,man[num]); //选用要更新的图案到mdc中,再输出显示每秒画面更新次数的字符串到mdc上,最后将mdc的内容贴到窗口中。 sprintf(str,"每秒显示 %d个画面",fps); TextOut(mdc,0,0,str,strlen(str)); BitBlt(hdc,0,0,600,450,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<7;i++) DeleteObject(man[i]); ReleaseDC(hWnd,hdc); PostQuitMessage(0); break; default: //其他消息 return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } GetMessage(&msg,NULL,NULL,NULL); //感谢xiaoxiang |