Bild auf Kommando mit einstellbarer Geschwindigkeit drehen

  • Für den Physikunterricht möchte ich ei programm schreiben welches das Drehen eines Elektromotors simulieren soll.
    Das Problem ist, dass kein Rotor angezeigt wird und das er sich nicht dreht.

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <WindowsConstants.au3>
    #include <GDIPlus.au3>
    #Include <WinAPI.au3>

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

    TrayTip("ACHTUNG!","Durch Klick auf dieses Symbol lässt sich das Programm beenden!!",1000)
    $hGUI = GUICreate("Elektromotor", 704, 780);,$DS_SETFOREGROUND))
    GUICtrlCreateLabel("Multiplikator",610,640)
    $combo = GUICtrlCreateCombo("",610,660,50,20)
    $slider = GUICtrlCreateSlider(200,660,400,30)
    $start = GUICtrlCreateButton("Strom an",50,660,100,30)
    GUICtrlCreateLabel("Anmerkungen:"&@CRLF&" -Das Programm lässt sich nicht durch Klick auf X oder 'Strom aus' beenden"&@CRLF&" -Durch den Slider lässt sich die Geschwindigkeit einstellen"&@CRLF&"DIESES PROGRAMM IST NOCH IM BETA-ZUSTAND; DIESE PROBLEME WERDEN GEFIXT!!",10,700)
    GUICtrlSetData($combo,"|1|2|3|4|5|6|","1")

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

    _GDIPlus_StartUp()
    $hImageStator = _GDIPlus_ImageLoadFromFile("Stator.png")
    $hImageKontakt = _GDIPlus_ImageLoadFromFile("Schleifkontakte.png")
    $hGraphic1 = _GDIPlus_GraphicsCreateFromHWND($hGUI)
    $hImageRotor = _GDIPlus_ImageLoadFromFile("rotor_small.png")
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImageRotor)

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

    $hMatrix = _GDIPlus_MatrixCreate()
    _GDIPlus_MatrixTranslate($hMatrix,0,0)

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

    _GDIPlus_MatrixRotate($hMatrix, 180, "False")
    _GDIPlus_GraphicsSetTransform($hGraphic, $hMatrix)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImageRotor, 100, 100)

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

    GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
    GUISetState()

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

    Func MY_WM_PAINT($hWnd, $Msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($hGraphic1, $hImageStator, 0, 0)
    _GDIPlus_GraphicsDrawImage($hGraphic1, $hImageKontakt, 0, 0)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImageRotor, 100, 100)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
    EndFunc

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

    While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $start

    GUICtrlSetData($start,"Strom aus")

    Do
    $multi = GUICtrlRead($combo)
    $msg = GUIGetMsg()
    $speed = 100-GUICtrlRead($slider)
    Sleep($speed*$multi)
    $msg = GUIGetMsg()
    Sleep($speed*$multi)
    $msg = GUIGetMsg()
    Sleep($speed*$multi)
    $msg = GUIGetMsg()
    Sleep($speed*$multi)
    $msg = GUIGetMsg()
    Sleep($speed*$multi)
    $msg = GUIGetMsg()
    Sleep($speed*$multi)
    $msg = GUIGetMsg()
    Until $msg = $start
    If $msg = $GUI_EVENT_CLOSE Then Exit
    GUICtrlSetData($start,"Strom an")
    Case $msg = $GUI_EVENT_CLOSE
    Exit
    EndSelect
    WEnd

    [/autoit]

    Was mache ich falsch?

  • Ich hab dir mal ein Beispiel für deinen Rotor gemacht ;).
    Sogar mit Backbuffer 8) .

    Spoiler anzeigen
    [autoit]

    #include <GDIPLus.au3>
    #include <GUIConstants.au3>

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

    Opt("GUIOnEventMode", 1)

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

    $hWnd = GUICreate("Motor", 400, 400)
    GUISetState()

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

    _GDIPlus_Startup()

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

    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics(400, 400, $hGraphic)
    $hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    _GDIPlus_GraphicsSetSmoothingMode($hBuffer, 2)

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

    $hImage_Rotor = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Rotor.png")

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

    $hMatrix_Rotate = _GDIPlus_MatrixCreate()
    _GDIPlus_MatrixTranslate($hMatrix_Rotate, 200, 200)

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

    OnAutoItExitRegister("_Exit")
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

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

    $iAngle = 5

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

    AdlibRegister("_ReDraw", 20)

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

    While Sleep(200)
    WEnd

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

    Func _ReDraw()
    _GDIPlus_GraphicsClear($hBuffer, 0xFFFFFFFF)
    _GDIPlus_MatrixRotate($hMatrix_Rotate, $iAngle)
    _GDIPlus_GraphicsSetTransform($hBuffer, $hMatrix_Rotate)
    _GDIPlus_GraphicsDrawImageRect($hBuffer, $hImage_Rotor, -200, -200, 400, 400)
    _GDIPlus_GraphicsResetTransform($hBuffer)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, 400, 400)
    EndFunc

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

    Func _Exit()
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_GraphicsDispose($hBuffer)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_ImageDispose($hImage_Rotor)
    _GDIPlus_MatrixDispose($hMatrix_Rotate)
    _GDIPlus_Shutdown()
    Exit
    EndFunc

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

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _GDIPlus_GraphicsResetTransform
    ; Description ...: Sets the world transformation matrix of a Graphics object to the identity matrix
    ; Syntax.........: _GDIPlus_GraphicsResetTransform($hGraphics)
    ; Parameters ....: $hGraphics - Pointer to a Graphics object
    ; Return values .: Success - True
    ; Failure - False and either:
    ; |@error and @extended are set if DllCall failed
    ; |$GDIP_STATUS contains a non zero value specifying the error code
    ; Remarks .......: None
    ; Related .......: None
    ; Link ..........; @@MsdnLink@@ GdipResetWorldTransform
    ; Example .......; No
    ; ===============================================================================================================================
    Func _GDIPlus_GraphicsResetTransform($hGraphics)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipResetWorldTransform", "hwnd", $hGraphics)

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

    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
    EndFunc ;==>_GDIPlus_GraphicsResetTransform

    [/autoit]
  • Genau das selbe haben wie vor kurzem auch in Physik gemach :D

    [autoit]


    While $Life = True
    $nMSG = BrainGetMsg()
    Switch $nMSG
    Case $Idea
    _Convert_Idea2Code()
    EndSwitch
    WEnd

    [/autoit]
  • Es gibt ein paar Sachen die mir an deinem Code komisch vorkommen...

    [autoit]

    _GDIPlus_MatrixTranslate($hMatrix,0,0)

    [/autoit]


    Was hat das für einen Sinn? Das beeinträchtigt die Matrix kein bisschen...

    [autoit]

    _GDIPlus_MatrixRotate($hMatrix, 180, "False")
    _GDIPlus_GraphicsSetTransform($hGraphic, $hMatrix)

    [/autoit]


    Wieso drehst du die Matrix ein Mal und bleibst danach in der GUI Schleife?
    Und das Bild wird auch nur ein Mal gezeichnet. :huh:

  • Zitat
    [autoit]

    _GDIPlus_MatrixRotate($hMatrix, 180, "False")
    _GDIPlus_GraphicsSetTransform($hGraphic, $hMatrix)

    [/autoit]

    Wieso drehst du die Matrix ein Mal und bleibst danach in der GUI Schleife?
    Und das Bild wird auch nur ein Mal gezeichnet. :huh:

    Ich habe nicht bis zu Ende geschreiben weil er schon ein Bild nicht gedreht oder angezeigt hat.
    Ich habs direkt hier gepostet.

  • Hier ein "fast hack" von mir:

    Spoiler anzeigen
    [autoit]


    #include <GDIplus.au3>
    Opt("GUIOnEventMode", 1)

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

    _GDIPlus_Startup()
    Global $Stator = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\Stator.png")
    Global $Rotor = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\Rotor_small.png")
    Global $Schleifkontakte = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\Schleifkontakte.png")
    Global $width = _GDIPlus_ImageGetWidth($Stator)
    Global $height = _GDIPlus_ImageGetHeight($Stator)

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

    Global $dx = _GDIPlus_ImageGetWidth($Rotor)
    Global $dy = _GDIPlus_ImageGetHeight($Rotor)

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

    Global $i

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

    Global $hWnd = GUICreate("GDI+: Example by UEZ", $width, $height, -1, -1, Default)
    GUISetOnEvent(-3, "_Exit")
    GUISetState()

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

    Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hWnd)
    Global $hBitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphics)
    Global $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)

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

    Global $hFGBitmap = _GDIPlus_BitmapCreateFromGraphics($dx, $dy, $hGraphics)
    Global $hFGBackbuffer = _GDIPlus_ImageGetGraphicsContext($hFGBitmap)

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

    Global $hMatrix = _GDIPlus_MatrixCreate()
    _GDIPlus_MatrixTranslate($hMatrix, $dx / 2, $dy / 2)
    _GDIPlus_GraphicsClear($hBackbuffer, 0xFFFFFFFF)

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

    Global $FG_x = 165
    Global $FG_y = 165
    Global $mid_x = $dx * 0.5
    Global $mid_y = $dy * 0.5

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

    $i = 5

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

    While Sleep(20)
    _GDIPlus_GraphicsDrawImage($hBackbuffer, $Stator, 0, 0) ;draw background
    _GDIPlus_GraphicsDrawImage($hBackbuffer, $Schleifkontakte, 0, 185) ;draw background
    _GDIPlus_GraphicsClear($hFGBackbuffer, 0xFFFFFFFF) ;clear backbuffer from foreground graphics

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

    _GDIPlus_MatrixRotate($hMatrix, $i, 0) ;rotate foreground graphic
    _GDIPlus_GraphicsSetTransform($hFGBackbuffer, $hMatrix)
    _GDIPlus_GraphicsDrawImage($hFGBackbuffer, $Rotor, -$mid_x, -$mid_y)

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

    _GDIPlus_GraphicsDrawImageRect($hBackbuffer, $hFGBitmap, $FG_x, $FG_y, $dx, $dy) ;copy foreground backbuffer to main backbuffer
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, $width, $height) ;copy full drawn image to main screen
    WEnd

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

    Func _Exit()
    _GDIPlus_BitmapDispose($Stator)
    _GDIPlus_BitmapDispose($Rotor)
    _GDIPlus_BitmapDispose($Schleifkontakte)
    _GDIPlus_BitmapDispose($hFGBitmap)
    _GDIPlus_GraphicsDispose($hFGBackbuffer)
    _GDIPlus_MatrixDispose($hMatrix)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hBackbuffer)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    Exit
    EndFunc

    [/autoit]

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

  • Hab eure Beispiele jetzt zusammengefasst; Hier das Ergebnis:

    Spoiler anzeigen
    [autoit]

    #include <GDIplus.au3>
    #include <GUIConstants.au3>
    #include <EditConstants.au3>
    Opt("GUIOnEventMode", 1)

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

    _GDIPlus_Startup()
    Global $Stator = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\Stator.png")
    Global $Rotor = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\Rotor_small.png")
    Global $Schleifkontakte = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\Schleifkontakte.png")
    Global $width = _GDIPlus_ImageGetWidth($Stator)
    Global $height = _GDIPlus_ImageGetHeight($Stator)

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

    Global $dx = _GDIPlus_ImageGetWidth($Rotor)
    Global $dy = _GDIPlus_ImageGetHeight($Rotor)

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

    Global $i

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

    Global $hWnd = GUICreate("Elektromotor by Jan Scheiper mit freundlicher Mithilfe von UEZ & name22", 704, 780, -1, -1, Default)
    GUICtrlCreateLabel("Wert:",50,733)
    $slider = GUICtrlCreateSlider(50,670,604,34)
    $speed = GUICtrlCreateInput("0",80,730,30,20,$ES_READONLY)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    GUISetState()

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

    Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hWnd)
    Global $hBitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphics)
    Global $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)

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

    Global $hFGBitmap = _GDIPlus_BitmapCreateFromGraphics($dx, $dy, $hGraphics)
    Global $hFGBackbuffer = _GDIPlus_ImageGetGraphicsContext($hFGBitmap)

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

    Global $hMatrix = _GDIPlus_MatrixCreate()
    _GDIPlus_MatrixTranslate($hMatrix, $dx / 2, $dy / 2)
    _GDIPlus_GraphicsClear($hBackbuffer, 0xFFFFFFFF)

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

    Global $FG_x = 163
    Global $FG_y = 221
    Global $mid_x = $dx * 0.5
    Global $mid_y = $dy * 0.5

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

    $i = 0

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

    While Sleep(20)
    GUICtrlSetData($speed,GUICtrlRead($slider))
    $i = GUICtrlRead($speed)
    _GDIPlus_GraphicsDrawImage($hBackbuffer, $Stator, 0, 0) ;draw background
    _GDIPlus_GraphicsClear($hFGBackbuffer, 0xFFFFFFFF) ;clear backbuffer from foreground graphics

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

    _GDIPlus_MatrixRotate($hMatrix, $i, 0) ;rotate foreground graphic
    _GDIPlus_GraphicsSetTransform($hFGBackbuffer, $hMatrix)
    _GDIPlus_GraphicsDrawImage($hFGBackbuffer, $Rotor, -$mid_x, -$mid_y)

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

    _GDIPlus_GraphicsDrawImageRect($hBackbuffer, $hFGBitmap, $FG_x, $FG_y, $dx, $dy) ;copy foreground backbuffer to main backbuffer
    _GDIPlus_GraphicsDrawImage($hBackbuffer, $Schleifkontakte, 0, 0)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, $width, $height) ;copy full drawn image to main screen

    WEnd

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

    Func _Exit()
    _GDIPlus_BitmapDispose($Stator)
    _GDIPlus_BitmapDispose($Rotor)
    _GDIPlus_BitmapDispose($Schleifkontakte)
    _GDIPlus_BitmapDispose($hFGBitmap)
    _GDIPlus_GraphicsDispose($hFGBackbuffer)
    _GDIPlus_MatrixDispose($hMatrix)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hBackbuffer)
    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_Shutdown()
    Exit
    EndFunc

    [/autoit]

    Danke für die Hilfe