#include〈windows.h〉
#include〈time.h〉
#include〈stdlib.h〉
#include〈iostream.h〉
const int SnowNumber=500; //雪點數(shù)量
struct SnowNode
{
POINT postion; //雪點位置
int iColor; //先前的顏色
int iSpeed; //下落速度
int iMove; //下落距離
int iStick; //粘貼度
};
SnowNode SnowNodes[SnowNumber]; //雪點數(shù)組
int hTimer=0;
int CrWind=0;
int CrStep=0; //當前循環(huán)步數(shù)(用于限速)
int ScreenWidth=0; //屏幕寬度
int ScreenHeight=0; //屏幕高度
void GetScreenSize();
void CALLBACK TimerProc(HANDLE hWnd,UINT uMsg,UINT idEvent,DWORD dwTime);
void InitSnowNodes();
void MoveSnowNodes();
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
MSG msg; //標準windows消息
LARGE_INTEGER Frequency; //高性能定時器頻率
LARGE_INTEGER StartCt,EndCt;//高性能定時器計數(shù)
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(“創(chuàng)建定時器失敗“),TEXT(“提示“),MB_OK|MB_ICONINFORMATION);
return -1;
}
RegisterHotKey(0,0,MOD_CONTROL,(int)’L’);
while(1)
{
QueryPerformanceCounter(&StartCt); //執(zhí)行運算前計數(shù)值
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(); //初始化雪點的數(shù)組
break;
}
}
MoveSnowNodes();
QueryPerformanceCounter(&EndCt);//執(zhí)行運算后的計數(shù)值
ElapsedTime=(EndCt.QuadPart-StartCt.QuadPart)/Frequency.QuadPart;
if((ElapsedTime〈0.0005))
Sleep(2); //簡單限速
else if(ElapsedTime〈0.0010)
Sleep(1);
else if(ElapsedTime〈0.0015)
Sleep(3);
}
//MessageBox(0,TEXT(“消息“),TEXT(“消息“),MB_OK|MB_ICONINFORMATION);
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(“消息“),TEXT(“消息“),MB_OK|MB_ICONINFORMATION);
srand((unsigned)time(NULL));
if(hTimer==0)
{
MessageBox(0,TEXT(“創(chuàng)建定時器失敗“),TEXT(“提示“),MB_OK|MB_ICONINFORMATION);
return ;
}
SetTimer(0,hTimer,((rand()%27+4)*500),(TIMERPROC)TimerProc); //// 重設下次風向改變時間
//修改風向
if(CrWind!=0)
CrWind=0;
else
CrWind=rand()%3-1;
return ;
}
void InitSnowNodes()
{
HDC hScreenDC=0;
int j=0;
hScreenDC=CreateDC(“DISPLAY“,NULL,NULL,NULL);
if(hScreenDC==NULL)
{
MessageBox(0,“獲取屏幕DC失敗!“,“信息“,MB_OK|MB_ICONERROR);
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); //每次下落距離(1-5)
SnowNodes[j].iStick=(30-rand()%SnowNodes[j].iSpeed); //粘貼度(幾次循環(huán)作一次粘貼連判斷
// cout〈〈SnowNodes[j].postion.x〈〈“ Y:“〈〈SnowNodes[j].postion.y〈〈endl;
}
DeleteDC(hScreenDC);
}
void MoveSnowNodes()
{
// MessageBox(0,TEXT(“消息“),TEXT(“消息“),MB_OK|MB_ICONINFORMATION);
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,“獲取屏幕DC失敗!“,“信息“,MB_OK|MB_ICONERROR);
return ;
}
// TextOut(hScreenDC,0,0,“雖然大檢查順順藤摸瓜克格勃呀加“,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);
//根據(jù)幾向作隨機飄落
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-1,y+1)))
&&( (GetPixel(hScreenDC,x+1,y))!=GetPixel(hScreenDC,x+1,y+1))
)
{
//稍稍調(diào)整坐標
if(GetPixel(hScreenDC,x,y-1)==GetPixel(hScreenDC,x,y-2))
{
y--;
}
else
{
if(GetPixel(hScreenDC,x,y-1)==GetPixel(hScreenDC,x,y-2))
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++;
}
#include〈time.h〉
#include〈stdlib.h〉
#include〈iostream.h〉
const int SnowNumber=500; //雪點數(shù)量
struct SnowNode
{
POINT postion; //雪點位置
int iColor; //先前的顏色
int iSpeed; //下落速度
int iMove; //下落距離
int iStick; //粘貼度
};
SnowNode SnowNodes[SnowNumber]; //雪點數(shù)組
int hTimer=0;
int CrWind=0;
int CrStep=0; //當前循環(huán)步數(shù)(用于限速)
int ScreenWidth=0; //屏幕寬度
int ScreenHeight=0; //屏幕高度
void GetScreenSize();
void CALLBACK TimerProc(HANDLE hWnd,UINT uMsg,UINT idEvent,DWORD dwTime);
void InitSnowNodes();
void MoveSnowNodes();
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
MSG msg; //標準windows消息
LARGE_INTEGER Frequency; //高性能定時器頻率
LARGE_INTEGER StartCt,EndCt;//高性能定時器計數(shù)
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(“創(chuàng)建定時器失敗“),TEXT(“提示“),MB_OK|MB_ICONINFORMATION);
return -1;
}
RegisterHotKey(0,0,MOD_CONTROL,(int)’L’);
while(1)
{
QueryPerformanceCounter(&StartCt); //執(zhí)行運算前計數(shù)值
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(); //初始化雪點的數(shù)組
break;
}
}
MoveSnowNodes();
QueryPerformanceCounter(&EndCt);//執(zhí)行運算后的計數(shù)值
ElapsedTime=(EndCt.QuadPart-StartCt.QuadPart)/Frequency.QuadPart;
if((ElapsedTime〈0.0005))
Sleep(2); //簡單限速
else if(ElapsedTime〈0.0010)
Sleep(1);
else if(ElapsedTime〈0.0015)
Sleep(3);
}
//MessageBox(0,TEXT(“消息“),TEXT(“消息“),MB_OK|MB_ICONINFORMATION);
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(“消息“),TEXT(“消息“),MB_OK|MB_ICONINFORMATION);
srand((unsigned)time(NULL));
if(hTimer==0)
{
MessageBox(0,TEXT(“創(chuàng)建定時器失敗“),TEXT(“提示“),MB_OK|MB_ICONINFORMATION);
return ;
}
SetTimer(0,hTimer,((rand()%27+4)*500),(TIMERPROC)TimerProc); //// 重設下次風向改變時間
//修改風向
if(CrWind!=0)
CrWind=0;
else
CrWind=rand()%3-1;
return ;
}
void InitSnowNodes()
{
HDC hScreenDC=0;
int j=0;
hScreenDC=CreateDC(“DISPLAY“,NULL,NULL,NULL);
if(hScreenDC==NULL)
{
MessageBox(0,“獲取屏幕DC失敗!“,“信息“,MB_OK|MB_ICONERROR);
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); //每次下落距離(1-5)
SnowNodes[j].iStick=(30-rand()%SnowNodes[j].iSpeed); //粘貼度(幾次循環(huán)作一次粘貼連判斷
// cout〈〈SnowNodes[j].postion.x〈〈“ Y:“〈〈SnowNodes[j].postion.y〈〈endl;
}
DeleteDC(hScreenDC);
}
void MoveSnowNodes()
{
// MessageBox(0,TEXT(“消息“),TEXT(“消息“),MB_OK|MB_ICONINFORMATION);
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,“獲取屏幕DC失敗!“,“信息“,MB_OK|MB_ICONERROR);
return ;
}
// TextOut(hScreenDC,0,0,“雖然大檢查順順藤摸瓜克格勃呀加“,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);
//根據(jù)幾向作隨機飄落
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-1,y+1)))
&&( (GetPixel(hScreenDC,x+1,y))!=GetPixel(hScreenDC,x+1,y+1))
)
{
//稍稍調(diào)整坐標
if(GetPixel(hScreenDC,x,y-1)==GetPixel(hScreenDC,x,y-2))
{
y--;
}
else
{
if(GetPixel(hScreenDC,x,y-1)==GetPixel(hScreenDC,x,y-2))
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++;
}