Jump to content

How do I read treeview from an array?


gr1fter
 Share

Recommended Posts

I want to create a gui that reads the current user names on the machine and place them in a tree with checkboxes.

With some searching I have found how to create the array to give me the user names, but when i apply it to the treeview, it is not working right. It seems like only 1 item is being read.

Here is my example, can anyone see what I am doing wrong? Thanks

#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiImageList.au3>
#include <WindowsConstants.au3>

Local $hItem[1], $hRandomItem, $hTreeView, $edit, $string,$i
Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)

GUICreate("TreeView Get Checked", 620, 300)

$edit = GUICtrlCreateEdit("", 400, 10, 200)

$hTreeView = GUICtrlCreateTreeView(2, 2, 396, 200, $iStyle, $WS_EX_CLIENTEDGE)
GUISetState()

$Users = _SystemUsers(2)


_GUICtrlTreeView_BeginUpdate($hTreeView)

For $x = 0 To UBound($hItem) - 1
For $i = 1 To $Users[0]

    $hItem[$x] = GUICtrlCreateTreeViewItem(StringFormat($Users[$i]), $hTreeView)
Next
Next
_GUICtrlTreeView_EndUpdate($hTreeView)

; Loop until user exits
Do
    Sleep(500)
    $string = ""
    For $x = 0 To UBound($hItem) -1
        $string &= "Item " & $x & " is checked: " & _GUICtrlTreeView_GetChecked($hTreeView, $hItem[$x]) & @CRLF

    Next
    GUICtrlSetData($edit, $string)
Until GUIGetMsg() = $GUI_EVENT_CLOSE
Exit
GUIDelete()


#cs ===============================================================================
    Function:      _SystemUsers($AccountType = 0)
    Description:   Return an array with the local or domain username
    Parameter(s):  $AccountType: Local, domain or both username
        0 = Local and Domain usernames
        1 = Local usernames only
        2 = Domain usernames only
    Returns:       An array with the list of usernames - Succeeded
        @error 1 - Didn't query any username
        @error 2 - Failed to create Win32_SystemUsers object
        @error 3 - Invalid $AccountType

    Author(s):  Danny35d
#ce ===============================================================================

Func _SystemUsers($AccountType = 0)
    Local $aSystemUsers
    Local $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20
    Local $colItems = "", $strComputer = "localhost"

    If Not StringRegExp($AccountType, '[012]') Then Return SetError(3, 3, '')
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_SystemUsers", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            $Output = StringSplit($objItem.PartComponent, ',')
            If IsArray($Output) Then
                $Temp = StringReplace(StringTrimLeft($Output[2], StringInStr($Output[2], '=', 0, -1)), '"', '')
                If $AccountType = 0 Or ($AccountType = 1 And @ComputerName = $Temp) Then
                    $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|'
                ElseIf $AccountType = 2 And @ComputerName <> $Temp Then
                    $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & '|'
                EndIf
            EndIf
        Next
        $aSystemUsers = StringTrimRight($aSystemUsers, 1)
        If $aSystemUsers = '' Then Return(SetError(1, 1, $aSystemUsers))
        Return(SetError(0, 0, StringSplit($aSystemUsers, '|')))
    Else
        $aSystemUsers = ''
        Return(SetError(2, 2, $aSystemUsers))
    EndIf
EndFunc   ;==>_SystemUsers
Link to comment
Share on other sites

I had luck with a static 6 users (items in the array). So maybe you'll have luck working backwards from here.

** Please update if you figure out a way to select multiples.

#AutoIt3Wrapper_outfile=D:\gui.exe

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <Array.au3>
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>

$Users = _SystemUsers(2)


$Debug_TV = False ; Check ClassName being passed to functions, set to True and use a handle to another control to see it work

Global $hTreeView

$GUI = GUICreate("", 430, 300, -1, -1, -1)
GUISetState (@SW_LOCK)
GUISetBkColor(0xFFFFFF)
GUISetState(@SW_ENABLE)
GUISetState(@SW_SHOW)

$Users = _SystemUsers(2)

$bigbutton = GUICtrlCreateButton("BIG BUTTON", 135, 260, 150, 30, $BS_DEFPUSHBUTTON,$WS_EX_TOPMOST)

Local $GUI, $hItem
Local $iStyle = BitOR( $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)

$hTreeView = _GUICtrlTreeView_Create($GUI, 0, 110, 430, 110, $iStyle)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

_GUICtrlTreeView_BeginUpdate($hTreeView)

$hItem2 = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("All Users"))

$hItem = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("Network Users"))





$read0 = _GUICtrlTreeView_AddChild($hTreeView, $hItem, StringFormat($Array[0]))
$read1 = _GUICtrlTreeView_AddChild($hTreeView, $hItem, StringFormat($Array[1]))
$read2 = _GUICtrlTreeView_AddChild($hTreeView, $hItem, StringFormat($Array[2]))
$read3 = _GUICtrlTreeView_AddChild($hTreeView, $hItem, StringFormat($Array[3]))
$read4 = _GUICtrlTreeView_AddChild($hTreeView, $hItem, StringFormat($Array[4]))
$read5 = _GUICtrlTreeView_AddChild($hTreeView, $hItem, StringFormat($Array[5]))
$read6 = _GUICtrlTreeView_AddChild($hTreeView, $hItem, StringFormat($Array[6]))


_GUICtrlTreeView_EndUpdate($hTreeView)



While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE  
            Exit
        Case $msg= $bigbutton And _GUICtrlTreeView_GetChecked($hTreeView,$hItem2)
            MsgBox (0,"", "YOU have ALL USERS checked")
        Case $msg= $bigbutton And _GUICtrlTreeView_GetChecked($hTreeView, $read0)
            MsgBox (0,"", "YOU have checked " & $array[0])
              Case $msg= $bigbutton And _GUICtrlTreeView_GetChecked($hTreeView, $read1)
            MsgBox (0,"", "YOU have checked " & $array[1])
              Case $msg= $bigbutton And _GUICtrlTreeView_GetChecked($hTreeView, $read2) 
            MsgBox (0,"", "YOU have checked " & $array[2])
                  Case $msg= $bigbutton And _GUICtrlTreeView_GetChecked($hTreeView, $read3) 
            MsgBox (0,"", "YOU have checked " & $array[3])
                  Case $msg= $bigbutton And _GUICtrlTreeView_GetChecked($hTreeView, $read4) 
            MsgBox (0,"", "YOU have checked " & $array[4])
                  Case $msg= $bigbutton And _GUICtrlTreeView_GetChecked($hTreeView, $read5) 
            MsgBox (0,"", "YOU have checked " & $array[5])    
            Case $msg= $bigbutton And _GUICtrlTreeView_GetChecked($hTreeView, $read6) 
            MsgBox (0,"", "YOU have checked " & $array[6])
            
        
        EndSelect
Wend




Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
    $hWndTreeview = $hTreeView
    If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeview
            Switch $iCode
                Case $NM_CLICK ; The user has clicked the left mouse button within the control
                    _DebugPrint("$NM_CLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                            "-->Code:" & @TAB & $iCode)
;~                  Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
                Case $NM_DBLCLK ; The user has double-clicked the left mouse button within the control
                    _DebugPrint("$NM_DBLCLK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                            "-->Code:" & @TAB & $iCode)
;~                  Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
                Case $NM_RCLICK ; The user has clicked the right mouse button within the control
                    _DebugPrint("$NM_RCLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                            "-->Code:" & @TAB & $iCode)
;~                  Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
                Case $NM_RDBLCLK ; The user has clicked the right mouse button within the control
                    _DebugPrint("$NM_RDBLCLK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                            "-->Code:" & @TAB & $iCode)
;~                  Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
                Case $NM_KILLFOCUS ; control has lost the input focus
                    _DebugPrint("$NM_KILLFOCUS" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                            "-->Code:" & @TAB & $iCode)
                    ; No return value
                Case $NM_RETURN ; control has the input focus and that the user has pressed the key
                    _DebugPrint("$NM_RETURN" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                            "-->Code:" & @TAB & $iCode)
;~                  Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
;~              Case $NM_SETCURSOR ; control is setting the cursor in response to a WM_SETCURSOR message
;~                  Local $tinfo = DllStructCreate($tagNMMOUSE, $ilParam)
;~                  $hWndFrom = HWnd(DllStructGetData($tinfo, "hWndFrom"))
;~                  $iIDFrom = DllStructGetData($tinfo, "IDFrom")
;~                  $iCode = DllStructGetData($tinfo, "Code")
;~                  _DebugPrint("$NM_SETCURSOR" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
;~                          "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
;~                          "-->Code:" & @TAB & $iCode & @LF & _
;~                          "-->ItemSpec:" & @TAB & DllStructGetData($tinfo, "ItemSpec") & @LF & _
;~                          "-->ItemData:" & @TAB & DllStructGetData($tinfo, "ItemData") & @LF & _
;~                          "-->X:" & @TAB & DllStructGetData($tinfo, "X") & @LF & _
;~                          "-->Y:" & @TAB & DllStructGetData($tinfo, "Y") & @LF & _
;~                          "-->HitInfo:" & @TAB & DllStructGetData($tinfo, "HitInfo"))
;~                  Return 0 ; to enable the control to set the cursor
;~                  Return 1 ; nonzero to prevent the control from setting the cursor
                Case $NM_SETFOCUS ; control has received the input focus
                    _DebugPrint("$NM_SETFOCUS" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                            "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                            "-->Code:" & @TAB & $iCode)
                    ; No return value
                Case $TVN_BEGINDRAGA, $TVN_BEGINDRAGW
                    _DebugPrint("$TVN_BEGINDRAG")
                Case $TVN_BEGINLABELEDITA, $TVN_BEGINLABELEDITW
                    _DebugPrint("$TVN_BEGINLABELEDIT")
                Case $TVN_BEGINRDRAGA, $TVN_BEGINRDRAGW
                    _DebugPrint("$TVN_BEGINRDRAG")
                Case $TVN_DELETEITEMA, $TVN_DELETEITEMW
                    _DebugPrint("$TVN_DELETEITEM")
                Case $TVN_ENDLABELEDITA, $TVN_ENDLABELEDITW
                    _DebugPrint("$TVN_ENDLABELEDIT")
                Case $TVN_GETDISPINFOA, $TVN_GETDISPINFOW
                    _DebugPrint("$TVN_GETDISPINFO")
                Case $TVN_GETINFOTIPA, $TVN_GETINFOTIPW
                    _DebugPrint("$TVN_GETINFOTIP")
                Case $TVN_ITEMEXPANDEDA, $TVN_ITEMEXPANDEDW
                    _DebugPrint("$TVN_ITEMEXPANDED")
                Case $TVN_ITEMEXPANDINGA, $TVN_ITEMEXPANDINGW
                    _DebugPrint("$TVN_ITEMEXPANDING")
                Case $TVN_KEYDOWN
                    _DebugPrint("$TVN_KEYDOWN")
                Case $TVN_SELCHANGEDA, $TVN_SELCHANGEDW
                    _DebugPrint("$TVN_SELCHANGED")
                Case $TVN_SELCHANGINGA, $TVN_SELCHANGINGW
                    _DebugPrint("$TVN_SELCHANGING")
                Case $TVN_SETDISPINFOA, $TVN_SETDISPINFOW
                    _DebugPrint("$TVN_SETDISPINFO")
                Case $TVN_SINGLEEXPAND
                    _DebugPrint("$TVN_SINGLEEXPAND")
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _DebugPrint($s_text, $line = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @LF & _
            "+======================================================" & @LF & _
            "-->Line(" & StringFormat("%04d", $line) & "):" & @TAB & $s_text & @LF & _
            "+======================================================" & @LF)
EndFunc   ;==>_DebugPrint



#cs ===============================================================================
    Function:      _SystemUsers($AccountType = 0)
    Description:   Return an array with the local or domain username
    Parameter(s):  $AccountType: Local, domain or both username
        0 = Local and Domain usernames
        1 = Local usernames only
        2 = Domain usernames only
    Returns:       An array with the list of usernames - Succeeded
        @error 1 - Didn't query any username
        @error 2 - Failed to create Win32_SystemUsers object
        @error 3 - Invalid $AccountType

    Author(s):  Danny35d
#ce ===============================================================================




Func _SystemUsers($AccountType = 0)
    Local $aSystemUsers
    Local $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20
    Local $colItems = "", $strComputer = "localhost"
    
    

    If Not StringRegExp($AccountType, '[012]') Then Return SetError(3, 3, '')
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_SystemUsers", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)



;~ IF IsObj($colItems) = 1 Then
        For $objItem In $colItems
            $Output = StringSplit($objItem.PartComponent, ',')
            
;~          $asystemusers = ''
            
;~             If IsArray($Output) Then
;~              _ArrayDisplay ($output)
;~              sleep (10000)
                $Temp = StringReplace(StringTrimLeft($Output[2], StringInStr($Output[2], '=', 0, -1)), '"', '')
                
                
    
;~                 If $AccountType = 2 Or $AccountType = 0 Or $AccountType = 1  Then
                    $aSystemUsers &= StringReplace(StringTrimLeft($Output[1], StringInStr($Output[1], '=', 0, -1)), '"', '') & ","
;~                 ElseIf $AccountType = 1  And @ComputerName <> $Temp Then

next

;~ msgbox (0,'',$asystemusers)

Global $Array = stringsplit ($aSystemUsers , "," , 2)


;~ ExitLoop
;~ endif
;~ endif


EndFunc   ;==>_SystemUsers

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

The buttons do work, but I was really hoping to stay away from static entries for the reason that I have no idea how many users there could be on a giving machine. 1 machine could have 2 users, another could have 15. So i would like it to dynamically adjust.

Maybe I am going about it the wrong way completely.

I just want to be able to do the following in the GUI:

List the current user names

User can select one or multiple user names

then run a command based off of what they choose

I thought treeview would be the way to go, but it seems too complicated for my skill level. So i guess the question now is....am i going the right route or should i be looking into something different?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...