Special Program for snow float on the desktop

Source: Internet
Author: User
# Include <windows. h>
# Include <time. h>
# Include <stdlib. h>
# Include <iostream. h>
Const int snownumber = 500; // Number of snow points
Struct snownode
{
Point postion; // the location of the snow point.
Int icolor; // previous color
Int ispeed; // Fall speed
Int imove; // The dropping distance.
Int istick; // paste degree
};
Snownode snownodes [snownumber]; // snow point Array
Int htimer = 0;
Int crwind = 0;
Int crstep = 0; // number of current cyclic steps (used for speed limit)
Int screenwidth = 0; // screen width
Int screenheight = 0; // screen height
Void getscreensize ();
Void callback timerproc (handle hwnd, uint umsg, uint idevent, DWORD dwtime );
Void initsnownodes ();
Void movesnownodes ();
Int winapi winmain (hinstance,
Hinstance hprevinstance,
Lpstr lpcmdline,
Int ncmdshow
)
{

MSG; // standard Windows message
Large_integer frequency; // high-performance timer frequency
Large_integer startct, endct; // high-performance timer count
Float elapsedtime; // Interval
Srand (unsigned) Time (null ));
Getscreensize ();
Initsnownodes ();
Queryperformancefrequency (& frequency );
Htimer = settimer (500, Rand () % 5 *, (timerproc );
If (htimer = 0)
{
MessageBox (0, text ("failed to create timer"), text ("prompt"), mb_ OK | mb_iconinformation );
Return-1;
}
Registerhotkey (0, 0, mod_control, (INT) 'l ');
While (1)
{
Queryperformancecounter (& startct); // The count before the operation is executed.
If (peekmessage (& MSG, 0, 0, 1 ))
{
Switch (msg. Message)
{
Case wm_timer: timerproc (0, 0, 0 );
Break; // the preset time for changing the wind direction has reached
Case wm_hotkey: killtimer (0, htimer); // Delete the random wind direction Timer
Unregisterhotkey (); // Delete the exit hotkey
Invalidaterect (0, null, true );
Exit (1 );
Break;
Case wm_displaychange:
Getscreensize (); // obtain the screen size again
Initsnownodes (); // initializes an array of snow points.
Break;
}
}
Movesnownodes ();
Queryperformancecounter (& endct); // The Count value after the operation is executed.
Elapsedtime = (endct. QuadPart-StartCt.QuadPart)/frequency. quadpart;
If (elapsedtime <0.0005 ))
Sleep (2); // simple speed limit
Else if (elapsedtime <0.0010)
Sleep (1 );
Else if (elapsedtime <0.0015)
Sleep (3 );
}
// MessageBox (0, text ("message"), text ("message"), 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 ("message"), text ("message"), mb_ OK | mb_iconinformation );
Srand (unsigned) Time (null ));
If (htimer = 0)
{
MessageBox (0, text ("failed to create timer"), text ("prompt"), mb_ OK | mb_iconinformation );
Return;
}
Settimer (0, htimer, (RAND () % 27 + 4) * 500), (timerproc); // reset the next wind direction change time
// Modify the wind direction
If (crwind! = 0)
Crwind = 0;
Else
Crwind = rand () % 3-1;
Return;
}
Void initsnownodes ()
{
HDC hscreendc = 0;
Int J = 0;
Hscreendc = createdc ("display", null );
If (hscreendc = NULL)
{
MessageBox (0, "An error occurred while obtaining the screen DC! "," Information ", 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); // the distance between each fall (1-5)
Snownodes [J]. istick = (30-rand () % snownodes [J]. ispeed); // paste degree (several cycles for one-time Pasting and connection judgment)
// Cout <snownodes [J]. postion. x <"Y:" <snownodes [J]. postion. Y <Endl;

}
Deletedc (hscreendc );
}
Void movesnownodes ()
{
// MessageBox (0, text ("message"), text ("message"), mb_ OK | mb_iconinformation );
HDC hscreendc = 0;
Srand (unsigned) Time (null ));
Int x = 0, y = 0, I = 0;
Hscreendc = createdc ("display", null );
If (hscreendc = NULL)
{
MessageBox (0, "An error occurred while obtaining the screen DC! "," Information ", mb_ OK | mb_iconerror );
Return;
}
// Textout (hscreendc, 0, 0, "although the big check is successful, it is 0 );
For (I = 0; I <snownumber; I ++)
{
// Control the speed of snow point descent
If (crstep % snownodes. Ispeed )! = 0)
Continue;
// Restore the last overwritten Vertex
If (getpixel (hscreendc, snownodes. Postion. X, snownodes. Postion. y) = 0 xffffff)
Setpixel (hscreendc, snownodes. Postion. X, snownodes. Postion. Y, snownodes. Icolor );
// Perform random falling based on several directions
X = snownodes. Postion. x + rand () % 3 + crwind;
Y = snownodes. Postion. Y + snownodes. Imove;
// Handle the snow Effect
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 ))
)
{
// Adjust coordinates slightly
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;
}
// Draw three snowflake points
Setpixel (hscreendc, X, Y, 0 xffffff );
Setpixel (hscreendc, x + 1, Y + 1, 0 xffffff );
Setpixel (hscreendc, x-1, Y + xffffff );
// Regenerate the snow point
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
{
// Save the color and draw the snow point
Snownodes. Icolor = getpixel (hscreendc, x, y );
Setpixel (hscreendc, X, Y, 0 xffffff );
// Save the position of the new snow point
Snownodes. Postion. x = X;
Snownodes. Postion. Y = y;
}
}
}
Deletedc (hscreendc );
Crstep ++;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.