Pic wechseln und Prozess über einen Button starten/beenden

  • Hi,

    Also nach langer Zeit habe ich mich dann doch entschlossen mich mal hier zu melden mit einem (hoffentlich) kleinen (möglichem) Problem.

    Eigendlich sind's zwei Probleme zum einen möchte ich mit einem Button eine Anwendung starten und beenden.
    also halt:
    Stadartmäßig OFF nach dem klicken auf den button ON und nach einem weiteren klicken wieder OFF, wie ein Schalter eben.
    Dazu soll neben diesem Button, eine leuchte sein. (Ein Bild halt) sollte bei off dunkelgrün und bei on hellgrün sein.

    Wie mach ich das? (Hoffe das ist halbwegsverständlich^^)

    Einmal editiert, zuletzt von vism (21. Dezember 2008 um 13:59)

    • Offizieller Beitrag

    Ich wollte gerade mal etwas mit GDI+ rumspielen, deshalb habe ich Dir dieses Beispielscript geschrieben:

    Spoiler anzeigen
    [autoit]


    #include <GuiConstantsEx.au3>
    #include <GDIPlus.au3>

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

    $hGUI = GUICreate("GDI+", 400, 300)
    $hWnd = WinGetHandle("GDI+")
    GUISetBkColor(0x000000)
    GUISetState()
    _GDIPlus_Startup()
    $hBrush1 = _GDIPlus_BrushCreateSolid(0xFF00FF00)
    $hBrush2 = _GDIPlus_BrushCreateSolid(0xFF004400)
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)
    $hButton = GUICtrlCreateButton('Programm starten', 60, 23, 100, 22)
    $bOnOff = False
    _GDIPlus_GraphicsFillEllipse($hGraphic, 180, 20, 25, 25, $hBrush2)

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

    While True
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
    ExitLoop
    case $hButton
    $bOnOff = Not $bOnOff
    Switch $bOnOff
    Case True
    _GDIPlus_GraphicsFillEllipse($hGraphic, 180, 20, 25, 25, $hBrush1)
    GUICtrlSetData($hButton, 'Programm stoppen')
    $PID = Run('Calc.exe')
    Case False
    _GDIPlus_GraphicsFillEllipse($hGraphic, 180, 20, 25, 25, $hBrush2)
    GUICtrlSetData($hButton, 'Programm starten')
    ProcessClose($PID)
    EndSwitch
    EndSwitch
    WEnd
    _GDIPlus_BrushDispose($hBrush1)
    _GDIPlus_BrushDispose($hBrush2)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()

    [/autoit]
  • Vielen Dank schonmal.

    Gut das bringt mich schonmal ein ganzes Stück weiter, wie mach ich das denn jetz wenn ich nen' 2ten Button brauch? Also wie schieb ich das dazwischen? Oder muss man die ganze "Konstruktion" kopieren? :D

    • Offizieller Beitrag

    Na gut, dann hier ein Script zum starten/stoppen von 20 Programmen. Musst nur noch oben die Programme (inkl. Pfad) eintragen:

    Spoiler anzeigen
    [autoit]


    #include <GuiConstantsEx.au3>
    #include <GDIPlus.au3>

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

    Opt('GUIOnEventMode', 1); Den OnEvent-Mode benutzen

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

    Global $ahButton[20], $abOnOff[20], $aPID[20], $aProg[20]
    $aProg[0] = 'calc.exe'
    $aProg[1] = 'calc.exe'
    $aProg[2] = 'calc.exe'
    $aProg[3] = 'calc.exe'
    $aProg[4] = 'calc.exe'
    $aProg[5] = 'calc.exe'
    $aProg[6] = 'calc.exe'
    $aProg[7] = 'calc.exe'
    $aProg[8] = 'calc.exe'
    $aProg[9] = 'calc.exe'
    $aProg[10] = 'calc.exe'
    $aProg[11] = 'calc.exe'
    $aProg[12] = 'calc.exe'
    $aProg[13] = 'calc.exe'
    $aProg[14] = 'calc.exe'
    $aProg[15] = 'calc.exe'
    $aProg[16] = 'calc.exe'
    $aProg[17] = 'calc.exe'
    $aProg[18] = 'calc.exe'
    $aProg[19] = 'calc.exe'

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

    $hGUI = GUICreate("GDI+", 400, 640)
    GUISetOnEvent($GUI_EVENT_CLOSE, '_End')
    $hWnd = WinGetHandle("GDI+")
    GUISetBkColor(0x000000)
    GUISetState()
    _GDIPlus_Startup()
    $hBrush1 = _GDIPlus_BrushCreateSolid(0xFF00FF00)
    $hBrush2 = _GDIPlus_BrushCreateSolid(0xFF004400)
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
    _GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)

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

    For $i = 0 To 19
    $ahButton[$i] = GUICtrlCreateButton('Programm "' & $i+1 & '" starten', 30, 23 + $i * 30, 130, 22)
    GUICtrlSetOnEvent(-1, '_ButtonKlick')
    $abOnOff[$i] = False
    _GDIPlus_GraphicsFillEllipse($hGraphic, 180, 20 + $i * 30, 25, 25, $hBrush2)
    Next

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

    While True
    Sleep(20)
    WEnd

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

    Func _ButtonKlick()
    Local $i = @GUI_CtrlId - $ahButton[0]
    $abOnOff[$i] = Not $abOnOff[$i]
    Switch $abOnOff[$i]
    Case True
    _GDIPlus_GraphicsFillEllipse($hGraphic, 180, 20 + $i * 30, 25, 25, $hBrush1)
    GUICtrlSetData($ahButton[$i], 'Programm "' & $i+1 & '" stoppen')
    $aPID[$i] = Run($aProg[$i])
    Case False
    _GDIPlus_GraphicsFillEllipse($hGraphic, 180, 20 + $i * 30, 25, 25, $hBrush2)
    GUICtrlSetData($ahButton[$i], 'Programm "' & $i+1 & '" starten')
    ProcessClose($aPID[$i])
    EndSwitch
    EndFunc

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

    Func _End()
    _GDIPlus_BrushDispose($hBrush1)
    _GDIPlus_BrushDispose($hBrush2)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    Exit
    EndFunc

    [/autoit]
  • Vielen Dank =)

    Hast mir meinen Hintern gerettet ;D

    [EDIT:]
    Da gäbs noch was :D Mal angenommen ich bräuchte ne Andere Bezeichnung als ne Zahl von 1 bis 20, wie kann ich das klären? :D

    [EDIT2:]
    Nochwas :D gibts auch ne möglichkeit dann mittels einem weiteren Button alle anderen auszuschalten sprich, alle auf den OFF status zu stellen? :D
    (>Wird ja scho peinlich für mich iwi :D bin sons net der der gern fragt. XD<)

    [EDIT3:]
    Naja ich denke mit arays werd ich da net weit komme, da ich die schließlich auch irgendwie hinternander bringen muss =(
    In der Sache bin ich total der noob :P

    Da ich die tolle aufgabe hab das in einer bestimmten Reihenfolge aufzubauen, kapier ichs wieder net.
    Original Zitat:
    Es geht nach wie vor um Buttons >_>
    1 Reihe = 2
    2+3Reihe = 5
    4 Reihe = 3
    5 Reihe = 2
    6 Reihe = 1
    Zitat_Ende:>
    Schule SuxXT!

    Wären arrays da eigendlich sinnvoll? Oder wäre standartmäßiges zuweisen da die bessere Lösung?^^ Hab da echt kA :D

    4 Mal editiert, zuletzt von vism (21. Dezember 2008 um 01:36)

    • Offizieller Beitrag

    Du hast Glück, dass mich das Thema gerade interessiert. :D
    So habe ich das Script noch etwas ausgebaut:

    Spoiler anzeigen
    [autoit]


    #include <GuiConstantsEx.au3>

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

    Opt('GUIOnEventMode', 1); Den OnEvent-Mode benutzen

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

    Global $iCount = 18 ; Anzahl der Buttons
    Global $ahButton[$iCount], $abOnOff[$iCount], $aPID[$iCount], $aProg[$iCount], $aProgName[$iCount], $ahIcon[$iCount]

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

    $aProg[0] = 'calc.exe' ; die zu startenden Programme eintragen (inkl. Pfad)
    $aProg[1] = 'calc.exe'
    $aProg[2] = 'calc.exe'
    $aProg[3] = 'calc.exe'
    $aProg[4] = 'calc.exe'
    $aProg[5] = 'calc.exe'
    $aProg[6] = 'calc.exe'
    $aProg[7] = 'calc.exe'
    $aProg[8] = 'calc.exe'
    $aProg[9] = 'calc.exe'
    $aProg[10] = 'calc.exe'
    $aProg[11] = 'calc.exe'
    $aProg[12] = 'calc.exe'
    $aProg[13] = 'calc.exe'
    $aProg[14] = 'calc.exe'
    $aProg[15] = 'calc.exe'
    $aProg[16] = 'calc.exe'
    $aProg[17] = 'calc.exe'

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

    $aProgName[0] = 'Rechner' ; die dazugehörigen Namen (werden auf den Buttons angezeigt)
    $aProgName[1] = 'Rechner'
    $aProgName[2] = 'Rechner'
    $aProgName[3] = 'Rechner'
    $aProgName[4] = 'Rechner'
    $aProgName[5] = 'Rechner'
    $aProgName[6] = 'Rechner'
    $aProgName[7] = 'Rechner'
    $aProgName[8] = 'Rechner'
    $aProgName[9] = 'Rechner'
    $aProgName[10] = 'Rechner'
    $aProgName[11] = 'Rechner'
    $aProgName[12] = 'Rechner'
    $aProgName[13] = 'Rechner'
    $aProgName[14] = 'Rechner'
    $aProgName[15] = 'Rechner'
    $aProgName[16] = 'Rechner'
    $aProgName[17] = 'Rechner'

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

    ; das folgende Array enthält die Positionsdaten der Buttons (jeweils x,y)
    Global $aPos[$iCount][2] = [ _
    [170, 20], _
    [450, 20], _
    [30, 80], _
    [170, 80], _
    [310, 80], _
    [450, 80], _
    [600, 80], _
    [30, 140], _
    [170, 140], _
    [310, 140], _
    [450, 140], _
    [600, 140], _
    [170, 200], _
    [310, 200], _
    [450, 200], _
    [170, 260], _
    [450, 260], _
    [310, 320] _
    ]
    Global $aIconPath[2] = [@ScriptDir & '\off.ico', @ScriptDir & '\on.ico'] ; Pfade zu den Icondateien

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

    GUICreate('ProgStarter', 760, 430)
    GUISetOnEvent($GUI_EVENT_CLOSE, '_End')
    GUISetBkColor(0x000000)
    For $i = 0 To $iCount - 1
    $ahButton[$i] = GUICtrlCreateButton($aProgName[$i], $aPos[$i][0], $aPos[$i][1], 80, 20)
    GUICtrlSetOnEvent(-1, '_Switch')
    $abOnOff[$i] = False
    $ahIcon[$i] = GUICtrlCreateIcon($aIconPath[0], -1, $aPos[$i][0] + 85, $aPos[$i][1] - 2, 24, 24)
    Next
    GUICtrlCreateButton('Alle starten', 190, 380, 120, 35)
    GUICtrlSetOnEvent(-1, '_StartAll')
    GUICtrlCreateButton('Alle stoppen', 390, 380, 120, 35)
    GUICtrlSetOnEvent(-1, '_StopAll')

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

    GUISetState()

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

    While True
    Sleep(20)
    WEnd

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

    Func _StartAll()
    For $i = 0 To $iCount - 1
    If Not $aPID[$i] Then $aPID[$i] = Run($aProg[$i])
    $abOnOff[$i] = True
    GUICtrlSetImage($ahIcon[$i], $aIconPath[1])
    Next
    EndFunc ;==>_StartAll

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

    Func _StopAll()
    For $i = 0 To $iCount - 1
    If $aPID[$i] Then
    ProcessClose($aPID[$i])
    $aPID[$i] = 0
    EndIf
    If $abOnOff[$i] Then
    $abOnOff[$i] = False
    GUICtrlSetImage($ahIcon[$i], $aIconPath[0])
    EndIf
    Next
    EndFunc ;==>_StopAll

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

    Func _Switch()
    Local $i = (@GUI_CtrlId - $ahButton[0]) / 2
    $abOnOff[$i] = Not $abOnOff[$i]
    Switch $abOnOff[$i]
    Case True
    GUICtrlSetImage($ahIcon[$i], $aIconPath[1])
    $aPID[$i] = Run($aProg[$i])
    Case False
    GUICtrlSetImage($ahIcon[$i], $aIconPath[0])
    ProcessClose($aPID[$i])
    $aPID[$i] = 0
    EndSwitch
    EndFunc ;==>_Switch

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

    Func _End()
    Exit
    EndFunc ;==>_End

    [/autoit]

    Jetzt mit Icons statt GDI+ und variablen Positionen sowie individuelle Namen für die Buttons. Das Script sowie die Icons befinden sich in dem ZIP-Archiv im Anhang!

  • Sehr geile Sache. Ich bedanke mich =)>

    Ein letztes gäbs da noch :D gibs auch ne möglichkeit die Buttons von der größe her anzupassen ?> Einzeln<
    Muss aber net sein :D> hast mir ja scho so sehr geholfen. xD Das es mich scho im Herz schmerzt weiter Fragen zu stellen. XD

    • Offizieller Beitrag

    Dazu braucht man nur ein paar kleine Änderungen:

    Spoiler anzeigen
    [autoit]


    #include <GuiConstantsEx.au3>

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

    Opt('GUIOnEventMode', 1); Den OnEvent-Mode benutzen

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

    Global $iCount = 18 ; Anzahl der Buttons
    Global $ahButton[$iCount], $abOnOff[$iCount], $aPID[$iCount], $aProg[$iCount], $aProgName[$iCount], $ahIcon[$iCount]

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

    $aProg[0] = 'calc.exe' ; die zu startenden Programme eintragen (inkl. Pfad)
    $aProg[1] = 'calc.exe'
    $aProg[2] = 'calc.exe'
    $aProg[3] = 'calc.exe'
    $aProg[4] = 'calc.exe'
    $aProg[5] = 'calc.exe'
    $aProg[6] = 'calc.exe'
    $aProg[7] = 'calc.exe'
    $aProg[8] = 'calc.exe'
    $aProg[9] = 'calc.exe'
    $aProg[10] = 'calc.exe'
    $aProg[11] = 'calc.exe'
    $aProg[12] = 'calc.exe'
    $aProg[13] = 'calc.exe'
    $aProg[14] = 'calc.exe'
    $aProg[15] = 'calc.exe'
    $aProg[16] = 'calc.exe'
    $aProg[17] = 'calc.exe'

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

    $aProgName[0] = 'Rechner' ; die dazugehörigen Namen (werden auf den Buttons angezeigt)
    $aProgName[1] = 'Rechner'
    $aProgName[2] = 'Rechner'
    $aProgName[3] = 'Rechner'
    $aProgName[4] = 'Rechner'
    $aProgName[5] = 'Rechner'
    $aProgName[6] = 'Rechner'
    $aProgName[7] = 'Rechner'
    $aProgName[8] = 'Rechner'
    $aProgName[9] = 'Rechner'
    $aProgName[10] = 'Rechner'
    $aProgName[11] = 'Rechner'
    $aProgName[12] = 'Rechner'
    $aProgName[13] = 'Rechner'
    $aProgName[14] = 'Rechner'
    $aProgName[15] = 'Rechner'
    $aProgName[16] = 'Rechner'
    $aProgName[17] = 'Rechner'

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

    ; das folgende Array enthält die Positionsdaten und Größe der Buttons (jeweils: left, top, width, height)
    Global $aPos[$iCount][4] = [ _
    [170, 020, 080, 020], _
    [450, 020, 080, 020], _
    [030, 080, 080, 020], _
    [170, 080, 080, 020], _
    [310, 080, 080, 020], _
    [450, 080, 080, 020], _
    [600, 080, 080, 020], _
    [030, 140, 080, 020], _
    [170, 140, 080, 020], _
    [310, 140, 080, 020], _
    [450, 140, 080, 020], _
    [600, 140, 080, 020], _
    [170, 200, 080, 020], _
    [310, 200, 080, 020], _
    [450, 200, 080, 020], _
    [170, 260, 080, 020], _
    [450, 260, 080, 020], _
    [310, 320, 080, 020] _
    ]
    Global $aIconPath[2] = [@ScriptDir & '\off.ico', @ScriptDir & '\on.ico'] ; Pfade zu den Icondateien

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

    GUICreate('ProgStarter', 760, 430)
    GUISetOnEvent($GUI_EVENT_CLOSE, '_End')
    GUISetBkColor(0x000000)
    For $i = 0 To $iCount - 1
    $ahButton[$i] = GUICtrlCreateButton($aProgName[$i], $aPos[$i][0], $aPos[$i][1], $aPos[$i][2], $aPos[$i][3])
    GUICtrlSetOnEvent(-1, '_Switch')
    $abOnOff[$i] = False
    $ahIcon[$i] = GUICtrlCreateIcon($aIconPath[0], -1, $aPos[$i][0] + $aPos[$i][2] + 5, $aPos[$i][1] - 2, 24, 24)
    Next
    GUICtrlCreateButton('Alle starten', 190, 380, 120, 35)
    GUICtrlSetOnEvent(-1, '_StartAll')
    GUICtrlCreateButton('Alle stoppen', 390, 380, 120, 35)
    GUICtrlSetOnEvent(-1, '_StopAll')

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

    GUISetState()

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

    While True
    Sleep(20)
    WEnd

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

    Func _StartAll()
    For $i = 0 To $iCount - 1
    If Not $aPID[$i] Then $aPID[$i] = Run($aProg[$i])
    $abOnOff[$i] = True
    GUICtrlSetImage($ahIcon[$i], $aIconPath[1])
    Next
    EndFunc ;==>_StartAll

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

    Func _StopAll()
    For $i = 0 To $iCount - 1
    If $aPID[$i] Then
    ProcessClose($aPID[$i])
    $aPID[$i] = 0
    EndIf
    If $abOnOff[$i] Then
    $abOnOff[$i] = False
    GUICtrlSetImage($ahIcon[$i], $aIconPath[0])
    EndIf
    Next
    EndFunc ;==>_StopAll

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

    Func _Switch()
    Local $i = (@GUI_CtrlId - $ahButton[0]) / 2
    $abOnOff[$i] = Not $abOnOff[$i]
    Switch $abOnOff[$i]
    Case True
    GUICtrlSetImage($ahIcon[$i], $aIconPath[1])
    $aPID[$i] = Run($aProg[$i])
    Case False
    GUICtrlSetImage($ahIcon[$i], $aIconPath[0])
    ProcessClose($aPID[$i])
    $aPID[$i] = 0
    EndSwitch
    EndFunc ;==>_Switch

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

    Func _End()
    _StopAll()
    Exit
    EndFunc ;==>_End

    [/autoit]