//FlipMotion.c
#include<windows.h>
#include"resource.h"
#define ID_TIMER 100
#define NUM 7
//全局变量(用来控制每一次缩放的大小,注意:只是缩放位图的高度、宽度不变)
int height[] = {144, 88, 32, 10, 75, 137, 200};
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
static TCHAR szAppName[] = TEXT("FlipMotion");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
int cxScreen, cyScreen;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
cxScreen = GetSystemMetrics(SM_CXSCREEN);
cyScreen = GetSystemMetrics(SM_CYSCREEN);
hwnd = CreateWindow(szAppName,
TEXT("FlipMotion Demo"),
WS_POPUP,
(cxScreen - 200) / 2,
(cyScreen - 200)/ 2,
200,
200,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//回调函数中的静态局部变量
static HBITMAP hBmp[2]; //存放正反面位图句柄
static SIZE sBmp, sClient; //位图大小、客户区大小(用于缩放)
static POINT ptBmp; //位图位置(保证始终居中显示)
static int curIndex, isFirst; //当前索引(height数组)、是否为正面(用于交换位图)
BITMAP bmp;
HINSTANCE hInstance;
HDC hdc, hdcMem;
PAINTSTRUCT ps;
switch(message)
{
case WM_CREATE:
//加载位图
hInstance = ((LPCREATESTRUCT)lParam)->hInstance;
hBmp[0] = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
hBmp[1] = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP2));
GetObject(hBmp[0], sizeof(BITMAP), &bmp);
sBmp.cx = bmp.bmWidth;
sBmp.cy = bmp.bmHeight;
//设置计时器
SetTimer(hwnd, ID_TIMER, 120, NULL);
return 0;
case WM_SIZE:
//获取客户区大小
sClient.cx = LOWORD(lParam);
sClient.cy = HIWORD(lParam);
//修改位图当前位置坐标
ptBmp.x = (sClient.cx - sBmp.cx) / 2;
ptBmp.y = (sClient.cy - sBmp.cy) / 2;
return 0;
case WM_PAINT:
//按照一定比例缩放位图
hdc = BeginPaint(hwnd, &ps);
hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBmp[isFirst]);
SetStretchBltMode(hdc, COLORONCOLOR);
StretchBlt(hdc, ptBmp.x, ptBmp.y, sBmp.cx, sBmp.cy, hdcMem, 0, 0, 200, 200, SRCCOPY);
DeleteDC(hdcMem);
EndPaint(hwnd, &ps);
return 0;
case WM_TIMER:
//修改当前显示的位图高度
sBmp.cy = height[curIndex];
//如果位图高度变为最小(也就是几乎看不太清的程序),就交换位图
if(sBmp.cy == height[3])
{
isFirst = !(isFirst & 1);
}
//如果位图高度之前是原始大小,那么应该停顿一会儿,这里停顿当前线程1秒
else if(sBmp.cy == height[0])
{
Sleep(1000);
}
//修改位图显示位置,保证居中显示
ptBmp.y = (sClient.cy - sBmp.cy) / 2;
//索引下一个高度(height)
curIndex = (curIndex + 1) % NUM;
//重绘
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_DESTROY:
//回收资源
KillTimer(hwnd, ID_TIMER);
{
int i=0;
for(;i<2; i++)
{
if(hBmp[i])
DeleteObject(hBmp[i]);
}
}
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
- 1
- 2
- 3
- 4
前往页