#include <windows.h>
#include <time.h>
#include "resource.h"
#define flower_num 50
int Position[flower_num][2] = {0}; //100个点
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("SNOW") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
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 ;
}
hwnd = CreateWindow( szAppName, // window class name
TEXT ("snow"), // window caption
WS_OVERLAPPEDWINDOW , // window style
600,// initial x position
350,// initial y position
200,// initial x size
300,// initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow(hwnd,SW_SHOWMINIMIZED);//显示窗口时是最小化状态
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 hbitmapimag,hbitmapmask,hbackupbitmap;
static HINSTANCE hinstance;
static int sx,sy; //屏幕宽度和高度
static int Vx,Vy; //垂直速度和 水平速度
static int temp_vx,temp_vy;
static int nCount;
BITMAP bitmap;
static HDC hdc,hdc_backup ,hdcMemImag,hdcMemMask;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE:
{
srand((unsigned)time(NULL));
SetTimer(hwnd,1,100,NULL);
hdc = CreateDC("DISPLAY",NULL,NULL,NULL);
sx = GetDeviceCaps(hdc,HORZRES); //得到屏幕宽度
sy = GetDeviceCaps(hdc,VERTRES); //得到屏幕高度
hinstance = ((LPCREATESTRUCT)lParam)->hInstance;
hbitmapimag = LoadBitmap(hinstance,MAKEINTRESOURCE(IDB_BITMAP1));
GetObject(hbitmapimag,sizeof(BITMAP),&bitmap);
hdc_backup = CreateCompatibleDC(hdc); //给原始屏幕备份
hbackupbitmap = CreateCompatibleBitmap(hdc,sx,sy);
SelectObject(hdc_backup,hbackupbitmap);
BitBlt(hdc_backup,0,0,sx,sy,hdc,0,0,SRCCOPY);
hdcMemImag = CreateCompatibleDC(hdc);
SelectObject(hdcMemImag,hbitmapimag);
hbitmapmask = LoadBitmap(hinstance,MAKEINTRESOURCE(IDB_BITMAPMASK));
GetObject(hbitmapmask,sizeof(BITMAP),&bitmap);
hdcMemMask = CreateCompatibleDC(hdc);
SelectObject(hdcMemMask,hbitmapmask);
BitBlt(hdcMemImag,0,0,32,32,hdcMemMask,0,0,SRCAND);
for(int i = 0; i < flower_num; i++)
{
Position[i][0] = rand()%sx;
Position[i][1] = rand()%sy;
}
Vx = rand()%4-2;
Vy = rand()%2+2;
}
return 0 ;
case WM_TIMER:
{
if(nCount > 200)
{
nCount = 0;
Vx = rand()%4-2;
Vy = rand()%2+2;
return 0;
}
for (int i = 0; i<flower_num; i++)
{
BitBlt(hdc,Position[i][0],Position[i][1],32,32,hdc_backup,Position[i][0],Position[i][1],SRCCOPY);
temp_vx = rand()%2-1+Vx*(i%3);
temp_vy = Vy*(i%3+1);
Position[i][0] = (Position[i][0]+temp_vx)%sx;
Position[i][1] = (Position[i][1]+temp_vy)%sy;
BitBlt(hdc,Position[i][0],Position[i][1],32,32,hdcMemMask,0,0,0x220326);
BitBlt(hdc,Position[i][0],Position[i][1],32,32,hdcMemImag,0,0,SRCPAINT);
}
}
return 0;
case WM_DESTROY:
KillTimer(hwnd,1);
BitBlt(hdc,0,0,sx,sy,hdc_backup,0,0,SRCCOPY);
DeleteDC(hdc);
DeleteDC(hdcMemImag);
DeleteDC(hdcMemMask);
DeleteDC(hdc_backup);
DeleteObject(hbitmapimag);
DeleteObject(hbitmapmask);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}