程序代码:
#include <windows.h>
#include <strsafe.h>
#include "resource.h"
#define BTN_START (1)//启动按钮ID
#define BTN_END (2)//
#define BUF_SIZE (40)//文件名缓冲区大小
#define BMP_COUNT (10)//位图的数量
#define ID_TIMER (1)//定时器ID
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,LPARAM lParam)
{
static HBITMAP hBitMap[BMP_COUNT];//位图句柄
static BITMAP bitMap[BMP_COUNT];//位图信息
static int cxClient, cyClient, i;
TCHAR szBmpName[BUF_SIZE] = {0};
PAINTSTRUCT ps;
HDC hdc, mdc;
switch (msg)
{
case WM_CREATE:
for (i = 0; i < BMP_COUNT; ++i)
{//获取位图信息
StringCchPrintf(szBmpName, _countof(szBmpName), TEXT("%d.bmp"),i);
hBitMap[i] = (HBITMAP)LoadImage(NULL, szBmpName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(hBitMap[i], sizeof(BITMAP), &bitMap[i]);
}
CreateWindow(TEXT("button"),
TEXT("开始"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, 40, 20,
hwnd,
(HMENU) BTN_START,
((LPCREATESTRUCT)lParam)->hInstance,
0);
CreateWindow(TEXT("button"),
TEXT("结束"),
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 21, 40, 20,
hwnd,
(HMENU) BTN_END,
((LPCREATESTRUCT)lParam)->hInstance,
0);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case BTN_START:
SetTimer(hwnd, ID_TIMER, 1000, NULL);
break;
case BTN_END:
KillTimer(hwnd, ID_TIMER);
break;
}
case WM_TIMER:
hdc = GetDC(hwnd);
mdc = CreateCompatibleDC(hdc);
i %= BMP_COUNT;
SelectObject(mdc, hBitMap[i]);
BitBlt(hdc, (cxClient - bitMap[i].bmWidth) / 2,
(cyClient - bitMap[i].bmHeight) / 2, bitMap[i].bmWidth,
bitMap[i].bmHeight, mdc, 0, 0, SRCCOPY);
DeleteDC(mdc);
DeleteDC(hdc);
++i;
break;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
mdc = CreateCompatibleDC(hdc);
i %= BMP_COUNT;
SelectObject(mdc, hBitMap[i]);
BitBlt(hdc, (cxClient - bitMap[i].bmWidth) / 2,
(cyClient - bitMap[i].bmHeight) / 2, bitMap[i].bmWidth,
bitMap[i].bmHeight, mdc, 0, 0, SRCCOPY);
EndPaint(hwnd,&ps);
DeleteDC(mdc);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //主窗口客户区内的背景颜色,这里是黑色。
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW); //鼠标在客户区内的形状。
wndclass.hIcon = LoadIcon(hInstance,(LPCTSTR)IDI_ICON1); //窗口最小化的图标,这里的图标是金钟。
wndclass.hInstance = hInstance; //应用程序实力的句柄由WinMain函数传进来.
wndclass.lpfnWndProc = WndProc; //指明回调函数,要回调自己定义的那个函数,发生消息过程的那个函数。
wndclass.lpszClassName = TEXT("chuangkou001"); //给自己设计的窗口类注册一个类名,后面创建窗口直接调用这个类名就能显示自己设置的特征。
wndclass.lpszMenuName = NULL; //系统菜单,如系统、帮助等左上角的下拉菜单,没有就用NULL代替。
wndclass.style = CS_HREDRAW | CS_VREDRAW; //类的样式,可以是任何组合。
if(! RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("注册类失败"), TEXT("错误"),MB_OK);
return -1;
}
//创建窗口
hwnd = CreateWindow(TEXT("chuangkou001"),
TEXT("动画窗口"),
WS_OVERLAPPEDWINDOW,
10,0,711,800,
NULL,
NULL,
hInstance,
NULL);
if(!hwnd) //如果窗口创建失败,弹出提示窗口。
{
MessageBox(NULL, TEXT("创建窗口失败"), TEXT("错误"), MB_OK);
return -1;
}
//显示并刷新窗口
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
//消息循环。
while(GetMessage(&msg, NULL, 0, 0) >0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}