案頭上飄雪特效程式

來源:互聯網
上載者:User
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<iostream.h>
const int SnowNumber=500; //雪點數量
struct SnowNode
{
POINT postion;  //雪點位置
int   iColor; //先前的顏色
int   iSpeed; //下落速度
int   iMove; //下落距離
int   iStick; //粘貼度
};
SnowNode SnowNodes[SnowNumber]; //雪點數組
int   hTimer=0;
int   CrWind=0;
int   CrStep=0; //當前迴圈步數(用於限速)
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;//高效能定時器計數
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("建立定時器失敗"),TEXT("提示"),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(&EndCt);//執行運算後的計數值
  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("建立定時器失敗"),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); //粘貼度(幾次迴圈作一次粘貼連判斷
//  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.iSpeed)!=0)
   continue;
  //恢複上次被覆蓋點
  if((GetPixel(hScreenDC,SnowNodes.postion.x,SnowNodes.postion.y))==0XFFFFFF)
   SetPixel(hScreenDC,SnowNodes.postion.x,SnowNodes.postion.y,SnowNodes.iColor);
  //根據幾向作隨機飄落
  x=SnowNodes.postion.x+rand()%3+CrWind;
  y=SnowNodes.postion.y+SnowNodes.iMove;
  //積雪(停留)效果處理
  if(  ( (CrStep%SnowNodes.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))
     )
  {
   //稍稍調整座標
   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.postion.x=rand()%ScreenWidth;
   SnowNodes.postion.y=rand()%10;
   SnowNodes.iColor=GetPixel(hScreenDC,SnowNodes.postion.x,SnowNodes.postion.y);
   
  }
  else
  {
   if( (x<0) || (x>ScreenWidth) || (y>ScreenHeight))
   {
    SnowNodes.postion.x=(rand()%10);
    SnowNodes.postion.y=(rand()%ScreenWidth);
    SnowNodes.iColor=GetPixel(hScreenDC,SnowNodes.postion.x,SnowNodes.postion.y);
   }
   else
   {
    //儲存顏色並繪製雪點
    SnowNodes.iColor=GetPixel(hScreenDC,x,y);
    SetPixel(hScreenDC,x,y,0XFFFFFF);
    //此時儲存新雪點位置
    SnowNodes.postion.x=x;
    SnowNodes.postion.y=y;
   }
  }  
}
DeleteDC(hScreenDC);
CrStep++;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.