star

搜索

RSS

RSS Link

计数器

140249
Windows进程管理实验

Windows屏幕飘雪

Star posted @ 2011年1月23日 07:46 in Windows with tags Windows API SDK c , 7682 阅读
#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;
}

Avatar_small
Star 说:
2011年1月25日 04:15

囧~明明代码都有了,那就附上吧。。

依云 说:
2011年1月25日 05:13

可是我没有 Windows 呀。。。

Avatar_small
Star 说:
2011年1月25日 07:30

额。。原来是这样啊~错怪您了

Daniel Moulds 说:
2019年6月03日 19:05

Windows is the file which is installed as the application in the system to make it good for the future use. The whole window of the tag is trustmypaper.com reviews a good review of the better structure to make the points and the point system with file making.

Tyler Hamilton 说:
2020年3月20日 03:13

I often benefit from finding a website that serves up wonderful info as I like finding out new things. Frequently that I have been to this web site I've really liked the good article on this website. I will be certain to watch out for the following article.. Thanks for the wonderful post I am going to revisit in the future. australia virtual number

Tyler Hamilton 说:
2020年4月01日 22:47

There is so much in this article that I would never have thought of on my own. Your content gives readers things to think about in an interesting way. 안전놀이터

Tyler Hamilton 说:
2020年4月02日 19:20

This is important, though it's necessary to help you head over to it weblink: 그래프게임

Tyler Hamilton 说:
2020年4月02日 19:28

Can nicely write on similar topics! Welcome to      here you'll find out how it should look. 바둑이사이트

JKBOSE 7th Model Pap 说:
2022年7月07日 15:31

J&K 6th, 7th, 8th, 9th, 10th Class Last year Exam Question Paper of will help you to Understand the Paper Pattern and types of Questions asked in the Final Exam. JKBOSE 7th Model Paper JKBOSE 6th, 7th, 8th, 9th, 10th Class Exam Question Paper 2023 are usually Prepared Using the Textbooks Prescribed by the Jammu and Kashmir State Board of School Education,Students can Download the Latest J&K 6th, 7th, 8th, 9th, 10th Class Blueprint 2023 for All the Subject Such as Dogri, Hindi, English, Urdu etc, Experts Teacher Prepared by JKBOSE 6th, 7th, 8th, 9th, 10th Class Question Paper 2023 will help Students gain Confidence and make Them ready to face their High School Examination


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter