TreeView auslesen/verarbeiten

  • Hallo zusammen,

    habe ein Problem mir meinem TreeView.

    Ich nutze GuiCtrlCreateTreeView um eine Liste von Software zur Installation auswählen zu können.
    Leider habe ich bisher keine Möglichkeit gefunden, zu prüfen, ob diese checked sind oder nicht.

    Kann mir da vielleicht jemand helfen?

    • Offizieller Beitrag

    HI,

    wo ist dein Problem?

    Habe das Hilfebsp mal für 2 Items angepaßt.

    Spoiler anzeigen
    [autoit]

    #include <GUIConstants.au3>

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

    GUICreate("My GUI with treeview", 350, 215)

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

    $treeview = GUICtrlCreateTreeView(6, 6, 100, 150, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES), $WS_EX_CLIENTEDGE)
    $generalitem = GUICtrlCreateTreeViewItem("General", $treeview)
    GUICtrlSetColor(-1, 0x0000C0)
    $displayitem = GUICtrlCreateTreeViewItem("Display", $treeview)
    GUICtrlSetColor(-1, 0x0000C0)
    $aboutitem = GUICtrlCreateTreeViewItem("About", $generalitem)
    $compitem = GUICtrlCreateTreeViewItem("Computer", $generalitem)
    $useritem = GUICtrlCreateTreeViewItem("User", $generalitem)
    $resitem = GUICtrlCreateTreeViewItem("Resolution", $displayitem)
    $otheritem = GUICtrlCreateTreeViewItem("Other", $displayitem)

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

    $startlabel = GUICtrlCreateLabel("TreeView Demo", 190, 90, 100, 20)
    $aboutlabel = GUICtrlCreateLabel("This little scripts demonstates the using of a treeview-control.", 190, 70, 100, 60)
    GUICtrlSetState(-1, $GUI_HIDE) ; Hides the "aboutlabel"-text during initialization
    $compinfo = GUICtrlCreateLabel("Name:" & @TAB & @ComputerName & @LF & "OS:" & @TAB & @OSVersion & @LF & "SP:" & @TAB & @OSServicePack, 120, 30, 200, 80)
    GUICtrlSetState(-1, $GUI_HIDE) ; Hides the "compinfo"-text during initialization

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

    GUICtrlCreateLabel("", 0, 170, 350, 2, $SS_SUNKEN)
    $togglebutton = GUICtrlCreateButton("&Toggle", 35, 185, 70, 20)
    $infobutton = GUICtrlCreateButton("&Info", 105, 185, 70, 20)
    $statebutton = GUICtrlCreateButton("Col./Exp.", 175, 185, 70, 20)
    $cancelbutton = GUICtrlCreateButton("&Cancel", 245, 185, 70, 20)

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

    GUICtrlSetState($generalitem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) ; Expand the "General"-item and paint in bold
    GUICtrlSetState($displayitem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON)) ; Expand the "Display"-item and paint in bold

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

    GUISetState()
    While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $cancelbutton Or $msg = $GUI_EVENT_CLOSE
    ExitLoop

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

    Case $msg = $togglebutton ; Toggle the bold painting
    If BitAND(GUICtrlRead($generalitem), $GUI_DEFBUTTON) Then
    GUICtrlSetState($generalitem, 0)
    GUICtrlSetState($displayitem, 0)
    Else
    GUICtrlSetState($generalitem, $GUI_DEFBUTTON)
    GUICtrlSetState($displayitem, $GUI_DEFBUTTON)
    EndIf

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

    Case $msg = $infobutton
    #cs
    $item = GUICtrlRead($treeview) ; Get the controlID of the current selected treeview item
    If $item = 0 Then
    MsgBox(64, "TreeView Demo", "No item currently selected")
    Else
    $text = GUICtrlRead($item, 1) ; Get the text of the treeview item
    If $text == "" Then
    MsgBox(16, "Error", "Error while retrieving infos about item")
    Else
    MsgBox(64, "TreeView Demo", "Current item selected is: " & $text) ; $advmsg[0] contains the text and $advmsg[1] the state value of the treeview item
    EndIf
    EndIf
    #ce
    If BitAND(GUICtrlRead($useritem), $GUI_CHECKED) Then MsgBox(64, "Checked", "$useritem")
    If BitAND(GUICtrlRead($compitem), $GUI_CHECKED) Then MsgBox(64, "Checked", "$compitem")

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

    Case $msg = $statebutton
    $item = GUICtrlRead($treeview)
    If $item > 0 Then
    $hItem = GUICtrlGetHandle($item)
    GUICtrlSendMsg($treeview, $TVM_EXPAND, $TVE_TOGGLE, $hItem)
    EndIf

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

    ; The following items will hide the other labels (1st and 2nd parameter) and then show the 'own' labels (3rd and 4th parameter)
    Case $msg = $generalitem
    GUIChangeItems($aboutlabel, $compinfo, $startlabel, $startlabel)

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

    Case $msg = $aboutitem
    GUICtrlSetState($compinfo, $GUI_HIDE)
    GUIChangeItems($startlabel, $startlabel, $aboutlabel, $aboutlabel)

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

    Case $msg = $compitem
    GUIChangeItems($startlabel, $aboutlabel, $compinfo, $compinfo)
    EndSelect
    WEnd

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

    GUIDelete()
    Exit

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

    Func GUIChangeItems($hidestart, $hideend, $showstart, $showend)
    Local $idx

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

    For $idx = $hidestart To $hideend
    GUICtrlSetState($idx, $GUI_HIDE)
    Next
    For $idx = $showstart To $showend
    GUICtrlSetState($idx, $GUI_SHOW)
    Next
    EndFunc ;==>GUIChangeItems

    [/autoit]

    User uns Compinfo checken und dann Info klicken.

    So long,

    Mega

    • Offizieller Beitrag

    Holger hat mal ein schönes Tristate-Treeview erstellt. Das häng ich mal an.
    Damit hatte ich auch schon mal ein Installationsscript begonnen. Ein bischen farbig gestaltet.

    Spoiler anzeigen
    [autoit]

    ; ----------------------------------------------------------------------------
    ;
    ; Script: Tristate TreeView
    ; Version: 0.2
    ; AutoIt Version: 3.1.1.128 Beta min.needed!
    ; Author: Holger Kotsch
    ;
    ; Script Function:
    ; Demonstrates a tristate treeview control -> just more like a fifthstate treeview ;)
    ;
    ; 5 item checkbox! states are usable:
    ; (can only used with TreeView with TVS_CHECKBOXES-style)
    ; - $GUI_CHECKED
    ; - $GUI_UNCHECKED
    ; - $GUI_INDETERMINATE
    ; - $GUI_DISABLE + $GUI_CHECKED
    ; - $GUI_DISABLE + $GUI_UNCHECKED
    ;
    ; ----------------------------------------------------------------------------

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

    #include <GUIConstants.au3>
    #include "TristateTreeViewLib.au3"
    #include <GuiTreeView.au3>
    Opt("GUIOnEventMode", 1)
    ; You could also use a integrated bmw (with ResourceHacker)
    ; Please see TristateTreeViewLib.au3 in line 257 (LoadStateImage)
    ; !!! You must not compiled it full with UPX, just use after compiling: upx.exe --best --compress-resources=0 xyz.exe !!!
    ; If you choose another reource number then 170 you have to change the LoadStateImage() function
    ;
    ; Userfunction My-WM_Notify() is registered in TristateTreeViewLib.au3 !
    ;
    ; You can get other check bitmaps also together with freeware install programs like i.e. NSIS
    ; it must have 5 image states in it:
    ; 1.empty, 2.unchecked, 3.checked, 4.disabled and unchecked, 5.disabled and checked

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

    Global $sStateIconFile = @ScriptDir & "\simple.bmp"
    ;~ Global $sStateIconFile = @ScriptDir & "\modern.bmp"

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

    $gui = GUICreate("Treeview - Install Script", 400, 400, -1, -1, Default, $WS_EX_TOPMOST)
    GUISetOnEvent($GUI_EVENT_CLOSE, "guiClose")
    GUISetBkColor(0xFFD700, $gui)
    GUISetIcon(@WindowsDir & "\system32\shell32.dll", 205)
    $nTV = GUICtrlCreateTreeView(10, 10, 380, 360, BitOr($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_CHECKBOXES), $WS_EX_CLIENTEDGE)
    LoadStateImage($nTV, $sStateIconFile)
    _GUICtrlTreeViewSetBkColor ($nTV, 0x191970)
    _GUICtrlTreeViewSetTextColor($nTV, 0xFFFAF0)
    _GUICtrlTreeViewSetLineColor ($nTV, 0xFFD700)
    $nItem1 = GUICtrlCreateTreeViewItem("Messenger", $nTV)
    GUICtrlSetColor(-1, 0xFF0000)
    $nItem2 = GUICtrlCreateTreeViewItem("Internet", $nTV)
    GUICtrlSetColor(-1, 0xFF00FF)
    $nItem3 = GUICtrlCreateTreeViewItem("Tools", $nTV)
    GUICtrlSetColor(-1, 0xFF8C00)
    $nItem4 = GUICtrlCreateTreeViewItem("Mediaplayer", $nTV)
    GUICtrlSetColor(-1, 0x00BFFF)
    $nSubItem1 = GUICtrlCreateTreeViewItem("InterCafe Client", $nItem1)
    $nSubItem2 = GUICtrlCreateTreeViewItem("ICQ 5.1", $nItem1)
    $nSubItem3 = GUICtrlCreateTreeViewItem("AOL Messenger", $nItem1)
    $nSubItem4 = GUICtrlCreateTreeViewItem("YAHOO Messenger", $nItem1)
    $nSubItem5 = GUICtrlCreateTreeViewItem("WinLive Messenger", $nItem1)
    $nSubItem6 = GUICtrlCreateTreeViewItem("Trillian 3.1", $nItem1)
    $nSubItem7 = GUICtrlCreateTreeViewItem("Skype", $nItem1)
    $nSubItem8 = GUICtrlCreateTreeViewItem("TeamSpeak", $nItem1)
    $nSubItem9 = GUICtrlCreateTreeViewItem("TS Overlay", $nItem1)
    $nSubItem10 = GUICtrlCreateTreeViewItem("Opera", $nItem2)
    $nSubItem11 = GUICtrlCreateTreeViewItem("Mozilla Firefox 2", $nItem2)
    $nSubItem12 = GUICtrlCreateTreeViewItem("NetFramework 2.0", $nItem2)
    $nSubItem13 = GUICtrlCreateTreeViewItem("LimeWire 4.12.0", $nItem2)
    $nSubItem14 = GUICtrlCreateTreeViewItem("Usenext", $nItem2)
    $nSubItem15 = GUICtrlCreateTreeViewItem("Adobe Reader", $nItem3)
    $nSubItem16 = GUICtrlCreateTreeViewItem("WinRar", $nItem3)
    $nSubItem17 = GUICtrlCreateTreeViewItem("GoBack", $nItem3)
    $nSubItem18 = GUICtrlCreateTreeViewItem("Ahead Nero 7.5", $nItem3)
    $nSubItem19 = GUICtrlCreateTreeViewItem("OpenOffice 2.0", $nItem3)
    $nSubItem20 = GUICtrlCreateTreeViewItem("VLC Player", $nItem4)
    $nSubItem21 = GUICtrlCreateTreeViewItem("QuickTime Player", $nItem4)
    $nSubItem22 = GUICtrlCreateTreeViewItem("RealPlayer", $nItem4)
    $nSubItem23 = GUICtrlCreateTreeViewItem("Windows Media Player 11", $nItem4)
    $nSubItem24 = GUICtrlCreateTreeViewItem("FlashPlayer", $nItem4)
    $text1 = "Ausklappen"
    $text2 = "Einklappen"
    $Btn_expand = GUICtrlCreateButton($text1, 30, 375, 90, 20)
    GUICtrlSetOnEvent(-1, "_expand")
    $text3 = "Alle wählen"
    $text4 = "Wahl aufheben"
    $Btn_all = GUICtrlCreateButton($text3, 280, 375, 90, 20)
    GUICtrlSetOnEvent(-1, "_all")
    $Btn_Info = GUICtrlCreateButton("Info zum Programm", 150, 375, 100, 20)
    GUICtrlSetOnEvent(-1, "_info")

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

    ; GUI Info
    $InfoGUI = GUICreate("", 333, 174, -1, -1, $WS_POPUP, $WS_EX_TOPMOST)
    GUISetBkColor(0xFFD700, $InfoGUI)
    $Edit1 = GUICtrlCreateLabel("", 5, 5, 320, 161, $ES_CENTER)
    GUICtrlSetBkColor(-1, 0xFFD700)

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

    GUISetState(@SW_SHOW, $gui)
    $expand = 0
    $switch = 0

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

    While 1
    Sleep(100)
    WEnd

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

    Func _all()
    If $switch = 0 Then
    SelectFull()
    $switch = 1
    GUICtrlSetData($Btn_all, $text4)
    Else
    ReSelect()
    $switch = 0
    GUICtrlSetData($Btn_all, $text3)
    EndIf
    EndFunc

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

    Func _expand()
    If $expand = 0 Then
    Ausklappen()
    $expand = 1
    GUICtrlSetData($Btn_expand, $text2)
    Else
    Einklappen()
    $expand = 0
    GUICtrlSetData($Btn_expand, $text1)
    EndIf
    EndFunc

    Func _info()
    $h_item = GUICtrlSendMsg($nTV, $TVM_GETNEXTITEM, $TVGN_CARET, 0)
    $val = _GUICtrlTreeViewGetText($nTV, $h_item)
    Switch $val
    Case "InterCafe Client"
    GUICtrlSetData($Edit1, $val & @LF & "xhcjvkblnk oüwpqem+vc+" & @LF & " üewq+mvc +w p,mc +wpoidülöe" & @LF & "klfrsdzfghkjl")
    GUISetState(@SW_SHOW, $InfoGUI)
    Sleep(4000)
    GUISetState(@SW_HIDE, $InfoGUI)
    GUICtrlSetData($Edit1, "")
    Case "ICQ 5.1"
    GUICtrlSetData($Edit1, $val & @LF & "5551111" & @LF & " üewq+mvc +w p,mc +wpoidülöe" & @LF & "klfrsdzfghkjl")
    GUISetState(@SW_SHOW, $InfoGUI)
    Sleep(4000)
    GUISetState(@SW_HIDE, $InfoGUI)
    GUICtrlSetData($Edit1, "")
    EndSwitch
    EndFunc

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

    Func guiClose()
    Exit
    EndFunc

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

    Func SelectFull()
    For $i = $nItem1 To $nItem4
    MyCtrlSetItemState($nTV, $i, $GUI_CHECKED)
    Next
    For $i = $nsubItem1 To $nsubItem24
    MyCtrlSetItemState($nTV, $i, $GUI_CHECKED)
    Next
    EndFunc

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

    Func ReSelect()
    For $i = $nItem1 To $nItem4
    MyCtrlSetItemState($nTV, $i, $GUI_UNCHECKED)
    Next
    For $i = $nsubItem1 To $nsubItem24
    MyCtrlSetItemState($nTV, $i, $GUI_UNCHECKED)
    Next
    EndFunc

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

    Func Ausklappen()
    _GUICtrlTreeViewExpand($nTV)
    EndFunc

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

    Func Einklappen()
    _GUICtrlTreeViewExpand($nTV, 0)
    EndFunc

    [/autoit]