star

搜索

RSS

RSS Link

计数器

139730

Windows屏幕飘雪

2011年1月23日 07:46 | Comments(10) | Category:Windows | Tags:

#include <windows.h>
#include <stdlib.h>

#define UNICODE			//使用UNICODE字符 
#define _UNICODE

#define ID_TIME 1
#define NUMOFSNOW 214	//雪花数量 

typedef struct tagSNOW
{
	POINT pos;			//雪花坐标 
	int   r;			//雪花半径 
	int   xSpeed;		//x,y速度 
	int   ySpeed;
}SNOW, *PSNOW;

INT screen_x, screen_y;		//屏幕坐标 

#pragma comment(linker, "/subsystem: windows")

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
LPSTR lpCmdLine, int iCmdShow)
{
	TCHAR	 szAppName[] = TEXT ("屏幕飘雪");	//应用程序名 
	TCHAR    szTitle[]   = TEXT ("桌面窗口");	//窗口名 
	HWND	 hWnd;								//窗口句柄 
	MSG		 msg;								//消息循环 
	WNDCLASS wc;								//窗口类 
	
	INT x, y;
	
	wc.cbClsExtra	 = 0;	//不用额外窗口类和窗口空间 
	wc.cbWndExtra	 = 0;
	wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);   //黑色背景 
	wc.hCursor		 = LoadCursor (hInstance, IDC_ARROW);		//默认箭头 
	wc.hIcon		 = LoadIcon   (hInstance, IDI_APPLICATION); //默认图标
	wc.hInstance	 = hInstance;								//程序句柄 
	wc.lpfnWndProc	 = WndProc;									//窗口过程 
	wc.lpszClassName = szAppName;								//程序名称 
	wc.lpszMenuName  = NULL;									//菜单置空 
	wc.style		 = CS_HREDRAW | CS_VREDRAW; 				//水平|垂直对齐
	
	if (!RegisterClass (&wc))
	{
		MessageBox (NULL, TEXT("窗口注册失败!"), TEXT("ERROR"), 
		MB_OK | MB_ICONINFORMATION);
		return -1;
	}
	
	screen_x = GetSystemMetrics (SM_CXSCREEN);
	screen_y = GetSystemMetrics (SM_CYSCREEN);
	
	hWnd = CreateWindow (szAppName, szTitle, WS_OVERLAPPEDWINDOW | WS_VISIBLE, 
						0, 0, screen_x, screen_y, NULL, NULL, hInstance, NULL);
	
	if (!hWnd)
	{
		MessageBox (NULL, TEXT("创建窗口失败!"), TEXT("ERROR"), MB_OK | 
		MB_ICONINFORMATION);
		return -2; 
	}
	
	SetWindowPos (hWnd, HWND_TOPMOST, 0, 0, screen_x, screen_y, SWP_NOMOVE);
	
	while (GetMessage (&msg, NULL, 0, 0) > 0)
	{
		TranslateMessage (&msg);
		DispatchMessage  (&msg);
	}
	
	return msg.wParam;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static HDC		hScrDC, hSaveDC, hMemDC;
	static HBITMAP  hSaveBmp, hBmp;
	static SNOW		snows[NUMOFSNOW];
	static RECT 	rtScr;
	
	INT	i;
	
	switch (message)
	{
		case WM_CREATE:		//创造窗口时 
		{
			hScrDC   = CreateDC ("DISPLAY", NULL, NULL, NULL);
			//召唤2个DC,并以此为祭品,召唤2个BMP 
			hSaveDC  = CreateCompatibleDC(hScrDC);
			hMemDC   = CreateCompatibleDC(hScrDC);
			hSaveBmp = CreateCompatibleBitmap(hScrDC, screen_x, screen_y);
			hBmp     = CreateCompatibleBitmap(hScrDC, screen_x, screen_y);
			SelectObject (hSaveDC, hSaveBmp);
			SelectObject (hMemDC , hBmp);
			//将屏幕拷贝到SAVE 
			BitBlt (hSaveDC, 0, 0, screen_x, screen_y, hScrDC, 0, 0, SRCCOPY);
			
			SetRect (&rtScr, 0, 0, screen_x, screen_y);
			srand ((unsigned)GetTickCount());
			//随机变量数据 
			for (i = 0; i < NUMOFSNOW; ++i)
			{
				snows[i].pos.x  = rand() % screen_x;
				snows[i].pos.y  = rand() % (screen_y / 3);	//屏幕1/3以上召唤雪 
				snows[i].r 	    = rand() % 4 + 1;			//1~4 
				snows[i].xSpeed = rand() % 2 - 1;			//0~1
				snows[i].ySpeed = rand() % 4 + 2;			//2~4
			}
			ShowCursor (FALSE);
			SetTimer (hWnd, ID_TIME, 12, NULL);
		}
		break;
		case WM_TIMER:
		{
			for(i = 0; i < NUMOFSNOW; ++i)
			{
				snows[i].pos.x += snows[i].xSpeed;		//移动 
				snows[i].pos.y += snows[i].ySpeed;
				if (!PtInRect (&rtScr, snows[i].pos))	//如果跑出屏幕外
				{
					snows[i].pos.x = rand() % screen_x;
					snows[i].pos.y = rand() % (screen_y / 3);
				}
			}
			//将存好的放入内存中 
			BitBlt (hMemDC, 0, 0, screen_x, screen_y, hSaveDC, 0, 0, SRCCOPY);	
			//创建雪 
			for(i = 0; i < NUMOFSNOW; ++i)
			{
				SelectObject (hMemDC, GetStockObject (NULL_PEN));
				Ellipse (hMemDC, snows[i].pos.x, snows[i].pos.y, 
				snows[i].pos.x + 2*snows[i].r, snows[i].pos.y + 2*snows[i].r);
			}
			//将雪放在屏幕上 
			BitBlt (hScrDC, 0, 0, screen_x, screen_y, hMemDC, 0, 0, SRCCOPY);	
		}
		break;
		case WM_LBUTTONDOWN:
			SendMessage (hWnd, WM_CLOSE, 0, 0);
			break;
		case WM_DESTROY:
		{
			//清理所有垃圾 
			ShowCursor   	(TRUE);
			KillTimer    	(hWnd, ID_TIME);
			DeleteObject 	(hBmp);
			DeleteObject 	(hSaveBmp);
			DeleteDC 	 	(hSaveDC);
			DeleteDC	 	(hScrDC);
			InvalidateRect  (NULL, NULL, TRUE);
			PostQuitMessage (0);
		}
		break;
		default:
			return DefWindowProc (hWnd, message, wParam, lParam);
	}
	return 0;
}