GDI+ Countdown

  • mit der For Schleife ist es sehr leicht ja:

    [autoit]

    For $i = 0 To 20
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0)
    _GDIPlus_GraphicsClear($hBuffer, 0xFFA2FF00)
    _GDIPlus_GraphicsDrawString($hBuffer, StringFormat("%02i - %02i:%02i:%02i", $days, $hour, $Mins, $secs), $xAchse, $yAchse, "Arial", $i)
    Sleep(50)
    Next

    [/autoit]

    Ist aber nicht das was ich will, wenn mir jemand helfen kann, ich währe dann sehr sehr dankbar

    Edit: Ich habe es jetzt so geschafft den Countdown zu machen, an sich sieht das schon super aus nur der einzige Fehler ist, während dem Zoomeffekt, Geht der Countdown nicht runter, das heist er bleibt stehen, das soll nicht sein hat jemand dazu Hilfestellung?

    Edit: So Ich habe es geschafft mein Problem nun selber zu lösen :) Habe mir den Code nochmal genau angeschaut und habe den Fehler gefunden :) Ich danke euch trotzdem für eure Hilfe :)

    Habe euch den Code aktualisiert, Funktioniert genau so wie ich es mir vorstelle

    Hier der Code:

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    #include <Date.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Opt('MustDeclareVars', 1)
    Opt('GUIOnEventMode', 1)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()

    [/autoit] [autoit][/autoit] [autoit]

    Global $iWidth = 400
    Global $iHeight = 200

    [/autoit] [autoit][/autoit] [autoit]

    Global $days, $hour, $hour2, $Mins, $secs

    [/autoit] [autoit][/autoit] [autoit]

    Global $iColor_BG = 0xFF008000
    Global $iColor_TextFill = 0xFFFFAA00
    Global $iColor_TextFill2 = 0xFFFF0000
    Global $iColor_TextFill3 = 0xFF0000FF
    Global $iColor_TextFill4 = 0xFF353535
    Global $iColor_TextBorder = 0xFFA00000

    [/autoit] [autoit][/autoit] [autoit]

    Global $hGui = GUICreate("GDI+", $iWidth, $iHeight)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGui)
    Global $hBmpBuffer = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $hGraphics)
    Global $hGfxBuffer = _GDIPlus_ImageGetGraphicsContext($hBmpBuffer)
    _GDIPlus_GraphicsSetSmoothingMode($hGfxBuffer, 2)
    _GDIPlus_GraphicsClear($hGfxBuffer, $iColor_BG)

    [/autoit] [autoit][/autoit] [autoit]

    Global $hPen = _GDIPlus_PenCreate($iColor_TextBorder, 3)
    Global $hBrush = _GDIPlus_BrushCreateSolid($iColor_TextFill)
    Global $day, $hour, $hour2, $Mins, $secs

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    GUIRegisterMsg($WM_PAINT, "WM_PAINT")
    GUIRegisterMsg($WM_ERASEBKGND, "WM_ERASEBKGND")

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    AdlibRegister("_Update", 1000)

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    Sleep(10)
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _Update()
    Global $fStep = 0

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    _GDIPlus_GraphicsClear($hGfxBuffer, 0xFFA2FF00)
    _TicksToTime(_DateDiff('s', _NowCalc(), "2013/01/01 00:00:00") * 1000, $hour, $Mins, $secs)
    ;GUICtrlSetData($Label1, StringFormat("%02i:%02i:%02i", $hour, $Mins, $secs))
    If $hour >23 Then
    $days = StringFormat("%02i", $hour / 24); volle Tage
    $hour = $hour - ($days*24); volle Tage abziehen
    Else
    $days = 0
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Global $hPath = _CreatePath(StringFormat("%02i - %02i:%02i:%02i", $days, $hour, $Mins, $secs))

    [/autoit] [autoit][/autoit] [autoit]

    $fStep += 0.025
    _Draw(Abs(Sin($fStep)))
    Sleep(50)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _Draw($fScale)
    _GDIPlus_GraphicsClear($hGfxBuffer, $iColor_BG)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    DllCall($ghGDIPDll, "uint", "GdipTranslateWorldTransform", "hwnd", $hGfxBuffer, "float", -$iWidth / 2, "float", -$iHeight / 2, "int", False)
    DllCall($ghGDIPDll, "uint", "GdipScaleWorldTransform", "hwnd", $hGfxBuffer, "float", $fScale, "float", $fScale, "int", True)
    DllCall($ghGDIPDll, "uint", "GdipTranslateWorldTransform", "hwnd", $hGfxBuffer, "float", $iWidth / 2, "float", $iHeight / 2, "int", True)

    [/autoit] [autoit][/autoit] [autoit]

    DllCall($ghGDIPDll, "uint", "GdipDrawPath", "hwnd", $hGfxBuffer, "hwnd", $hPen, "hwnd", $hPath)
    DllCall($ghGDIPDll, "uint", "GdipFillPath", "hwnd", $hGfxBuffer, "hwnd", $hBrush, "hwnd", $hPath)

    [/autoit] [autoit][/autoit] [autoit]

    DllCall($ghGDIPDll, "uint", "GdipResetWorldTransform", "hwnd", $hGfxBuffer)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_GraphicsDrawImage($hGraphics, $hBmpBuffer, 0, 0)
    EndFunc ;==>_Draw

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _CreatePath($sText)
    Local $hFormat = _GDIPlus_StringFormatCreate()
    Local $hFamily = _GDIPlus_FontFamilyCreate("Arial Black")

    [/autoit] [autoit][/autoit] [autoit]

    Local $tLayout = _GDIPlus_RectFCreate(0, 0, 0, 0)
    Local $tBounds = _GDIPlus_RectFCreate(0, 0, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreatePath", "int", 0, "int*", 0)
    If @error Or Not IsArray($aResult) Then Return SetError(1, 1, False)
    Local $hPath = $aResult[2]

    [/autoit] [autoit][/autoit] [autoit]

    DllCall($ghGDIPDll, "uint", "GdipAddPathString", "hwnd", $hPath, "wstr", $sText, "int", -1, "hwnd", $hFamily, "int", 1, "float", 100, "ptr", DllStructGetPtr($tLayout), "hwnd", $hFormat)
    DllCall($ghGDIPDll, "uint", "GdipGetPathWorldBounds", "hwnd", $hPath, "ptr", DllStructGetPtr($tBounds), "hwnd", 0, "hwnd", 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $hMatrix = _GDIPlus_MatrixCreate()
    _GDIPlus_MatrixTranslate($hMatrix, -DllStructGetData($tBounds, "X"), -DllStructGetData($tBounds, "Y"))
    _GDIPlus_MatrixScale($hMatrix, $iWidth / DllStructGetData($tBounds, "Width"), $iHeight / DllStructGetData($tBounds, "Height"), True)
    DllCall($ghGDIPDll, "uint", "GdipTransformPath", "hwnd", $hPath, "hwnd", $hMatrix)
    _GDIPlus_MatrixDispose($hMatrix)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)

    [/autoit] [autoit][/autoit] [autoit]

    Return $hPath
    EndFunc ;==>_CreatePath

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func WM_PAINT($hWnd, $uMsgm, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBmpBuffer, 0, 0)
    Return $GUI_RUNDEFMSG
    EndFunc ;==>WM_PAINT

    [/autoit] [autoit][/autoit] [autoit]

    Func WM_ERASEBKGND($hWnd, $uMsgm, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBmpBuffer, 0, 0)
    Return True
    EndFunc ;==>WM_ERASEBKGND

    [/autoit] [autoit][/autoit] [autoit]

    Func _Exit()
    DllCall($ghGDIPDll, "uint", "GdipDeletePath", "hwnd", $hPath)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGfxBuffer)
    _GDIPlus_BitmapDispose($hBmpBuffer)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    Exit
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit]
  • Ich hab dein Script etwas verändert:

    Spoiler anzeigen
    [autoit]

    #include <GDIPlus.au3>
    #include <Date.au3>
    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Opt('MustDeclareVars', 1)
    Opt('GUIOnEventMode', 1)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_Startup()

    [/autoit] [autoit][/autoit] [autoit]

    Global $iWidth = 500
    Global $iHeight = 140

    [/autoit] [autoit][/autoit] [autoit]

    Global $days, $hour, $hour2, $Mins, $secs

    [/autoit] [autoit][/autoit] [autoit]

    Global $iColor_BG = 0xFF008000
    Global $iColor_TextFill = 0xFFFFAA00
    Global $iColor_TextFill2 = 0xFFFF0000
    Global $iColor_TextFill3 = 0xFF0000FF
    Global $iColor_TextFill4 = 0xFF353535
    Global $iColor_TextBorder = 0xFFA00000

    [/autoit] [autoit][/autoit] [autoit]

    Global $hGui = GUICreate("GDI+", $iWidth, $iHeight)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGui)
    Global $hBmpBuffer = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $hGraphics)
    Global $hGfxBuffer = _GDIPlus_ImageGetGraphicsContext($hBmpBuffer)
    _GDIPlus_GraphicsSetSmoothingMode($hGfxBuffer, 2)
    _GDIPlus_GraphicsClear($hGfxBuffer, $iColor_BG)

    [/autoit] [autoit][/autoit] [autoit]

    Global $hPen = _GDIPlus_PenCreate($iColor_TextBorder, 3)
    DllCall($ghGDIPDll, "uint", "GdipSetPenLineJoin", "hwnd", $hPen, "int", 2)
    Global $hBrush = _GDIPlus_BrushCreateSolid($iColor_TextFill)
    Global $day, $hour, $hour2, $Mins, $secs

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    GUIRegisterMsg($WM_PAINT, "WM_PAINT")
    GUIRegisterMsg($WM_ERASEBKGND, "WM_ERASEBKGND")

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    Global $hMatrix = _GDIPlus_MatrixCreate()
    Global $tLayout = _GDIPlus_RectFCreate(0, 0, 0, 0)
    Global $hFormat = _GDIPlus_StringFormatCreate()
    Global $hFamily = _GDIPlus_FontFamilyCreate("Arial")
    Global $hPath = _CreatePath("88 - 88:88:88")

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Global $sText, $sTextOld
    Global $fStep = 0

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    _TicksToTime(_DateDiff('s', _NowCalc(), "2013/01/01 00:00:00") * 1000, $hour, $Mins, $secs)
    If $hour >23 Then
    $days = StringFormat("%02i", $hour / 24); volle Tage
    $hour = $hour - ($days*24); volle Tage abziehen
    Else
    $days = 0
    EndIf
    $sText = StringFormat("%02i - %02i:%02i:%02i", $days, $hour, $Mins, $secs)

    [/autoit] [autoit][/autoit] [autoit]

    If $sText <> $sTextOld Then
    $sTextOld = $sText
    DllCall($ghGDIPDll, "uint", "GdipResetPath", "hwnd", $hPath)
    DllCall($ghGDIPDll, "uint", "GdipAddPathString", "hwnd", $hPath, "wstr", $sText, "int", -1, "hwnd", $hFamily, "int", 1, "float", 100, "ptr", DllStructGetPtr($tLayout), "hwnd", $hFormat)
    DllCall($ghGDIPDll, "uint", "GdipTransformPath", "hwnd", $hPath, "hwnd", $hMatrix)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    $fStep += 0.01
    _Draw(Abs(Sin($fStep)))
    Sleep(10)
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _Draw($fScale)
    _GDIPlus_GraphicsClear($hGfxBuffer, $iColor_BG)

    [/autoit] [autoit][/autoit] [autoit]

    DllCall($ghGDIPDll, "uint", "GdipTranslateWorldTransform", "hwnd", $hGfxBuffer, "float", -$iWidth / 2, "float", -$iHeight / 2, "int", False)
    DllCall($ghGDIPDll, "uint", "GdipScaleWorldTransform", "hwnd", $hGfxBuffer, "float", $fScale, "float", $fScale, "int", True)
    DllCall($ghGDIPDll, "uint", "GdipTranslateWorldTransform", "hwnd", $hGfxBuffer, "float", $iWidth / 2, "float", $iHeight / 2, "int", True)

    [/autoit] [autoit][/autoit] [autoit]

    DllCall($ghGDIPDll, "uint", "GdipDrawPath", "hwnd", $hGfxBuffer, "hwnd", $hPen, "hwnd", $hPath)
    DllCall($ghGDIPDll, "uint", "GdipFillPath", "hwnd", $hGfxBuffer, "hwnd", $hBrush, "hwnd", $hPath)

    [/autoit] [autoit][/autoit] [autoit]

    DllCall($ghGDIPDll, "uint", "GdipResetWorldTransform", "hwnd", $hGfxBuffer)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_GraphicsDrawImage($hGraphics, $hBmpBuffer, 0, 0)
    EndFunc ;==>_Draw

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _CreatePath($sText)

    [/autoit] [autoit][/autoit] [autoit]

    Local $tBounds = _GDIPlus_RectFCreate(0, 0, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreatePath", "int", 0, "int*", 0)
    If @error Or Not IsArray($aResult) Then Return SetError(1, 1, False)
    Local $hPath = $aResult[2]

    [/autoit] [autoit][/autoit] [autoit]

    DllCall($ghGDIPDll, "uint", "GdipAddPathString", "hwnd", $hPath, "wstr", $sText, "int", -1, "hwnd", $hFamily, "int", 1, "float", 100, "ptr", DllStructGetPtr($tLayout), "hwnd", $hFormat)
    DllCall($ghGDIPDll, "uint", "GdipGetPathWorldBounds", "hwnd", $hPath, "ptr", DllStructGetPtr($tBounds), "hwnd", 0, "hwnd", 0)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_MatrixTranslate($hMatrix, -DllStructGetData($tBounds, "X"), -DllStructGetData($tBounds, "Y"))
    _GDIPlus_MatrixScale($hMatrix, $iWidth / DllStructGetData($tBounds, "Width"), $iHeight / DllStructGetData($tBounds, "Height"), True)
    DllCall($ghGDIPDll, "uint", "GdipTransformPath", "hwnd", $hPath, "hwnd", $hMatrix)

    [/autoit] [autoit][/autoit] [autoit]

    Return $hPath
    EndFunc ;==>_CreatePath

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func WM_PAINT($hWnd, $uMsgm, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBmpBuffer, 0, 0)
    Return $GUI_RUNDEFMSG
    EndFunc ;==>WM_PAINT

    [/autoit] [autoit][/autoit] [autoit]

    Func WM_ERASEBKGND($hWnd, $uMsgm, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBmpBuffer, 0, 0)
    Return True
    EndFunc ;==>WM_ERASEBKGND

    [/autoit] [autoit][/autoit] [autoit]

    Func _Exit()
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_StringFormatDispose($hFormat)
    DllCall($ghGDIPDll, "uint", "GdipDeletePath", "hwnd", $hPath)
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hGfxBuffer)
    _GDIPlus_BitmapDispose($hBmpBuffer)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    Exit
    EndFunc

    [/autoit]

    Und zwar hab ich folgende Unschönheiten beseitigt:
    .) Du erstellst bei jedem Schleifendurchgang einen neuen Pfad, ohne dass du diesen wieder aus dem Speicher freigibst.
    entweder immer wieder freigeben (GdipDeletePath), oder wie in meinem Sript nur einmal erstellen und diesen einen Pfad verwenden

    .) Die Matrix, welche die Schrift zentriert, muss nur einmal berechnet werden

    .) Du hast eine Endlosschleife in deiner Update-Funktion und rufst diese trotzdem jede Sekunde neu auf
    Ich hab es so verändert, dass der Text nur dann neu geschrieben wird, wenn dieser sich verändert hat

    .) Du solltest in Zukunft auch diese 2 Dinge berücksichtigen:
    - deklariere niemals innerhalb einer Function eine Variable Global
    - deklariere niemals innerhalb einer Schleife eine Variable, sondern immer davor


    Evtl interessiert dich das noch: einen schönen Effekt ergibt es, wenn du als Hintergrund ein Foto verwendest (_GdiPlus_ImageLoadFromFile) und den Alphakanal von $hBrush auf leicht transparent setzt...

    E

  • eukalyptus
    ich bedanke mich bei dir für deine Hilfe, und deine Hinweise, diese werde ich dann in Zukunft berücksichtigen.
    ich habe dein Script direkt getestet, und es kommt mir so vor, als würde der Zoom flüssiger laufen als davor :)

    Vielen Dank dafür.

    Nur eine Frage hätte ich noch Warum sollte man niemals innerhalb einer Schleife eine Variable Global deklarieren, wenn es dennoch zu Funktionieren scheint, diese Frage ist nur zum Verständnis für mich :)

  • Hi,

    weil diese Variable dann nur unter Umständen (der Schleifenbedingung) deklariert wird. Gilt natürlich vor allem für kopfgesteuerte Schleifen. Das kann Fehler verursachen. Außerdem ist es kein guter Stil.
    Allgemein werden Variablen immer am Programmanfang deklariert (außer lokale natürlich :D).

    Gruß stay

  • ok das klingt plausibel damit kann ich etwas anfangen :) dann werd ich mich in zukunft daran halten. nur zum verständnis,

    Global Variablen deklariere ich immer am Scriptanfang auserhalb einer schleife,
    Local Variablen deklariert man auch normalerweise am Scriptanfang, man kann sie aber auch in einer schleife für bestimmte Optionen deklarieren

    habe ich das richtig verstanden?

    • Offizieller Beitrag

    man kann sie aber auch in einer schleife für bestimmte Optionen deklarieren


    Nein, innerhalb einer Schleife notwendige Variablen sollst du vor Schleifenbeginn deklarieren. Anderenfalls wird bei jedem Durchlauf völlig unnötig die Variable neu deklariert.

    Local Variablen deklariert man auch normalerweise am Scriptanfang


    Auch nein. Locale Variablen sind, wie der Name sagt, nur in einem lokalen Umfeld, also innerhalb einer Funktion gültig. Somit erstellt man sie erst innerhalb einer Funktion.
    Wird eine Variable am Skriptanfang mit Local deklariert ist sie dann trotzdem Global.