A snowy applet C ++ vc6.0

Source: Internet
Author: User
A small Win32 program. Use vc6.0.
  1. # Include "stdafx. H"
  2. # Include <windows. h>
  3. # Include <math. h>
  4. # Include <stdlib. h>
  5. # Define id_timer 1
  6. # Define snownum 500 // snowflake count
  7. # Define contrast 50 // contrast
  8. # Define ystart 5 // determines the Y coordinate at the beginning of the snowflake.
  9. # Define snowcr RGB (0xff, 0xff, 0xff) // snowflake color-white
  10. # Define snowgathercr RGB (0xdb, 0xdb, 0xff) // stacked snowflake color
  11. Typedef struct tagsnow
  12. {
  13. Point ptsnowspos [snownum]; // used to save the coordinates of each snowflake
  14. Colorref crorg [snownum]; // used to draw the original color of the screen before snowflake
  15. Int ivx, Ivy, iallvx, iallvy;
  16. } Snow;
  17. Void initsnow (HDC, snow * sn, int isnow, int cxscreen );
  18. Int getcontrast (HDC, snow * sn, int isnow );
  19. Void drawsnow (HDC, snow * sn, int cxscreen );
  20. Lresult callback wndproc (hwnd, uint, wparam, lparam );
  21. Int winapi winmain (hinstance, hinstance hprevinstance,
  22. Pstr szcmdline, int icmdshow)
  23. {
  24. Static tchar szappname [] = text ("cls1_snow ");
  25. Hwnd;
  26. MSG;
  27. Wndclass;
  28. Wndclass. Style = cs_hredraw | cs_vredraw;
  29. Wndclass. lpfnwndproc = wndproc;
  30. Wndclass. cbclsextra = 0;
  31. Wndclass. cbwndextra = 0;
  32. Wndclass. hinstance = hinstance;
  33. Wndclass. hicon = loadicon (null, idi_application );
  34. Wndclass. hcursor = loadcursor (null, idc_arrow );
  35. Wndclass. hbrbackground = (hbrush) getstockobject (white_brush );
  36. Wndclass. lpszmenuname = NULL;
  37. Wndclass. lpszclassname = szappname;
  38. If (! Registerclass (& wndclass ))
  39. {
  40. MessageBox (null, text ("this program requires Windows NT! "), Szappname, mb_iconerror );
  41. Return 0;
  42. }
  43. Hwnd = createwindow (szappname, text ("desktop snow "),
  44. Ws_minimizebox | ws_sysmenu,
  45. Cw_usedefault, cw_usedefault,
  46. 240,120,
  47. Null, null, hinstance, null );
  48. Showwindow (hwnd, icmdshow );
  49. Updatewindow (hwnd );
  50. While (getmessage (& MSG, null, 0, 0 ))
  51. {
  52. Translatemessage (& MSG );
  53. Dispatchmessage (& MSG );
  54. }
  55. Return msg. wparam;
  56. }
  57. Lresult callback wndproc (hwnd, uint message, wparam, lparam)
  58. {
  59. HDC;
  60. Paintstruct pS;
  61. Rect;
  62. Static int cxscreen, cyscreen; // screen width and height (unit: pixels)
  63. Static int itimes, ilooptimes = 100;
  64. Static snow snowdream;
  65. Int I;
  66. Switch (Message)
  67. {
  68. Case wm_create:
  69. Cxscreen = getsystemmetrics (sm_cxscreen );
  70. Cyscreen = getsystemmetrics (sm_cyscreen );
  71. Srand (INT) getcurrenttime (); // initialize the random number generator
  72. Snowdream. iallvx = (unsigned) rand () % 3-1; // snowflake overall horizontal floating speed (-, 1)
  73. Snowdream. iallvy = (unsigned) rand () % 2 + 1; // The overall vertical falling speed of snowflake (1, 2)
  74. HDC = getdc (null); // retrieves the context of the entire screen.
  75. For (I = 0; I <snownum; I ++)
  76. {
  77. Snowdream. ptsnowspos [I]. x = rand () % cxscreen; // X coordinate of a snowflake that begins to fall
  78. Snowdream. ptsnowspos [I]. Y = rand () % ystart; // y coordinate of a snowflake that begins to fall
  79. Snowdream. crorg [I] = getpixel (HDC, snowdream. ptsnowspos [I]. X,
  80. Snowdream. ptsnowspos [I]. Y); // obtain the original color value of the specified point.
  81. }
  82. Releasedc (null, HDC );
  83. Settimer (hwnd, id_timer, 10, null); // timer, 10 ms
  84. Return 0;
  85. Case wm_displaychange: // when the resolution is changed
  86. Cxscreen = getsystemmetrics (sm_cxscreen );
  87. Cyscreen = getsystemmetrics (sm_cyscreen );
  88. Return 0;
  89. Case wm_timer:
  90. HDC = getdc (null); // retrieves the context of the entire screen.
  91. If (itimes> ilooptimes)
  92. {
  93. Itimes = 0;
  94. Ilooptimes = 50 + (unsigned) rand () % 50;
  95. If (snowdream. iallvx! = 0)
  96. Snowdream. iallvx = 0;
  97. Else
  98. Snowdream. iallvx = (unsigned) rand () % 3-1; // snowflake overall horizontal floating speed (-, 1)
  99. Snowdream. iallvy = (unsigned) rand () % 2 + 1; // The overall vertical falling speed of snowflake (1, 2)
  100. }
  101. Else
  102. Itimes ++;
  103. Drawsnow (HDC, & snowdream, cxscreen );
  104. Releasedc (null, HDC );
  105. Return 0;
  106. Case wm_paint:
  107. HDC = beginpaint (hwnd, & PS );
  108. Getclientrect (hwnd, & rect );
  109. Drawtext (HDC, text ("it's snowing on the desktop! "),-1, & rect,
  110. Dt_singleline | dt_center | dt_vcenter );
  111. Endpaint (hwnd, & PS );
  112. Return 0;
  113. Case wm_destroy:
  114. Killtimer (hwnd, id_timer); // stop the timer
  115. Invalidaterect (null, null, true); // refresh the Desktop
  116. Postquitmessage (0 );
  117. Return 0;
  118. }
  119. Return defwindowproc (hwnd, message, wparam, lparam );
  120. }
  121. Void initsnow (HDC, snow * sn, int isnow, int cxscreen) // initialize the isnow snowflake
  122. {
  123. Sn-> ptsnowspos [isnow]. x = (unsigned) rand () % cxscreen; // The Screen width in the X range
  124. Sn-> ptsnowspos [isnow]. Y = (unsigned) rand () % ystart; // The value of Y is within the ystart pixel on the top of the screen.
  125. Sn-> crorg [isnow] = getpixel (HDC, Sn-> ptsnowspos [isnow]. X,
  126. Sn-> ptsnowspos [isnow]. Y); // obtain the original color value for the specified point
  127. }
  128. Int getcontrast (HDC, snow * sn, int isnow)
  129. {
  130. Int IR, Ig, IB;
  131. Colorref crcmp;
  132. If (0 = Sn-> ivx) // If the horizontal speed is 0, the point at the bottom of the pixel is greater than the value.
  133. Crcmp = getpixel (HDC, Sn-> ptsnowspos [isnow]. X, Sn-> ptsnowspos [isnow]. Y + 1 );
  134. Else // If the horizontal speed is greater than 0, take the point in the lower right. <0 indicates that the point in the lower left is used.
  135. Crcmp = getpixel (HDC, Sn-> ptsnowspos [isnow]. x + (Sn-> ivx> 0? 1:-1), Sn-> ptsnowspos [isnow]. Y + 1 );
  136. If (crcmp = snowcr) // if it is a snowflake color
  137. Return 0;
  138. // Obtain the difference between the blue, green, and red parts of the crcmp and the comparison point.
  139. IB = ABS (crcmp> 16) & 0xff-(Sn-> crorg [isnow]> 16) & 0xff );
  140. IG = ABS (crcmp> 8) & 0xff-(Sn-> crorg [isnow]> 8) & 0xff );
  141. IR = ABS (crcmp) & 0xff-(Sn-> crorg [isnow]) & 0xff );
  142. Return (IR + ig + IB)/3;
  143. }
  144. Void drawsnow (HDC, snow * sn, int cxscreen)
  145. {
  146. Int I;
  147. For (I = 0; I <snownum; I ++)
  148. {
  149. // If the original color is not the snowflake color
  150. If (Sn-> crorg [I]! = Snowcr)
  151. Setpixel (HDC, Sn-> ptsnowspos [I]. X, Sn-> ptsnowspos [I]. y,
  152. Sn-> crorg [I]); // restores the color of the previous position
  153. Sn-> ivx = Sn-> iallvx * (I % 3 + 1); // X floating speed of snowflake
  154. Sn-> Ivy = Sn-> iallvy * (I % 3 + 1); // y floating speed of snowflake
  155. // Rand () % 5-2 jitter when the snowflake falls
  156. Sn-> ptsnowspos [I]. x + = Sn-> ivx + rand () % 5-2; // The next X coordinate of snowflake
  157. Sn-> ptsnowspos [I]. Y + = Sn-> Ivy + 1; // The next y coordinate of snowflake
  158. // Obtain the original color value for the specified point
  159. Sn-> crorg [I] = getpixel (HDC, Sn-> ptsnowspos [I]. X, Sn-> ptsnowspos [I]. y );
  160. If (clr_invalid = Sn-> crorg [I]) // if the color fails to be obtained, the snowflake floats out of the screen.
  161. {
  162. Initsnow (HDC, Sn, I, cxscreen); // reinitialize snowflake
  163. Continue;
  164. }
  165. If (Sn-> crorg [I]! = Snowcr) // if the color of the current vertex is not the color of snowflake
  166. {
  167. If (snowgathercr = Sn-> crorg [I]) // color of the current vertex = color of accumulated snow
  168. {// Set to the snowflake color
  169. Setpixel (HDC, Sn-> ptsnowspos [I]. X, Sn-> ptsnowspos [I]. Y, snowcr );
  170. Sn-> crorg [I] = snowcr;
  171. // Initsnow (HDC, Sn, I, cxscreen); // reinitialize snowflake
  172. }
  173. Else if (getcontrast (HDC, Sn, I)> 50) // If the contrast is greater than 50
  174. {// Snow accumulation
  175. Setpixel (HDC, Sn-> ptsnowspos [I]. X, Sn-> ptsnowspos [I]. Y, snowgathercr );
  176. Setpixel (HDC, Sn-> ptsnowspos [I]. X-1, Sn-> ptsnowspos [I]. Y + 1, snowgathercr );
  177. Setpixel (HDC, Sn-> ptsnowspos [I]. x + 1, Sn-> ptsnowspos [I]. Y + 1, snowgathercr );
  178. Initsnow (HDC, Sn, I, cxscreen); // reinitialize snowflake
  179. }
  180. Else // contrast <50, do not accumulate, draw the snowflake frame, and then restore the original color of the point next time to produce the floating Effect
  181. Setpixel (HDC, Sn-> ptsnowspos [I]. X, Sn-> ptsnowspos [I]. Y, snowcr );
  182. }
  183. }
  184. }

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.