name22

GDI+ New Year Countdown w/Fireworks

Dec 8th, 2012
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WindowsConstants.au3>
  2. #include <GUIConstants.au3>
  3. #include <GDIPlus.au3>
  4. #include <WinAPI.au3>
  5. #include <Date.au3>
  6.  
  7. ; -Authors: name22 (www.autoit.de), Andy (www.autoit.de), Facemix (www.autoit.de)
  8.  
  9. Opt("GUIOnEventMode", 1)
  10.  
  11. Global Const $nTau = ACos(-1) * 2
  12. Global $vNTdll = DllOpen("ntdll.dll")
  13. Global $tPrecSleep = DllStructCreate("int64 time;")
  14. Global $pPrecSleep = DllStructGetPtr($tPrecSleep)
  15.  
  16. ;###-v-SETTINGS-v-###
  17. Global $iWidth = 600
  18. Global $iHeight = 300
  19. Global $nFPS = 60
  20.  
  21. Global $nStepSpeed = 1
  22.  
  23. Global $iARGB_BG = 0xFF505050
  24. Global $iARGB_Snow = 0xFFFFFFFF
  25. Global $iARGB_TextFill = 0xFF0000FF
  26. Global $iARGB_TextFill2 = 0xFFFF0000
  27. Global $iARGB_TextFill3 = 0xFF0000FF
  28. Global $iARGB_TextFill4 = 0xFF353535
  29. Global $iARGB_TextBorder = 0xFFA00000
  30.  
  31. Global $iFlakeCount = 60
  32. Global $iMinFallingSpeed = 20, $iMaxFallingSpeed = 50
  33. Global $iMinOscillations = 2, $iMaxOscillations = 5
  34. Global $iMinOscillationDist = 10, $iMaxOscillationDist = 50
  35. Global $iMinRadius = 1, $iMaxRadius = 3
  36.  
  37. Global $iBlastRadius = 200
  38. Global $iBlastAccell = 300
  39. Global $nAirFriction = 0.95
  40. ;###-^-SETTINGS-^-###
  41.  
  42. Global $aFlakes[$iFlakeCount + 1][8] = [[$iFlakeCount]]
  43. Global $hWnd, $hDC_Window, $hDC_Bitmap, $hBitmap, $hGraphics, $hOldObj, $hBrush_Snow, $hBrush_TextFill, $hPen_Border
  44. Global $hMatrix, $tLayout, $hFormat, $hFamily, $hPath, $nScale, $nStep = 0, $iH, $iM, $iS, $iD, $sText, $sTextOld
  45. Global $nSleepTime = 1000 / $nFPS
  46.  
  47. For $i = 1 To $aFlakes[0][0]
  48.     $aFlakes[$i][0] = Random(10, $iWidth - 10, 1)
  49.     $aFlakes[$i][1] = Random(-$iHeight, 0, 1)
  50.     $aFlakes[$i][2] = 0
  51.     $aFlakes[$i][3] = 0
  52.     $aFlakes[$i][4] = Random($iMinRadius, $iMaxRadius, 1)
  53.     $aFlakes[$i][5] = Random($iMinFallingSpeed, $iMaxFallingSpeed)
  54.     $aFlakes[$i][6] = Random($iMinOscillations, $iMaxOscillations, 1)
  55.     $aFlakes[$i][7] = Random($iMinOscillationDist, $iMaxOscillationDist) * (-1) ^ Random(1, 2, 1)
  56. Next
  57.  
  58. $hWnd = GUICreate("name22 - Snowflakes", $iWidth, $iHeight)
  59. GUISetState()
  60.  
  61. $hDC_Window = _WinAPI_GetDC($hWnd)
  62. $hDC_Bitmap = _WinAPI_CreateCompatibleDC($hDC_Window)
  63. $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC_Window, $iWidth, $iHeight)
  64. $hOldObj = _WinAPI_SelectObject($hDC_Bitmap, $hBitmap)
  65.  
  66. _GDIPlus_Startup()
  67.  
  68. $hGraphics = _GDIPlus_GraphicsCreateFromHDC($hDC_Bitmap)
  69. _GDIPlus_GraphicsSetClipRect($hGraphics, 0, 0, $iWidth, $iHeight)
  70. _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2)
  71.  
  72. $hBrush_Snow = _GDIPlus_BrushCreateSolid($iARGB_Snow)
  73. $hBrush_TextFill = _GDIPlus_BrushCreateSolid($iARGB_TextFill)
  74. $hPen_Border = _GDIPlus_PenCreate($iARGB_TextBorder, 3)
  75. DllCall($ghGDIPDll, "uint", "GdipSetPenLineJoin", "hwnd", $hPen_Border, "int", 2)
  76.  
  77. $hMatrix = _GDIPlus_MatrixCreate()
  78. $tLayout = _GDIPlus_RectFCreate(0, 0, 0, 0)
  79. $hFormat = _GDIPlus_StringFormatCreate()
  80. $hFamily = _GDIPlus_FontFamilyCreate("Arial")
  81. $hPath = _CreatePath("88 - 88:88:88")
  82.  
  83. _GDIPlus_GraphicsClear($hGraphics, $iARGB_BG)
  84. _Redraw()
  85.  
  86. OnAutoItExitRegister("_Shutdown")
  87. GUIRegisterMsg($WM_PAINT, "_Redraw")
  88. GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
  89. GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "_Click")
  90.  
  91. AdlibRegister("_UpdateTime", 100)
  92.  
  93. $nT_Sleep = TimerInit() + $nSleepTime
  94.  
  95. While True
  96.     DllStructSetData($tPrecSleep, "time", -10000 * ($nSleepTime - TimerDiff($nT_Sleep)))
  97.     DllCall($vNTdll, "dword", "ZwDelayExecution", "int", 0, "ptr", $pPrecSleep)
  98.     $nFrameTime = TimerDiff($nT_Sleep)
  99.     $nT_Sleep = TimerInit()
  100.  
  101.     $nFPS_Cur = 1000 / $nFrameTime
  102.  
  103.     $nStep += $nStepSpeed / $nFPS_Cur
  104.     $nScale = Abs(Sin($nStep))
  105.  
  106.     _GDIPlus_GraphicsClear($hGraphics, $iARGB_BG)
  107.  
  108.     DllCall($ghGDIPDll, "uint", "GdipTranslateWorldTransform", "hwnd", $hGraphics, "float", -$iWidth / 2, "float", -$iHeight / 2, "int", False)
  109.     DllCall($ghGDIPDll, "uint", "GdipScaleWorldTransform", "hwnd", $hGraphics, "float", $nScale, "float", $nScale, "int", True)
  110.     DllCall($ghGDIPDll, "uint", "GdipTranslateWorldTransform", "hwnd", $hGraphics, "float", $iWidth / 2, "float", $iHeight / 2, "int", True)
  111.  
  112.     DllCall($ghGDIPDll, "uint", "GdipDrawPath", "hwnd", $hGraphics, "hwnd", $hPen_Border, "hwnd", $hPath)
  113.     DllCall($ghGDIPDll, "uint", "GdipFillPath", "hwnd", $hGraphics, "hwnd", $hBrush_TextFill, "hwnd", $hPath)
  114.  
  115.     DllCall($ghGDIPDll, "uint", "GdipResetWorldTransform", "hwnd", $hGraphics)
  116.  
  117.     For $i = 1 To $aFlakes[0][0]
  118.         $aFlakes[$i][1] += ($aFlakes[$i][5] + $aFlakes[$i][3]) / $nFPS_Cur
  119.         $aFlakes[$i][0] += (Sin($aFlakes[$i][1] / $iHeight * $nTau * $aFlakes[$i][6]) * $aFlakes[$i][7] + $aFlakes[$i][2]) / $nFPS_Cur
  120.         $aFlakes[$i][2] *= $nAirFriction
  121.         $aFlakes[$i][3] *= $nAirFriction
  122.         If $aFlakes[$i][1] > $iHeight Or $aFlakes[$i][0] < -20 Or $aFlakes[$i][0] > $iWidth + 20 Then
  123.             $aFlakes[$i][0] = Random(10, $iWidth - 10, 1)
  124.             $aFlakes[$i][1] = Random(-$iHeight, 0, 1)
  125.             $aFlakes[$i][2] = 0
  126.             $aFlakes[$i][3] = 0
  127.             $aFlakes[$i][4] = Random($iMinRadius, $iMaxRadius, 1)
  128.             $aFlakes[$i][5] = Random($iMinFallingSpeed, $iMaxFallingSpeed)
  129.             $aFlakes[$i][6] = Random($iMinOscillations, $iMaxOscillations, 1)
  130.             $aFlakes[$i][7] = Random($iMinOscillationDist, $iMaxOscillationDist)
  131.         EndIf
  132.         _GDIPlus_GraphicsFillEllipse($hGraphics, $aFlakes[$i][0] - $aFlakes[$i][4] / 2, $aFlakes[$i][1] - $aFlakes[$i][4] / 2, $aFlakes[$i][4] * 2, $aFlakes[$i][4] * 2, $hBrush_Snow)
  133.     Next
  134.     _WinAPI_BitBlt($hDC_Window, 0, 0, $iWidth, $iHeight, $hDC_Bitmap, 0, 0, $SRCCOPY)
  135. WEnd
  136.  
  137. Func _UpdateTime()
  138.     _TicksToTime(_DateDiff('s', _NowCalc(), "2013/01/01 00:00:00") * 1000, $iH, $iM, $iS)
  139.     If $iH > 23 Then
  140.         $iD = StringFormat("%02i", $iH / 24); volle Tage
  141.         $iH = $iH - ($iD * 24); volle Tage abziehen
  142.     Else
  143.         $iD = 0
  144.     EndIf
  145.  
  146.     If $sText = "00 - 00:00:00" Then
  147.         AdlibUnRegister("_UpdateTime")
  148.         Feuerwerk() ; Funktion Feuerwerk soll dann ausgeführt werden
  149.     Else
  150.         $sText = StringFormat("%02i - %02i:%02i:%02i", $iD, $iH, $iM, $iS)
  151.     EndIf
  152.  
  153.     If $sText <> $sTextOld Then
  154.         $sTextOld = $sText
  155.         DllCall($ghGDIPDll, "uint", "GdipResetPath", "hwnd", $hPath)
  156.         DllCall($ghGDIPDll, "uint", "GdipAddPathString", "hwnd", $hPath, "wstr", $sText, "int", -1, "hwnd", $hFamily, "int", 1, "float", 100, "ptr", DllStructGetPtr($tLayout), "hwnd", $hFormat)
  157.         DllCall($ghGDIPDll, "uint", "GdipTransformPath", "hwnd", $hPath, "hwnd", $hMatrix)
  158.     EndIf
  159. EndFunc
  160.  
  161. Func _Click()
  162.     Local $tMousePos = _WinAPI_GetMousePos(True, $hWnd)
  163.     For $i = 1 To $aFlakes[0][0]
  164.         $iX_Dist = ($aFlakes[$i][0] - $aFlakes[$i][4]) - DllStructGetData($tMousePos, "X")
  165.         $iY_Dist = ($aFlakes[$i][1] - $aFlakes[$i][4]) - DllStructGetData($tMousePos, "Y")
  166.         $iDist = Sqrt($iX_Dist ^ 2 + $iY_Dist ^ 2)
  167.         If $iDist = 0 Then ContinueLoop
  168.         If $iDist < $iBlastRadius Then
  169.             $aFlakes[$i][2] = (1 - $iDist / $iBlastRadius) * ($iX_Dist / $iDist) * $iBlastAccell
  170.             $aFlakes[$i][3] = (1 - $iDist / $iBlastRadius) * ($iY_Dist / $iDist) * $iBlastAccell
  171.         EndIf
  172.     Next
  173. EndFunc   ;==>_Click
  174.  
  175. Func _Redraw()
  176.     _WinAPI_BitBlt($hDC_Window, 0, 0, $iWidth, $iHeight, $hDC_Bitmap, 0, 0, $SRCCOPY)
  177. EndFunc   ;==>_Redraw
  178.  
  179. Func _Close()
  180.     Exit
  181. EndFunc   ;==>_Close
  182.  
  183. Func _Shutdown()
  184.     _WinAPI_SelectObject($hDC_Bitmap, $hOldObj)
  185.     _WinAPI_ReleaseDC($hWnd, $hDC_Window)
  186.     _WinAPI_DeleteDC($hDC_Bitmap)
  187.     _WinAPI_DeleteObject($hBitmap)
  188.  
  189.     _GDIPlus_GraphicsDispose($hGraphics)
  190.     _GDIPlus_BrushDispose($hBrush_Snow)
  191.     _GDIPlus_PenDispose($hPen_Border)
  192.     _GDIPlus_MatrixDispose($hMatrix)
  193.     _GDIPlus_StringFormatDispose($hFormat)
  194.     _GDIPlus_FontFamilyDispose($hFamily)
  195.     DllCall($ghGDIPDll, "uint", "GdipDeletePath", "hwnd", $hPath)
  196.     _GDIPlus_Shutdown()
  197.  
  198.     DllClose($vNTdll)
  199. EndFunc   ;==>_Shutdown
  200.  
  201. Func _CreatePath($sText)
  202.  
  203.     Local $tBounds = _GDIPlus_RectFCreate(0, 0, 0, 0)
  204.  
  205.     Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreatePath", "int", 0, "int*", 0)
  206.     If @error Or Not IsArray($aResult) Then Return SetError(1, 1, False)
  207.     Local $hPath = $aResult[2]
  208.  
  209.     DllCall($ghGDIPDll, "uint", "GdipAddPathString", "hwnd", $hPath, "wstr", $sText, "int", -1, "hwnd", $hFamily, "int", 1, "float", 100, "ptr", DllStructGetPtr($tLayout), "hwnd", $hFormat)
  210.     DllCall($ghGDIPDll, "uint", "GdipGetPathWorldBounds", "hwnd", $hPath, "ptr", DllStructGetPtr($tBounds), "hwnd", 0, "hwnd", 0)
  211.  
  212.  
  213.     _GDIPlus_MatrixTranslate($hMatrix, -DllStructGetData($tBounds, "X"), -DllStructGetData($tBounds, "Y"))
  214.     _GDIPlus_MatrixScale($hMatrix, $iWidth / DllStructGetData($tBounds, "Width"), $iHeight / DllStructGetData($tBounds, "Height"), True)
  215.     DllCall($ghGDIPDll, "uint", "GdipTransformPath", "hwnd", $hPath, "hwnd", $hMatrix)
  216.  
  217.     Return $hPath
  218. EndFunc   ;==>_CreatePath
  219.  
  220. Func Feuerwerk()
  221.     Local $aDraw
  222.     Local $iCount = 10, $iExplosion = 100, $iExplosionGround = 1, $iRadius = 600, $iBack = 0x09000000, $iSleep = 5
  223.     Global $iWidth2 = @DesktopWidth, $iHeight2 = @DesktopHeight, $aStart[$iCount][4] = [[Random(0, $iWidth2, 0), Random(0, $iHeight2, 0), 0, _GDIPlus_PenCreate(Random(0xFF000000, 0xFFFFFFFF, 0))],[Random(0, $iWidth2, 0), Random(0, $iHeight2, 0), 0, _GDIPlus_PenCreate(Random(0xFF000000, 0xFFFFFFFF, 0))]], $iSleeping = 0
  224.     For $i = 0 To UBound($aStart) - 1
  225.         $aStart[$i][0] = Random(0, $iWidth2, 0)
  226.         $aStart[$i][1] = Random(0, $iHeight2, 0)
  227.         $aStart[$i][2] = $iExplosion
  228.         $aStart[$i][3] = _GDIPlus_PenCreate(Random(0xFF000000, 0xFFFFFFFF, 0))
  229.     Next
  230.  
  231.  
  232.     While True
  233.         _GDIPlus_GraphicsClear($hGraphics, $iBack)
  234.         If $iSleeping = 0 Then
  235.             For $j = 0 To UBound($aStart) - 1
  236.                 For $i = 0 To 35
  237.                     $aDraw = Dreieck($i * 10, $aStart[$j][2] - $iExplosion)
  238.                     _GDIPlus_GraphicsDrawRect($hGraphics, $aStart[$j][0] + Round($aDraw[0], 0), $aStart[$j][1] + Round($aDraw[1], 0), 2, 2, $aStart[$j][3])
  239.                 Next
  240.                 $aStart[$j][2] += $iExplosionGround + Ceiling(10 * $iExplosion / ($aStart[$j][2]))
  241.                 If $aStart[$j][2] > $iRadius Then
  242.                     $iSleeping = $iSleep
  243.                     $aStart[$j][0] = Random(0, $iWidth2, 0)
  244.                     $aStart[$j][1] = Random(0, $iHeight2, 0)
  245.                     $aStart[$j][2] = $iExplosion
  246.                     _GDIPlus_PenDispose($aStart[$j][3])
  247.                     $aStart[$j][3] = _GDIPlus_PenCreate(Random(0xFF000000, 0xFFFFFFFF, 0))
  248.                 EndIf
  249.             Next
  250.         Else
  251.             $iSleeping -= 1
  252.         EndIf
  253.         _WinAPI_RedrawWindow($hWnd, 0, 0, 2)
  254.     WEnd
  255. EndFunc   ;==>Feuerwerk
  256.  
  257. Func Dreieck($iWinkel, $iLaenge)
  258.     Local $iDeg = 0.0174532925199433, $aRet[2]
  259.     $aRet[0] = Sin($iWinkel * $iDeg) * $iLaenge
  260.     $aRet[1] = Cos($iWinkel * $iDeg) * $iLaenge
  261.     Return $aRet
  262. EndFunc   ;==>Dreieck
  263.  
  264.  
  265. ; #FUNCTION# ====================================================================================================================
  266. ; Name...........: _GDIPlus_GraphicsSetClipRect
  267. ; Description ...: Updates the clipping region of a Graphics object to a region that is the combination of itself and a rectangle
  268. ; Syntax.........: _GDIPlus_GraphicsSetClipRect($hGraphics, $nX, $nY, $nWidth, $nHeight[, $iCombineMode = 0])
  269. ; Parameters ....: $hGraphics - Pointer to a Graphics object
  270. ;                  $nX        - X coordinate of the upper-left corner of the rectangle
  271. ;                  $nY        - Y coordinate of the upper-left corner of the rectangle
  272. ;                  $nWidth    - Width of the rectangle
  273. ;                  $nHeight   - Height of the rectangle
  274. ;                  $iCombineMode - Regions combination mode:
  275. ;                  |0 - The existing region is replaced by the new region
  276. ;                  |1 - The existing region is replaced by the intersection of itself and the new region
  277. ;                  |2 - The existing region is replaced by the union of itself and the new region
  278. ;                  |3 - The existing region is replaced by the result of performing an XOR on the two regions
  279. ;                  |4 - The existing region is replaced by the portion of itself that is outside of the new region
  280. ;                  |5 - The existing region is replaced by the portion of the new region that is outside of the existing region
  281. ; Return values .: Success      - True
  282. ;                  Failure      - False and either:
  283. ;                  |@error and @extended are set if DllCall failed
  284. ;                  |$GDIP_STATUS contains a non zero value specifying the error code
  285. ; Remarks .......: None
  286. ; Related .......: None
  287. ; Link ..........; @@MsdnLink@@ GdipSetClipRect
  288. ; Example .......; No
  289. ; ===============================================================================================================================
  290. Func _GDIPlus_GraphicsSetClipRect($hGraphics, $nX, $nY, $nWidth, $nHeight, $iCombineMode = 0)
  291.     Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetClipRect", "hwnd", $hGraphics, "float", $nX, "float", $nY, "float", $nWidth, "float", $nHeight, "int", $iCombineMode)
  292.  
  293.     If @error Then Return SetError(@error, @extended, False)
  294.     $GDIP_STATUS = $aResult[0]
  295.     Return $aResult[0] = 0
  296. EndFunc   ;==>_GDIPlus_GraphicsSetClipRect
RAW Paste Data