Buttons mit Farbänderung

  • Hallo Zusammen,
    ich bin recht neu in der AutoIt Welt bin aber angenhem überrascht wie einfach man doch zu erfolgen kommen :)
    Gestern Abend hab ich mich an ein ganz kleine Gui Anwendung versucht die auch im Grunde läuft aber mit einem Logik Fehler, und da hoffe ich nun auf eure Hilfe.

    Ich habe ein kleine Gui mit 4 Buttons. Beim ersten Klick auf einem Button wird dieser Grün. So soll es auch sein. Sobald ich wieder auf diesem Button klicke wird dieser Rot. Dann wieder Grün und so weiter.
    So nun habe ich aber 4 Buttons und es Klappt nicht mehr. Wenn ich den ersten Button auf Grün klicke und dann auf den zweiten wird dieser nicht auch Grün sondern gleich Rot.
    Das soll natürlich nicht so sein. Hier mal mein Code :

    [autoit]


    #include <GUIConstantsEx.au3>
    #include <GuiButton.au3>
    #include <WindowsConstants.au3>
    Local $switch = False

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

    GUICreate('')
    $b1 = GUICtrlCreateButton("", 112, 64, 25, 25)
    $b1LEDid = "0101"
    $b2 = GUICtrlCreateButton("", 212, 64, 25, 25)
    $b2LEDid = "0100"
    $b3 = GUICtrlCreateButton("", 112, 128, 25, 25)
    $b3LEDid = "0110"
    $b4 = GUICtrlCreateButton("", 212, 128, 25, 25)
    $b4LEDid = "1100"

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

    GUISetState()

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

    While True
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
    Exit
    Case $b1
    $switch = Not $switch
    If $switch Then
    GUICtrlSetBkColor($b1,0x990000)
    _Func1 ($b1LEDid)
    Else
    GUICtrlSetBkColor($b1,0x059122)
    _Func1 ($b1LEDid)
    EndIf

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

    Case $b2
    $switch = Not $switch
    If $switch Then
    GUICtrlSetBkColor($b2,0x990000)
    _Func1 ($b2LEDid)
    Else
    GUICtrlSetBkColor($b2,0x059122)
    _Func1 ($b2LEDid)
    EndIf

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

    Case $b3
    $switch = Not $switch
    If $switch Then
    GUICtrlSetBkColor($b3,0x990000)
    _Func1 ($b3LEDid)
    Else
    GUICtrlSetBkColor($b2,0x059122)
    _Func1 ($b3LEDid)
    EndIf

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

    Case $b4
    $switch = Not $switch
    If $switch Then
    GUICtrlSetBkColor($b4,0x990000)
    _Func1 ($b2LEDid)
    Else
    GUICtrlSetBkColor($b4,0x059122)
    _Func1 ($b2LEDid)
    EndIf

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

    EndSwitch
    WEnd

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

    Func _Func1 ($var)
    MsgBox(0, "Test", $var)
    EndFunc

    [/autoit]


    Es ist doch sicher möglich das verhalten der Buttons so hin zu bekommen das die richtigen Farben kommen oder?

    Eine weiter Frage die sich mit stellt kann man den Codeteil mit dem Switch verbessern?
    Ich benötige Später nicht 4 Buttons sondern einige Hundert.

    Beste Grüße
    Luxor

    2 Mal editiert, zuletzt von luxor63 (3. April 2013 um 19:13)

    • Offizieller Beitrag

    Du benutzt die Variable $switch für alle Buttons, deshalb kommt der Fehler. Wenn, dann mußt du für jeden Button eine eigene Variable benutzen.

    Spoiler anzeigen
    [autoit]

    #region - Timestamp
    ; 2013-04-03 18:35:55
    #endregion - Timestamp

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

    #include <GUIConstantsEx.au3>
    #include <GuiButton.au3>
    #include <WindowsConstants.au3>
    Local $switch0 = False, $switch1 = False, $switch2 = False, $switch3 = False

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

    GUICreate('')
    $b1 = GUICtrlCreateButton("", 112, 64, 25, 25)
    $b1LEDid = "0101"
    $b2 = GUICtrlCreateButton("", 212, 64, 25, 25)
    $b2LEDid = "0100"
    $b3 = GUICtrlCreateButton("", 112, 128, 25, 25)
    $b3LEDid = "0110"
    $b4 = GUICtrlCreateButton("", 212, 128, 25, 25)
    $b4LEDid = "1100"

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

    GUISetState()

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

    While True
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
    Exit
    Case $b1
    $switch0 = Not $switch0
    If $switch0 Then
    GUICtrlSetBkColor($b1, 0x990000)
    _Func1($b1LEDid)
    Else
    GUICtrlSetBkColor($b1, 0x059122)
    _Func1($b1LEDid)
    EndIf

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

    Case $b2
    $switch1 = Not $switch1
    If $switch1 Then
    GUICtrlSetBkColor($b2, 0x990000)
    _Func1($b2LEDid)
    Else
    GUICtrlSetBkColor($b2, 0x059122)
    _Func1($b2LEDid)
    EndIf

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

    Case $b3
    $switch2 = Not $switch2
    If $switch2 Then
    GUICtrlSetBkColor($b3, 0x990000)
    _Func1($b3LEDid)
    Else
    GUICtrlSetBkColor($b2, 0x059122)
    _Func1($b3LEDid)
    EndIf

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

    Case $b4
    $switch3 = Not $switch3
    If $switch3 Then
    GUICtrlSetBkColor($b4, 0x990000)
    _Func1($b2LEDid)
    Else
    GUICtrlSetBkColor($b4, 0x059122)
    _Func1($b2LEDid)
    EndIf

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

    EndSwitch
    WEnd

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

    Func _Func1($var)
    MsgBox(0, "Test", $var)
    EndFunc ;==>_Func1

    [/autoit]
    • Offizieller Beitrag

    Wenn der Thread erledigt ist, setze ihn bitte auf gelöst.
    1. Post editieren, Prefix auf gelöst setzen ;)

  • Zudem würde ich dir ein Array empfehlen, wenn da noch mehr von diesen Buttons erscheinen sollen.

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <GuiButton.au3>
    #include <WindowsConstants.au3>
    Global $GMsg, $iCount
    Global $avButton[4][3]

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

    GUICreate('')
    $avButton[0][0] = GUICtrlCreateButton("", 112, 64, 25, 25)
    $avButton[0][1] = "0101"
    $avButton[1][0] = GUICtrlCreateButton("", 212, 64, 25, 25)
    $avButton[1][1] = "0100"
    $avButton[2][0] = GUICtrlCreateButton("", 112, 128, 25, 25)
    $avButton[2][1] = "0110"
    $avButton[3][0] = GUICtrlCreateButton("", 212, 128, 25, 25)
    $avButton[3][1] = "1100"

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

    GUISetState()

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

    While True
    $GMsg = GUIGetMsg()
    Switch $GMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch

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

    For $iCount = 0 To UBound($avButton) - 1
    If $GMsg = $avButton[$iCount][0] Then ; Wenn Button gedrückt wird...
    $avButton[$iCount][2] = Not $avButton[$iCount][2]
    If $avButton[$iCount][2] Then
    GUICtrlSetBkColor($avButton[$iCount][0], 0x990000)
    _Func1($avButton[$iCount][1])
    Else
    GUICtrlSetBkColor($avButton[$iCount][0], 0x059122)
    _Func1($avButton[$iCount][1])
    EndIf
    EndIf
    Next
    WEnd

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

    Func _Func1($var)
    MsgBox(0, "Test", $var)
    EndFunc ;==>_Func1

    [/autoit]

    Einmal editiert, zuletzt von Yjuq (3. April 2013 um 18:58)

  • als erstes solltest du AutoIt-Quellcode so posten :

    Code
    [autoit];Hier der Code[/autoit]


    Ich würde den Status + die ControlID in einem Array speichern

    In etwa so :

    Spoiler anzeigen
    [autoit]

    #include <GUIConstantsEx.au3>
    #include <GuiButton.au3>
    #include <WindowsConstants.au3>
    Global $aButtons[4][3]

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

    GUICreate('')
    $aButtons[0][0] = GUICtrlCreateButton("", 112, 64, 25, 25)
    $aButtons[0][2] = "0101"
    $aButtons[1][0] = GUICtrlCreateButton("", 212, 64, 25, 25)
    $aButtons[1][2] = "0100"
    $aButtons[2][0] = GUICtrlCreateButton("", 112, 128, 25, 25)
    $aButtons[2][2] = "0110"
    $aButtons[3][0] = GUICtrlCreateButton("", 212, 128, 25, 25)
    $aButtons[3][2] = "1100"

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

    GUISetState()

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

    While True
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
    Exit
    Case $aButtons[0][0]
    _SwitchColor(0)
    Case $aButtons[1][0]
    _SwitchColor(1)
    Case $aButtons[2][0]
    _SwitchColor(2)
    Case $aButtons[3][0]
    _SwitchColor(3)
    EndSwitch
    WEnd

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

    Func _SwitchColor ($iIndex)
    $aButtons[$iIndex][1] = Not $aButtons[$iIndex][1]
    If $aButtons[$iIndex][1] Then
    GUICtrlSetBkColor($aButtons[$iIndex][0],0x990000)
    Else
    GUICtrlSetBkColor($aButtons[$iIndex][0],0x059122)
    EndIf
    MsgBox(0, "Test", $aButtons[$iIndex][2])
    EndFunc

    [/autoit]

    Edit : Eine For-Schleife ist natürlich noch komfortabler, danke Make-Grafik ...

  • Eine weiter Frage die sich mit stellt kann man den Codeteil mit dem Switch verbessern?
    Ich benötige Später nicht 4 Buttons sondern einige Hundert.


    Du musst dazu in einem 2D-Array die ControlId und den Zustand eintragen. Hier die daraus resultierende Kurzfassung:

    Spoiler anzeigen

    [autoit#include <GUIConstantsEx.au3>

    Global $aBtnIds[9][3]
    $aBtnIds[0][2] = "0101"
    $aBtnIds[1][2] = "0100"
    $aBtnIds[2][2] = "0110"
    $aBtnIds[3][2] = "1100"


    $hGui = GUICreate('Buttontest', 105, 105)
    For $i = 0 To 8
    $aBtnIds[$i][0] = GUICtrlCreateButton($i + 1, 10 + Mod($i, 3) * 30, 10 + Int($i / 3) * 30, 25, 25)
    Next
    GUISetState()

    While 1
    $msg = GUIGetMsg()
    Switch $msg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $aBtnIds[0][0] To $aBtnIds[8][0]
    _Toggle($msg - $aBtnIds[0][0])
    EndSwitch
    WEnd

    Func _Toggle($iBtn)
    If $aBtnIds[$iBtn][1] Then
    GUICtrlSetBkColor($aBtnIds[$iBtn][0], 0x990000)
    Else
    GUICtrlSetBkColor($aBtnIds[$iBtn][0], 0x059122)
    EndIf
    $aBtnIds[$iBtn][1] = Not $aBtnIds[$iBtn][1]
    MsgBox(0, 'Button', 'No. ' & $aBtnIds[$iBtn][0] + 1 & ' was clicked'&@CRLF&'Var: '&$aBtnIds[$iBtn][2]&@CRLF&'True/False: '&$aBtnIds[$iBtn][1], 0, $hGui)
    EndFunc ;==>_Toggle[/autoit]

    mfg autoBert

    Einmal editiert, zuletzt von autoBert (3. April 2013 um 20:03)