用C++写的在桌面上飘雪的特效程序
# include〈windows . h 〉
# include〈time . h 〉
# include〈stdlib . h 〉
# include〈iostream . h〉
const int SnowNumber = 500//雪点的数量
struct SnowNode
{
点位置;//雪斑位置
int iColor;//previous color
int is peed;//下降速度
inti mov;//下落距离
int iStick;//粘贴度数
};
snow node snow nodes[snow number];//雪点数组
int hTimer = 0;
int Cr wind = 0;
int Cr step = 0;//当前循环步数(限速)
int screen width = 0;//屏幕宽度
int Screen height = 0;//屏幕高度
void GetScreenSize();
void回调TimerProc(HANDLE hWnd,UINT uMsg,UINT idEvent,DWORD dwTime);
void InitSnowNodes();
void MoveSnowNodes();
int WINAPI WinMain(h instance h instance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
MSG MSG;//标准windows消息
LARGE_INTEGER频率;//高性能定时器频率
large _ integer start CT,end CT//高性能计时器计数
float elapsed time;//时间间隔
srand((无符号)time(null));
GetScreenSize();
InitSnowNodes();
QueryPerformanceFrequency(& Frequency);
hTimer=SetTimer(0,0,rand()%5*500,(timer proc)timer proc);
if(h timer = = 0)
{
Messagebox(0,TEXT("创建定时器失败"),text ("prompt "),MB _ OK | MB _ icon information);
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:timer proc(
break;//是时候改变预设风向了
casewm _ hotkey:kill timer(0,h timer);//删除随机风向定时器
UnregisterHotKey(0,0);//删除退出热键
invalidate rect (0,null,true);
退出(1);
break;
case WM _ display change:
GetScreenSize();//重新调整屏幕大小
InitSnowNodes();//初始化雪点数组
break;
}
}
MoveSnowNodes();
QueryPerformanceCounter(& EndCt);//运算后计数值
经过时间=(endct . quad part-startct . quad part)/frequency . quad part;
if((ElapsedTime〈0.0005))
Sleep(2);//简单限速
else if(经过时间< 0.0010)
sleep(1);
else if(elapsed time〈0.0015)
Sleep(3);
}
//MessageBox(0,TEXT ("message "),TEXT ("message "),MB _ OK | MB _ icon information);
返回0;
}
void GetScreenSize()
{
screen width = getsystem metrics(SM _ CX screen);
screen height = getsystem metrics(SM _ cy screen);
退货;
}
void回调定时器proc (handle hwnd,uint umsg,uint id event,dword dwtime)
{
//messagebox(0,TEXT ("message "),text ("message "),MB _ OK | MB _ icon information);
srand((无符号)time(NULL));
if(h timer = = 0)
{
Messagebox(0,TEXT("创建定时器失败"),text ("prompt "),MB _ OK | MB _ icon information);
退货;
}
SetTimer(0,hTimer,((rand()%27+4)*500),(timer proc)timer proc);////重置下一次风向改变时间
//修改风向
if(CrWind!= 0)
Cr wind = 0;
else
Cr wind = rand()% 3-1;
退货;
}
void InitSnowNodes()
{
HDC hScreenDC = 0;
int j = 0;
hScreenDC = CreateDC(" DISPLAY ",NULL,NULL,NULL);
if(hscreendc = = null)
{
messagebox(0,"未能获取屏幕dc!",“信息”,MB _ OK | MB _ icon error);
退货;
}
srand((无符号)time(NULL));
for(j = 0;j〈snow number;j++)
{
snow nodes[j]. position . x = rand()% screen width;
snow nodes[j]. position . y = rand()% screen height;[br/]snow nodes[j]。iColor=GetPixel(hScreenDC,snow nodes[j]. position . x,snow nodes[j]. position . y);[br/]snow nodes[j]。is peed =(rand()% 5+1);//每次下落距离为(1-5)
snownodes [j]。istick = (30-rand ()% snownodes [j].ispeed);//粘贴度(做几个周期的粘贴连接判断
/cout < < snow nodes [j].posting.x < < "y:" <雪节点[j]。posting.y < < endl
}
DeleteDC(hScreenDC);
}
void MovesNowNodes()
{
/MessageBox(0,TEXT ("message "),text ("message "),MB _ OK | MB _ icon information);
HDC hScreenDC = 0;
srand((无符号)time(NULL));
int x=0,y=0,I = 0;
hScreenDC = CreateDC(" DISPLAY ",NULL,NULL,NULL);
if(hscreendc = = null)
{
messagebox(0,"未能获取屏幕dc!",“信息”,MB _ OK | MB _ icon error);
退货;
}
/Textout(hscreendc,0,0,“虽然大检查和克格勃很顺利”,0);
for(I = 0;我的雪数;++)
{
/控制雪点的下落速度
if ((crstep% snownodes [i])。ispeed)!=0)
继续;
//如果((GetPixel (HSCreedC,Snownodes [I])则恢复最后一个覆盖点
。Postion.x,Snownodes [I]。position . y))= = 0x ffffff)
set pixel(HSCreedC,HSCreedC)
//根据几个方向随机下降
x = snownodes [I]。position . x+rand()% 3+Cr wind;
y = snow nodes[I]. position . y+snow nodes[I]。iMove
//snow (stay)效果处理
if ((crstep% snownodes [I])。istick)= = 0)
&((get pixel(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))
[
/稍微调整一下坐标
if (getpixel (hscreendc,x,y-1) = = getpixel (hscreendc)
}
else
{
if(get pixel(hScreenDC,x,y-1)= = get pixel(hScreenDC,x,y-2))
y++;
x+= Cr wind;
}
/绘制三个雪花点
SETPixels (hscreendc,x,y,0x ffffff);
SetPixel(hScreenDC,x+1,y+1,0x ffffff);
SetPixel(hScreenDC,x-1,y+1,0x ffffff);
//重生雪点
snownodes [i]。posting . x = rand()% screen width;
SnowNodes[I]. position . y = rand()% 10;
SnowNodes[i]。iColor=GetPixel(hScreenDC,snow nodes[I]. position . x,snow nodes[I]. position . y);
}
else
{
if((x〈0)| |(x〈屏幕宽度)| |(y〈屏幕高度))
{
SnowNodes[I]. position . x =(rand()% 10);
SnowNodes[I]. position . y =(rand()% screen width);
SnowNodes[i]。iColor=GetPixel(hScreenDC,snow nodes[I]. position . x,snow nodes[I]. position . y);
}
else
{
/保存颜色并绘制雪点
snownodes [i]。icon = getpixel (hscreendc,x,y);
SetPixel(hScreenDC,x,y,0x ffffff);
//此时保存新的雪点位置
雪节点[i]。posting.x = x
SnowNodes[I]. position . y = y;
}
}
}
DeleteDC(hScreenDC);
Cr step++;
}
0条评论