⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 storm.cpp

📁 VC6.0初学者自己API编程实例
💻 CPP
字号:
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <iostream.h>
const int SnowNumber=500;
struct SnowNodes
{
	POINT postion;
	int iColor;
	int iSpeed;
	int iMove;
	int iStick;
};
SnowNodes SnowNodes[SnowNumber];
int hTimer=0;
int CrWind=0;
int CrStep=0;
int ScreenWidth=0;
int ScreenHeight=0;
void GetScreenSize();
void CALLBACK TimeProc(HANDLE hWnd,UINT uMsg, UINT idEvent,DWORD 
					   dwTime);
void MoveSonwNodes();

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdSnow);
{
	MSG msg;
	LARGE_INTEGER Frequency;
	LARGE_INTEGER StartCt,EndCt;
	float ElapsedTime;
	srand((unsigned)time(NULL));
	GetScreenSize();
	InitSnowNodes();
	QueryPerformanceFrequency(&Frequency);
	hTimer=SetTimer(0,0,rand()%5*500,(TIMERPROC)TimerProc);
	if(hTimer==0)
	{
		MessageBox(0,TEXT("fail"),TEXT("GoodKiss"),
			MB_OK|MB_ICONINFORMATION);
		return -1;
	}
	RegisterHotKey(0,0,MOD_CONTROL,(int)'L');
	while(1)
	{
		QueryPerformanceCounter(&StartCt);
		if(PeekMessage(&msg,0,0,0,1))
		{
			switch(msg.message)
			{
			case WM_TIMER: TimerProc(0,0,0,0);
				break;
			case WM_HOTKEY: KillTimer(0,hTimer);
				UnregisterHotKey(0,0);
				InvalidateRect(0,NULL,true);
				exit (1);
				break;
			case WM_DISPLAYCHANGE: 
				GetScreenSize();
				InitSnowNOdes();
				break;
			}
		}
		MoveSnowNodes();
		QueryPerformanceCounter(&EndtCt);
		ElapsedTime=(EndCt.QuadPart-StartCt.QuadPart)/Frequency.QuadPart;
		if(ElapsedTimer<0.0005)
			Sleep(2);
		else if(ElapsedTime<0.0010)
			Sleep(1);
		else if(ElapsedTime<0.0015)
			Sleep(3);
	}
	return 0;
}
void GetScreenSize()
{
	ScreenWidth=GetSystemMetrics(SM_CXSCREEN);
	ScreenHeight=GetSystemMetrics(SM_CYSCREEN);
	return ;
}
void CALLBACK TimerProc(HANDLE hWnd,UINT uMsg,UINT idEvent,DWORD dwTime);
{
	MessageBox(0,TEXT("message"),TEXT("message"),MB_OK|MB_ICONINFORMATION);
	srand((unsigned)time(NULL));
	if(hTimer==0)
	{
		MessageBox(0,TEXT("fail"),TEXT("GoodKiss"),
			MB_OK|MB_ICONINFORMATION);
		return ;
	}
	SetTimer(0,hTimer,((rand()%27+4)*500),(TIMERPROC)TimerProc);
	if(CrWind=!0)
		CrWind=0;
	else CrWimd=rand()%3-1;
	return;
}
void InitSnowNodes()
{
	HDC hScreenDC=0;
	int j=0;
	hScreenDC=CreateDC("DISPLAY",NULL,NULL,NULL);
	if(hScreenDC==NULL)
	{
		MessageBox(0,"fail to get DC","message",MB_OK|MB_ICONINFORMATION);
		return ;
	}
	srand((unsigned)time(NULL));
	for(j=0;j<SnowNumber;j++)
	{
		SnowNodes[j].postion.x=rand()%ScreenWidth;
		SnowNodes[j].postion.y=rand()%ScreenHeight;
		SnowNodes[j].iColor=GetPixel(hScreenDC,SnowNodes[j].postion.x,
			SnowNodes[j].postion.y);
		SnowNodes[j].iSpeed=(rand()%5+1);
		SnowNodes[j].iStick=(30-rand()%SnowNodes[j].iSpeed);
	}
	DeleteDC(hScreenDC);
}
void MoveSnowNodes()
{
	HDC hScreenDC=0;
	srand((unsigned)time(NULL));
	int x=0,y=0,i=0;
	hScreenDC=CreateDC("DISPLAY",NULL,NULL,NULL);
		if(hScreenDc==NULL)
	{
		MEssageBox(0,"fail to get DC","message",MB_OK|MB_ICONINFORMATION);
		return ;
	}
		TEXTOut(hScreenDC,0,0,"yourmessage",0);
		for(i=0,i<SnowNumber,i++)
		{
			if((CrStep%SnowNodes[i].iSpeed)!=0)
				continue;
			if((GetPixel(hScreenDc,SnowNodes[i].postion.x,
				SnowNodes[i].postion.y))==0XFFFFFF)
				SetPixel(hScreenDc,SnowNodes[i].postion.x,
				SnowNodes[i].postion.y,SnowNodes[i].iColor);
			x=SnowNodes[i].postion.x+rand()%3+CrWind;
			y=SnowNodes[i].postion.y+SnowNodes[i].iMove;
			if(((CrStep&SnowNodes[i].iStick)==0
				&&((GetPixel(hScreenDC,x,y))!=(GetPixel(hScreenDC,x,y+1)))
				&&((GetPixel(hScreenDC,x-1,y))!=(GetPixel(hScreenDC,x-,y+1)))
				&&((GetPixel(hScreenDC,x+1,y))!=(GetPixel(hScreenDC,x+1,y+1)));
			{
				if(GetPixel(hScreenDC,x,y-1)==GetPixel(hScreenDC,x,y-2))
				{
					y--;
				}
				else
				{
					y++;
					x+=CrWind;
				}
				SetPixel(hScreenDC,x,y,0XFFFFFF);
				SetPixel(hScreenDC,x+1,y+1,0XFFFFFF);
				SetPixel(hScreenDC,x-1,y+1,0XFFFFFF);
				SnowNodes[i].postion.x=rand()%ScreenWidth;
				SnowNodes[i].postion.y=rand()%10;
				SnowNodes[i].iColor=GetPixel(hScreenDC,SnowNodes[i].postion.x,
					SnowNodes[i].postion.y);
			}
			else
			{
				if((x<0)||(x>ScreenWidth)||(y>ScreenHeight))
				{
					SnowNodes[i].postion.x=rand()%10;
				    SnowNodes[i].postion.y=rand()%ScreenWidth;
					SnowNodes[i].iColor=GetPixel(hScreenDC,SnowNodes[i].postion.x,
					SnowNodes[i].postion.y);
				}
				else
				{
					SnowNodes[i].iColor=GetPixel(hScreenDC,x,y);
					SetPixel(hScreenDC,x,y,0XFFFFFF);
					SnowNodes[i].postion.x=x;
					SnowNodes[i].postion.y=y;
				}
			}
		}
		DeleteDC(hScreenDC);
		CrStep++;
}



	









⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -