您的位置:首页 > 其它

[游戏模版11] Win32 动画 时间消息

2014-05-17 12:43 381 查看
>_<:This time we will study a new way to operate your picture.That is running your picture by give it a timer-message.

>_<:Firstly,you should use the function SetTimer(hWnd,1,50,NULL) to create and set a timer (here "1" means the timer is number 1,you can understand it as the timer's name; "50" means frequency)

>_<:Then only need to add timer-message listener in WndProc(...) function:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i;

switch (message)                   //判断消息类型
{
case WM_TIMER:                //时间消息
MyPaint(hdc);
break;
case WM_DESTROY:              //处理窗口结束消息
DeleteDC(mdc);
ReleaseDC(hWnd,hdc);
for(i=0;i<12;i++)
DeleteObject(girl[i]);
KillTimer(hWnd,1);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}


>_<:And now function MyPaint(...) will be carryed out at stated times.

#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[12];
HDC mdc,hdc;
int num;

// 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 (GetMessage(&msg, NULL, 0, 0)) //获取程序消息
{
TranslateMessage(&msg);//转换伪码及字符
DispatchMessage(&msg);//将控制权交给系统,再有系统决定负责处理消息的程序;
}

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        = 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    = "fe";
wcex.hIconSm        = NULL;

return RegisterClassEx(&wcex);//调用RegisterClassEx函数注册类别,返回一个“ATOM"形态的字符串
//此字符串即为类别名称”fe";
}
//============================================================================================

//============================================================================================
//按照前面所定义的窗口类别来建立并显示实际的程序窗口
//============================================================================================
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
char filename[20]="";
int i;
hInst = hInstance; // 把instance handle 储存在全局变量中;

hWnd = CreateWindow("fe","绘图窗口",WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
//-----------------------------------------------
//调用CreateWindow函数来建立一个窗口对象
//第一个参数就是窗口建立依据的类别名称
//-----------------------------------------------
if (!hWnd)
{
return FALSE;
}
//------------------------------------------------
//设定窗口的位置及窗口的大小,然后绘制显示在设备上
//-------------------------------------------------
MoveWindow(hWnd,10,10,616,440,true);//位置及大小
ShowWindow(hWnd, nCmdShow);//改定窗口显示时的状态
UpdateWindow(hWnd);//将窗口绘制在显示设备上

hdc=GetDC(hWnd);
mdc=CreateCompatibleDC(hdc);

for(i=0;i<12;i++)
{
sprintf(filename,"map%d.bmp",i);
girl[i]=(HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
}

num=0;
SetTimer(hWnd,1,50,NULL);
srand((unsigned)time(NULL));

MyPaint(hdc);

return TRUE;
}
//============================================================================================

//============================================================================================
//
//============================================================================================
void MyPaint(HDC hdc)
{
if(num>11)num=0;
for(int j=0;j<96;j++)
{
SelectObject(mdc,girl[ rand()%12]);
BitBlt(hdc,50*(j%12),j/12*50,650,450,mdc,0,0,SRCCOPY);
}
num++;
}
//============================================================================================

//============================================================================================
//在前面定义类别的时候把WndProc定义为消息处理函数(当某些外部消息发生时,会按消息的类型
//来决定该如何进行处理。此外该函数也是一个回叫函数(CALLBACK)(windows系统函数)每一个
//程序都会接收信息,选择性接受、处理;
//============================================================================================
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int i;

switch (message)                   //判断消息类型
{
case WM_TIMER:                //时间消息
MyPaint(hdc);
break;
case WM_DESTROY:              //处理窗口结束消息
DeleteDC(mdc);
ReleaseDC(hWnd,hdc);
for(i=0;i<12;i++)
DeleteObject(girl[i]);
KillTimer(hWnd,1);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//============================================================================================


main.cpp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: