版权声明:转载请注明出处。 https://blog.csdn.net/baidu_38304645/article/details/82826853
运行结果:
源代码:
#include <windows.h>
#include <tchar.h> //使用swprintf_s函数所需的头文件
#pragma comment(lib, "winmm.lib") //调用PlaySound函数所需库文件t
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE L"动画演示_消息循环"
//全局设备环境句柄g_hdc与全局内存DC句柄g_mdc,并声明了位图数组g_hSprite用来储存各人物位图
HINSTANCE hInst;
HDC g_hdc;
HDC g_mdc;
HBITMAP g_hSprite[12]; //12张图片DC
DWORD g_tPre = 0, g_tNow = 0; //长整型
int g_iNum;
HWND hwnd;
int MyWindowClass(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyDraw(HWND);
/*****************************************************************************************************************
在不同的应用程序中,在此处添加相关的全局变量
******************************************************************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPreInstace,
LPSTR lpCmdLine, int nCmdShow)
{
MyWindowClass(hInstance);
PlaySound(L"sound.wav", NULL, SND_FILENAME| SND_ASYNC| SND_LOOP); //循环播放背景音乐
if(!InitInstance(hInstance, nCmdShow))
return FALSE;
MSG msg = {0}; //初始化
while(msg.message != WM_QUIT)
{
//检测目前是否有需要处理的消息,有返回非0,否则返回0
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
g_tNow = GetTickCount(); //获取当前系统时间
if(g_tNow - g_tPre >= 100) //当此次循环运行与上次绘图时间相差0.1s进行重绘操作
{
MyDraw(hwnd);
g_tPre = GetTickCount(); //记录此次绘图时间,供下次游戏循环中判断是否达到画面更新操作
}
}
}
return 0;
}
int MyWindowClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"gamebase";
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
hwnd = CreateWindow(L"gamebase", WINDOW_TITLE, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
if(!hwnd)
return FALSE;
MoveWindow(hwnd, 10, 10, WINDOW_WIDTH, WINDOW_HEIGHT, true);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
g_hdc = GetDC(hwnd);
g_mdc = CreateCompatibleDC(g_hdc);
//载入各个位图,并建立定时器,间隔0.09秒发送WM_TIMER消息
wchar_t filename[20];
for(int i = 0; i != 12; ++i)
{
//调用swprintf_s函数,组装出对应的图片文件名称
swprintf_s(filename, L"%d.bmp", i);
g_hSprite[i] = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, WINDOW_WIDTH, WINDOW_HEIGHT, LR_LOADFROMFILE);
}
g_iNum = 0; //图片号
MyDraw(hwnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_KEYDOWN:
if(wParam == VK_ESCAPE)
DestroyWindow(hwnd); //销毁窗口
break;
/**************************************************************************************************************
在退出程序前,往往在此处删除创建的相关资源
***************************************************************************************************************/
case WM_DESTROY:
for(int i = 0; i != 12; ++i) //删除位图资源
DeleteObject(g_hSprite[i]);
DeleteDC(g_mdc); //删除内存DC
ReleaseDC(hwnd, g_hdc); //释放设备环境
PostQuitMessage(0); //在消息队列中放入0
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
/***************************************************************************************************************
在函数MyDraw()中进行相关绘制工作
****************************************************************************************************************/
void MyDraw(HWND hwnd)
{
if(g_iNum == 12) //判断是否超过最大图号,保证循环播放图片
g_iNum = 0;
SelectObject(g_mdc, g_hSprite[g_iNum]); //选择对应的位图
BitBlt(g_hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, g_mdc, 0, 0, SRCCOPY);
++g_iNum;
}