/*************************************************************
*文件名:WindCar.cpp
*说明:本程序用转动的风车演示节日礼花的燃放效果
*最后一次修改时间:2009年10月29日
**************************************************************/
#include <windows.h>
#include "resource.h"
#include "WindCarClass.h"
bool bStart = false;
WindCar* arrWindCar[50] = {NULL};
int xPos = 0; //鼠标点击x位置
int yPos = 0; //鼠标点击y位置
int iTime = 0; //记录定时器启动后的周期数
int iWindCarNum = 0; //随即生成风车数量
int iWindCarLive = 0; //记录还健在的风车数量
HINSTANCE hIns;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG uMsg;
HWND hwnd;
hIns = hInstance;
WNDCLASS wndClass;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.hCursor = NULL;
wndClass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc = WindowProc;
wndClass.lpszClassName = "WindCar";
wndClass.lpszMenuName = NULL;
wndClass.style = CS_HREDRAW | CS_VREDRAW;
if(!RegisterClass(&wndClass))
{
MessageBox(NULL, TEXT("RegisterClass Failed!"), TEXT("Error"), MB_OK);
return 0;
}
/*创建窗口,其中定义窗口客户区大小为900*600*/
hwnd = CreateWindow(TEXT("WindCar"), TEXT("风车礼花"), WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX,
50, 30, 900 + GetSystemMetrics(SM_CXFIXEDFRAME) * 2,
600 + GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYCAPTION), NULL, NULL, hInstance,NULL);
/*显示和更新窗口*/
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
/*消息循环*/
while (GetMessage(&uMsg, NULL, 0, 0))
{
TranslateMessage(&uMsg);
DispatchMessage(&uMsg);
}
return (int)uMsg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
switch (uMsg)
{
case WM_CREATE:
break;
case WM_PAINT:
hDC = BeginPaint(hwnd, &ps);
if (bStart)
{
int i = 0;
HBITMAP hBp = LoadBitmap(hIns, MAKEINTRESOURCE(IDB_BITMAP1)); //加载位图
HBITMAP hComBp = CreateCompatibleBitmap(hDC, 900, 600); //创建兼容位图
HDC hComDC = CreateCompatibleDC(hDC); //创建兼容DC,用以选进兼容位图
HDC hBpDC = CreateCompatibleDC(hDC); //创建兼容DC,用以选进位图
/*选进位图*/
SelectObject(hBpDC, hBp);
SelectObject(hComDC, hComBp);
BitBlt(hComDC, 0, 0, 900, 600, hBpDC, 0, 0, SRCCOPY); //将兼容位图贴到兼容DC
for (i = 0; i < iWindCarNum - 1; ++i)
{
if (!(arrWindCar[i]->isOver()))
arrWindCar[i]->drawPic(hComDC, hwnd, iTime, i);
}
arrWindCar[iWindCarNum - 1]->drawPic(hComDC, hwnd, iTime, i);
BitBlt(hDC, 0, 0, 900, 600, hComDC, 0, 0, SRCCOPY); //将兼容DC中位图贴到窗口DC
/*销毁创建的资源*/
DeleteObject(hComBp);
DeleteObject(hBp);
DeleteDC(hComDC);
DeleteDC(hBpDC);
}
if (iWindCarLive == 0)
{
/*如果没有健在风车,则不用缓冲贴图*/
HBITMAP hBp = LoadBitmap(hIns, MAKEINTRESOURCE(IDB_BITMAP1));
HDC hdc = CreateCompatibleDC(hDC);
SelectObject(hdc, hBp);
BitBlt(hDC, 0, 0, 900, 600, hdc, 0, 0, SRCCOPY);
DeleteDC(hdc);
DeleteObject(hBp);
/*各种变量还原*/
/* KillTimer(hwnd, 1);
for (int k = 0; k < iWindCarNum; k++)
delete arrWindCar[k];
iWindCarNum = 0;
bStart = false;
xPos = 0;
yPos = 0;
iTime = 0; */
}
EndPaint(hwnd, &ps);
break;
case WM_TIMER:
++ iTime; //记录定时器已消耗的时间周期数
/*如果是第一次响应定时器,则创建风车基类指针数组*/
if (1 == iTime)
{
iWindCarNum = rand() % 30;
if (iWindCarNum < 10)
{
iWindCarNum += 20;
} //随机生成风车数量,不小于10个
iWindCarLive = iWindCarNum;
for (int i = 0; i < iWindCarNum - 1; i++)
{
arrWindCar[i] = new TriWindCar(rand() % 10 - 4, rand() % 5 - 3, xPos, yPos, 10 + rand() % 10, 100 + rand() % 100, 2 + rand() % 4, 10 + rand() % 10);
}
arrWindCar[iWindCarNum - 1] = new TriWindCar(0, 0, 50, 50, 10, 100, 2, 15);
/*随即生成不同类型风车*/
}
InvalidateRect(hwnd, NULL, FALSE);
break;
case WM_LBUTTONDOWN:
if (!bStart)
{
bStart = true;
POINTS pt = MAKEPOINTS(lParam);
xPos = pt.x;
yPos = pt.y;
iTime = 0;
SetTimer(hwnd, 1, 20, NULL);
}
else
{
for (int k = 0; k < iWindCarNum; k++)
delete arrWindCar[k];
iWindCarNum = 0;
bStart = false;
xPos = 0;
yPos = 0;
iTime = 0;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}