Sign in to follow this  
Followers 0
storme

TreeView - interaction with other controls

8 posts in this topic

G'day all

Thanks for any help you can offer.

I'm trying to set up a treeview that will interact with other controls.

To be more precise I want my program to :

  • Feed main tree from an array for easy updates - DONE
  • Link to tree items from array for later processing - DONE
  • Link from tree item to array - DONE (using ItemParam)
  • De/Select top level branch de/selects items connected to it - DONE
  • Clicking an item sets an external edit box with information for that item - DONE
  • Clicking an item sets the tasks tree view - DONE (DEMO only)
  • Save selections in $tvTasks tree view - ?? don't have any idea??
  • Selecting an item by cursor control sets external edit box - ?? don't have any idea??
  • Run tasks when RUN ALL button is pressed - Partial demo code

I've been doing a lot of searching on the forum and a few hours of playing myself and this is what I've come up with so far.

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
;Opt("GUIOnEventMode", 1)
Local $frmMain = GUICreate("Health and fitness for your PC", 910, 568, 194, 128)
Local $tvFuncSelect = GUICtrlCreateTreeView(8, 8, 305, 497, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES, $WS_GROUP, $WS_TABSTOP, $WS_BORDER))
Local $tvTasks = GUICtrlCreateTreeView(320, 8, 105, 81, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES, $WS_GROUP, $WS_TABSTOP, $WS_BORDER))
Local $btnRunSingleTask = GUICtrlCreateButton("Run this task", 320, 96, 105, 33, $WS_GROUP)
GUICtrlSetFont($btnRunSingleTask, 10, 800, 0, "MS Sans Serif")
GUICtrlSetTip($btnRunSingleTask, "Run the task ONLY")

Local $btnRunAll = GUICtrlCreateButton("Run All Tasks", 800, 512, 105, 25, $WS_GROUP)
GUICtrlSetFont($btnRunAll, 10, 800, 0, "MS Sans Serif")
GUICtrlSetTip($btnRunAll, "Run All selected tasks")

Local $btnSaveSelected = GUICtrlCreateButton("Save Selected", 200, 512, 113, 25, $WS_GROUP)
GUICtrlSetFont($btnSaveSelected, 10, 800, 0, "MS Sans Serif")

Local $btnLoadSelected = GUICtrlCreateButton("Load Selected", 8, 512, 113, 25, $WS_GROUP)
GUICtrlSetFont($btnLoadSelected, 10, 800, 0, "MS Sans Serif")

Local $edtDescription = GUICtrlCreateEdit("", 432, 8, 473, 121, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN))
GUICtrlSetData($edtDescription, "Task Description")

Local $btnClearSelection = GUICtrlCreateButton("Clear all", 128, 512, 65, 25, $WS_GROUP)
GUICtrlSetFont($btnClearSelection, 10, 800, 0, "MS Sans Serif")

Global $hTree = GUICtrlGetHandle($tvFuncSelect) ; Used by _WM_NOTIFY function
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

Global $functionAray[5][6] = [ _
        ["Restore", "", "Windows registry restore tasks", "", "", ""], _
        ["Restore,Create restore Point", "", "Use windows restore to create recovery point", "createRestorePointOSCheck()", "createRestorePointFunctions()", "createRestorePointState()"], _
        ["Defrag", "", "Disk defragmenter tasks", "", "", ""], _
        ["Defrag,JKDefrag", "", "Disk defragmenter and optimizer for Windows", "JKDefragOSCheck()", "JKDefragFunctions()", "JKDefragState()"], _
        ["Defrag,Windows Defrag", "", "Built in disk defragmenter and optimizer for Windows", "DefragOSCheck()", "DefragFunctions()", "DefragState()"] _
        ]
loadFunctionTree($tvFuncSelect) ; Load Function  treeview

GUISetState(@SW_SHOW)


While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $tvFuncSelect
            ConsoleWrite("msg = " & $msg & @CR)

        Case $msg = $btnRunAll
            ;RUN ALL
            RunAllTasks()

    EndSelect
    Sleep(10)
WEnd

Func loadFunctionTree($tvFuncSelect)
    ; [0]Menu item - main,secondary,etc,this items name
    ; [1]Link to menu item GUICtrlCreateTreeViewItem(
    ; [2]Description of item
    ; [3]Compatible OSVersion/OSArch check function eg JKDefragOSCheck() Could be set to TRUE for ALL OS
    ; [4]Function used to fill "$tvTasks" if given a parapeter of "ALL" it will return an array of tasks (eg run, update, etc)
    ;        If given a task name eg RUN it will return the function to perform it.
    ; [5]check/set the state of the item eg JKDefragState()
    ;       Used to "autoset" functions that can/can't run
    _ArraySort($functionAray)
    _GUICtrlTreeView_BeginUpdate($tvFuncSelect)
    For $item = 0 To UBound($functionAray) - 1
        $functionAray[$item][1] = GUICtrlCreateTreeViewItem($functionAray[$item][0], $tvFuncSelect)
        _GUICtrlTreeView_SetItemParam($tvFuncSelect, $functionAray[$item][1], $item) ; set array position for future reference
    Next
    _GUICtrlTreeView_EndUpdate($tvFuncSelect)
EndFunc   ;==>loadFunctionTree

;-------------------------------------------------------------------------------------------------------------
Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    Local $iPos, $iX, $iY, $tPt, $iHitTest, $fWasChecked
    Local $hItem

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hTree
            Switch $iCode
                Case $NM_CLICK
                    $iPos = _WinAPI_GetMessagePos()
                    $iX = BitAND($iPos, 0xFFFF)
                    $iY = BitShift($iPos, 16)
                    $tPt = DllStructCreate($tagPOINT)
                    DllStructSetData($tPt, "X", $iX)
                    DllStructSetData($tPt, "Y", $iY)
                    _WinAPI_ScreenToClient($hTree, $tPt)
                    $iX = DllStructGetData($tPt, "X")
                    $iY = DllStructGetData($tPt, "Y")

                    $tTVHTInfo = _GUICtrlTreeView_HitTestEx($hTree, $iX, $iY)
                    $tvFlag = DllStructGetData($tTVHTInfo, "Flags")
                    ConsoleWrite("." & $tvFlag & " " & _GetHitString($tvFlag) & ".")
                    Switch $tvFlag
                        Case $TVHT_ONITEMSTATEICON
                            $hItem = DllStructGetData($tTVHTInfo, "Item")

                            $fWasChecked = _GUICtrlTreeView_GetChecked($hTree, $hItem)

                            If $fWasChecked Then
                                _GUICtrlTreeView_BeginUpdate($hTree)
                                _Uncheck($hItem)
                                _GUICtrlTreeView_EndUpdate($hTree)
                            Else
                                _GUICtrlTreeView_BeginUpdate($hTree)
                                _Check($hItem)
                                _GUICtrlTreeView_EndUpdate($hTree)
                            EndIf
                            ConsoleWrite("_")
                            _CallItemFunc($hItem)
                        Case $TVHT_ONITEMLABEL, $TVHT_ONITEMSTATEICON, $TVHT_ONITEMBUTTON
                            $hItem = DllStructGetData($tTVHTInfo, "Item")
                            ConsoleWrite("-")
                            _CallItemFunc($hItem)

                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Func _GetHitString($iFlags)
    ;Local $iFlags = DllStructGetData($tHitTest, "Flags")
    Select
        Case BitAND($iFlags, $TVHT_NOWHERE) <> 0
            Return "In the client area, but below the last item"
        Case BitAND($iFlags, $TVHT_ONITEMICON) <> 0
            Return "On the bitmap associated with an item"
        Case BitAND($iFlags, $TVHT_ONITEMLABEL) <> 0
            Return "On the text associated with an item"
        Case BitAND($iFlags, $TVHT_ONITEMINDENT) <> 0
            Return "In the indentation associated with an item"
        Case BitAND($iFlags, $TVHT_ONITEMBUTTON) <> 0
            Return "On the button associated with an item"
        Case BitAND($iFlags, $TVHT_ONITEMRIGHT) <> 0
            Return "In the area to the right of an item"
        Case BitAND($iFlags, $TVHT_ONITEMSTATEICON) <> 0
            Return "On the state icon for a item that is in a user-defined state"
        Case BitAND($iFlags, $TVHT_ABOVE) <> 0
            Return "Above the client area"
        Case BitAND($iFlags, $TVHT_BELOW) <> 0
            Return "Below the client area"
        Case BitAND($iFlags, $TVHT_TORIGHT) <> 0
            Return "To the left of the client area"
        Case BitAND($iFlags, $TVHT_TOLEFT) <> 0
            Return "To the right of the client area"
    EndSelect
EndFunc   ;==>_GetHitString

Func _Uncheck($hItem)
    Local $hParent, $hItemCur, $iLevel

    $iLevel = _GUICtrlTreeView_Level($hTree, $hItem)
    $hItemCur = _GUICtrlTreeView_GetFirstChild($hTree, $hItem)
    While $hItemCur And _GUICtrlTreeView_Level($hTree, $hItemCur) > $iLevel
        _GUICtrlTreeView_SetChecked($hTree, $hItemCur, False)
        $hItemCur = _GUICtrlTreeView_GetNext($hTree, $hItemCur)
    WEnd

    $hParent = _GUICtrlTreeView_GetParentHandle($hTree, $hItem)
    If $hParent Then
        While $hParent
            If $hParent <> $hItem Then _GUICtrlTreeView_SetChecked($hTree, $hParent, False)
            $hParent = _GUICtrlTreeView_GetParentHandle($hTree, $hParent)
        WEnd
    EndIf
EndFunc   ;==>_Uncheck

Func _Check($hItem)
    Local $hParent, $hItemCur, $iLevel

    $iLevel = _GUICtrlTreeView_Level($hTree, $hItem)
    $hItemCur = _GUICtrlTreeView_GetFirstChild($hTree, $hItem)
    While $hItemCur And _GUICtrlTreeView_Level($hTree, $hItemCur) > $iLevel
        _GUICtrlTreeView_SetChecked($hTree, $hItemCur)
        $hItemCur = _GUICtrlTreeView_GetNext($hTree, $hItemCur)
    WEnd

    $hParent = _GUICtrlTreeView_GetParentHandle($hTree, $hItem)
    While $hParent
        $hItemCur = _GUICtrlTreeView_GetFirstChild($hTree, $hParent)
        While $hItemCur
            If Not _GUICtrlTreeView_GetChecked($hTree, $hItemCur) And $hItemCur <> $hItem Then ExitLoop 2
            $hItemCur = _GUICtrlTreeView_GetNextSibling($hTree, $hItemCur)
        WEnd

        _GUICtrlTreeView_SetChecked($hTree, $hParent)
        $hParent = _GUICtrlTreeView_GetParentHandle($hTree, $hParent)
    WEnd
EndFunc   ;==>_Check

Func _WinAPI_GetMessagePos()
    Local $aResult = DllCall("user32.dll", "uint", "GetMessagePos")

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

Func _CallItemFunc($hItem)
    ConsoleWrite(@CR & _GUICtrlTreeView_GetText($hTree, $hItem) & "__" & _GUICtrlTreeView_GetItemParam($hTree, $hItem) & @CR)
    GUICtrlSetData($edtDescription, $functionAray[_GUICtrlTreeView_GetItemParam($hTree, $hItem)][2])

    ; set tasks that element can perform
    _GUICtrlTreeView_BeginUpdate($tvTasks)
    _GUICtrlTreeView_DeleteAll($tvTasks)
    If $functionAray[_GUICtrlTreeView_GetItemParam($hTree, $hItem)][4] <> "" Then
        ; just demo code to give an idea of what it will look like
        GUICtrlCreateTreeViewItem("Install", $tvTasks)
        GUICtrlCreateTreeViewItem("run", $tvTasks)
        GUICtrlCreateTreeViewItem("update", $tvTasks)
        GUICtrlCreateTreeViewItem("uninstall", $tvTasks)

    EndIf
    _GUICtrlTreeView_EndUpdate($tvTasks)

EndFunc   ;==>_CallItemFunc

Func RunAlltasks()
    ConsoleWrite("|" & @cr)
    For $item = 0 To UBound($functionAray) - 1
        ;test if item is checked in tree view and is a runnable item
        If _GUICtrlTreeView_GetChecked($tvFuncSelect, $functionAray[$item][1]) And $functionAray[$item][4] <> "" Then
            ;TODO : read tasks tree items to work out what needs to be run
            ConsoleWrite(">" & $functionAray[$item][0] & "<" & @cr)
        EndIf
    Next
EndFunc   ;==>RunAllSasks

Any help with items I can't get working would be great. Any advice on the code/approach would also be welcomed.

Thanks again for your help.

John Morrison

aka

Storm-E

Share this post


Link to post
Share on other sites



Updated Code

I've added code to make a real tree from the array and a few other touches.

  • Feed main tree from an array for easy updates - DONE
  • Link to tree items from array for later processing - DONE
  • Link from tree item to array - DONE (using ItemParam)
  • De/Select top level branch de/selects items connected to it - DONE
  • Clicking an item sets an external edit box with information for that item - DONE
  • Clicking an item sets the tasks tree view - DONE (DEMO only)
  • Save selections in $tvTasks tree view - ?? don't have any idea??
  • Selecting an item by cursor control sets external edit box - ?? don't have any idea??
  • Run tasks when RUN ALL button is pressed - Partial demo code
  • Copy array items as a real tree view - DONE
  • Clear ALL - DONE

Any help with the BOLD items would be appreacated.

I'm a novice on treeview so if there is an easier way of doing what I've done please let me know.

Updated Code :

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
;Opt("GUIOnEventMode", 1)
Local $frmMain = GUICreate("Health and fitness for your PC", 910, 568, 194, 128)
Local $tvFuncSelect = GUICtrlCreateTreeView(8, 8, 305, 497, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES, $WS_GROUP, $WS_TABSTOP, $WS_BORDER))
Local $tvTasks = GUICtrlCreateTreeView(320, 8, 105, 81, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES, $WS_GROUP, $WS_TABSTOP, $WS_BORDER))
Local $btnRunSingleTask = GUICtrlCreateButton("Run this task", 320, 96, 105, 33, $WS_GROUP)
GUICtrlSetFont($btnRunSingleTask, 10, 800, 0, "MS Sans Serif")
GUICtrlSetTip($btnRunSingleTask, "Run the task ONLY")

Local $btnRunAll = GUICtrlCreateButton("Run All Tasks", 800, 512, 105, 25, $WS_GROUP)
GUICtrlSetFont($btnRunAll, 10, 800, 0, "MS Sans Serif")
GUICtrlSetTip($btnRunAll, "Run All selected tasks")

Local $btnSaveSelected = GUICtrlCreateButton("Save Selected", 200, 512, 113, 25, $WS_GROUP)
GUICtrlSetFont($btnSaveSelected, 10, 800, 0, "MS Sans Serif")

Local $btnLoadSelected = GUICtrlCreateButton("Load Selected", 8, 512, 113, 25, $WS_GROUP)
GUICtrlSetFont($btnLoadSelected, 10, 800, 0, "MS Sans Serif")

Local $edtDescription = GUICtrlCreateEdit("", 432, 8, 473, 121, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN))
GUICtrlSetData($edtDescription, "Task Description")

Local $btnClearSelection = GUICtrlCreateButton("Clear all", 128, 512, 65, 25, $WS_GROUP)
GUICtrlSetFont($btnClearSelection, 10, 800, 0, "MS Sans Serif")

Global $hTree = GUICtrlGetHandle($tvFuncSelect) ; Used by _WM_NOTIFY function
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")

Global $functionArray[5][6] = [ _
        ["Restore", "", "Windows registry restore tasks", "", "", ""], _
        ["Restore,Create restore Point", "", "Use windows restore to create recovery point", "createRestorePointOSCheck()", "createRestorePointFunctions()", "createRestorePointState()"], _
        ["Defrag", "", "Disk defragmenter tasks", "", "", ""], _
        ["Defrag,JKDefrag", "", "Disk defragmenter and optimizer for Windows", "JKDefragOSCheck()", "JKDefragFunctions()", "JKDefragState()"], _
        ["Defrag,Windows Defrag", "", "Built in disk defragmenter and optimizer for Windows", "DefragOSCheck()", "DefragFunctions()", "DefragState()"] _
        ]
loadFunctionTree($tvFuncSelect) ; Load Function  treeview

GUISetState(@SW_SHOW)


While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $tvFuncSelect
            ConsoleWrite("msg = " & $msg & @CR)

        Case $btnRunAll
            ;RUN ALL
            RunAllTasks()
        Case $btnClearSelection
            ;Clear allselections
            _GUICtrlTreeView_BeginUpdate($tvFuncSelect)
            For $item = 0 To UBound($functionArray) - 1
                _GUICtrlTreeView_SetChecked($hTree, GUICtrlGetHandle($functionArray[$item][1]), False)
            Next
            _GUICtrlTreeView_EndUpdate($tvFuncSelect)
    EndSwitch
    Sleep(10)
WEnd

Func loadFunctionTree($tvFuncSelect)
    ; [0]Menu item - main,secondary,etc,this items name
    ; [1]Link to menu item GUICtrlCreateTreeViewItem(
    ; [2]Description of item
    ; [3]Compatible OSVersion/OSArch check function eg JKDefragOSCheck() Could be set to TRUE for ALL OS
    ; [4]Function used to fill "$tvTasks" if given a parapeter of "ALL" it will return an array of tasks (eg run, update, etc)
    ;        If given a task name eg RUN it will return the function to perform it.
    ; [5]check/set the state of the item eg JKDefragState()
    ;       Used to "autoset" functions that can/can't run
    _ArraySort($functionArray)
    _GUICtrlTreeView_BeginUpdate($tvFuncSelect)
    For $item = 0 To UBound($functionArray) - 1
        $functionArray[$item][1] = GUICtrlCreateTreeViewItem(GetItemText($functionArray, $item), FindTreeParent($functionArray, $item, $tvFuncSelect))
        _GUICtrlTreeView_SetItemParam($tvFuncSelect, $functionArray[$item][1], $item) ; set array index for back reference
    Next
    _GUICtrlTreeView_Expand($tvFuncSelect)
    _GUICtrlTreeView_EndUpdate($tvFuncSelect)
EndFunc   ;==>loadFunctionTree

Func FindTreeParent($functionArray, $item, $tvFuncSelect)
    If $item = 0 Then Return $tvFuncSelect

    Local $searchText = StringInStr($functionArray[$item][0], ",", -1) ; Find last occurence of ","
    If $searchText = 0 Then
        ; No commas so assume it's a top level item
        Return $tvFuncSelect
    Else
        ; extract parent part for search
        $searchText = StringLeft($functionArray[$item][0], $searchText - 1)
    EndIf

    Local $ParentIndex = _ArraySearch($functionArray, $searchText, 0, $item - 1)
    If Not @error Then
        Return $functionArray[$ParentIndex][1] ; parent index
    Else
        Return $tvFuncSelect
    EndIf
EndFunc   ;==>FindTreeParent

Func GetItemText($functionArray, $item)
    If $item = 0 Then Return $functionArray[$item][0]

    Local $searchText = StringInStr($functionArray[$item][0], ",", -1) ; Find last occurence of ","
    If $searchText = 0 Then
        ; No commas so assume it's a top level item
        Return $functionArray[$item][0]
    Else
        ; extract parent part for search
        Return StringMid($functionArray[$item][0], $searchText + 1, 100)
    EndIf
EndFunc   ;==>GetItemText


;-------------------------------------------------------------------------------------------------------------
Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    Local $iPos, $iX, $iY, $tPt, $iHitTest, $fWasChecked
    Local $hItem

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hTree
            Switch $iCode
                Case $NM_CLICK
                    $iPos = _WinAPI_GetMessagePos()
                    $iX = BitAND($iPos, 0xFFFF)
                    $iY = BitShift($iPos, 16)
                    $tPt = DllStructCreate($tagPOINT)
                    DllStructSetData($tPt, "X", $iX)
                    DllStructSetData($tPt, "Y", $iY)
                    _WinAPI_ScreenToClient($hTree, $tPt)
                    $iX = DllStructGetData($tPt, "X")
                    $iY = DllStructGetData($tPt, "Y")

                    $tTVHTInfo = _GUICtrlTreeView_HitTestEx($hTree, $iX, $iY)
                    $tvFlag = DllStructGetData($tTVHTInfo, "Flags")
                    ConsoleWrite("." & $tvFlag & " " & _GetHitString($tvFlag) & ".")
                    Switch $tvFlag
                        Case $TVHT_ONITEMSTATEICON
                            $hItem = DllStructGetData($tTVHTInfo, "Item")

                            $fWasChecked = _GUICtrlTreeView_GetChecked($hTree, $hItem)

                            If $fWasChecked Then
                                _GUICtrlTreeView_BeginUpdate($hTree)
                                _Uncheck($hItem)
                                _GUICtrlTreeView_EndUpdate($hTree)
                            Else
                                _GUICtrlTreeView_BeginUpdate($hTree)
                                _Check($hItem)
                                _GUICtrlTreeView_EndUpdate($hTree)
                            EndIf
                            ConsoleWrite("_")
                            _CallItemFunc($hItem)
                        Case $TVHT_ONITEMLABEL, $TVHT_ONITEMSTATEICON, $TVHT_ONITEMBUTTON
                            $hItem = DllStructGetData($tTVHTInfo, "Item")
                            ConsoleWrite("-")
                            _CallItemFunc($hItem)

                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Func _GetHitString($iFlags)
    ;Local $iFlags = DllStructGetData($tHitTest, "Flags")
    Select
        Case BitAND($iFlags, $TVHT_NOWHERE) <> 0
            Return "In the client area, but below the last item"
        Case BitAND($iFlags, $TVHT_ONITEMICON) <> 0
            Return "On the bitmap associated with an item"
        Case BitAND($iFlags, $TVHT_ONITEMLABEL) <> 0
            Return "On the text associated with an item"
        Case BitAND($iFlags, $TVHT_ONITEMINDENT) <> 0
            Return "In the indentation associated with an item"
        Case BitAND($iFlags, $TVHT_ONITEMBUTTON) <> 0
            Return "On the button associated with an item"
        Case BitAND($iFlags, $TVHT_ONITEMRIGHT) <> 0
            Return "In the area to the right of an item"
        Case BitAND($iFlags, $TVHT_ONITEMSTATEICON) <> 0
            Return "On the state icon for a item that is in a user-defined state"
        Case BitAND($iFlags, $TVHT_ABOVE) <> 0
            Return "Above the client area"
        Case BitAND($iFlags, $TVHT_BELOW) <> 0
            Return "Below the client area"
        Case BitAND($iFlags, $TVHT_TORIGHT) <> 0
            Return "To the left of the client area"
        Case BitAND($iFlags, $TVHT_TOLEFT) <> 0
            Return "To the right of the client area"
    EndSelect
EndFunc   ;==>_GetHitString

Func _Uncheck($hItem)
    Local $hParent, $hItemCur, $iLevel

    $iLevel = _GUICtrlTreeView_Level($hTree, $hItem)
    $hItemCur = _GUICtrlTreeView_GetFirstChild($hTree, $hItem)
    While $hItemCur And _GUICtrlTreeView_Level($hTree, $hItemCur) > $iLevel
        _GUICtrlTreeView_SetChecked($hTree, $hItemCur, False)
        $hItemCur = _GUICtrlTreeView_GetNext($hTree, $hItemCur)
    WEnd

    $hParent = _GUICtrlTreeView_GetParentHandle($hTree, $hItem)
    If $hParent Then
        While $hParent
            If $hParent <> $hItem Then _GUICtrlTreeView_SetChecked($hTree, $hParent, False)
            $hParent = _GUICtrlTreeView_GetParentHandle($hTree, $hParent)
        WEnd
    EndIf
EndFunc   ;==>_Uncheck

Func _Check($hItem)
    Local $hParent, $hItemCur, $iLevel

    $iLevel = _GUICtrlTreeView_Level($hTree, $hItem)
    $hItemCur = _GUICtrlTreeView_GetFirstChild($hTree, $hItem)
    While $hItemCur And _GUICtrlTreeView_Level($hTree, $hItemCur) > $iLevel
        _GUICtrlTreeView_SetChecked($hTree, $hItemCur)
        $hItemCur = _GUICtrlTreeView_GetNext($hTree, $hItemCur)
    WEnd

    $hParent = _GUICtrlTreeView_GetParentHandle($hTree, $hItem)
    While $hParent
        $hItemCur = _GUICtrlTreeView_GetFirstChild($hTree, $hParent)
        While $hItemCur
            If Not _GUICtrlTreeView_GetChecked($hTree, $hItemCur) And $hItemCur <> $hItem Then ExitLoop 2
            $hItemCur = _GUICtrlTreeView_GetNextSibling($hTree, $hItemCur)
        WEnd

        _GUICtrlTreeView_SetChecked($hTree, $hParent)
        $hParent = _GUICtrlTreeView_GetParentHandle($hTree, $hParent)
    WEnd
EndFunc   ;==>_Check

Func _WinAPI_GetMessagePos()
    Local $aResult = DllCall("user32.dll", "uint", "GetMessagePos")

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

Func _CallItemFunc($hItem)
    ConsoleWrite(@CR & _GUICtrlTreeView_GetText($hTree, $hItem) & "__" & _GUICtrlTreeView_GetItemParam($hTree, $hItem) & @CR)
    GUICtrlSetData($edtDescription, $functionArray[_GUICtrlTreeView_GetItemParam($hTree, $hItem)][2])

    ; set tasks that element can perform
    _GUICtrlTreeView_BeginUpdate($tvTasks)
    _GUICtrlTreeView_DeleteAll($tvTasks)
    If $functionArray[_GUICtrlTreeView_GetItemParam($hTree, $hItem)][4] <> "" Then
        ; just demo code to give an idea of what it will look like
        GUICtrlCreateTreeViewItem("Install", $tvTasks)
        GUICtrlCreateTreeViewItem("run", $tvTasks)
        GUICtrlCreateTreeViewItem("update", $tvTasks)
        GUICtrlCreateTreeViewItem("uninstall", $tvTasks)

    EndIf
    _GUICtrlTreeView_EndUpdate($tvTasks)

EndFunc   ;==>_CallItemFunc

Func RunAlltasks()
    ConsoleWrite("|" & @CR)
    For $item = 0 To UBound($functionArray) - 1
        ;test if item is checked in tree view and is a runnable item
        If _GUICtrlTreeView_GetChecked($tvFuncSelect, $functionArray[$item][1]) And $functionArray[$item][4] <> "" Then
            ;TODO : read tasks tree items to work out what needs to be run
            ConsoleWrite(">" & $functionArray[$item][0] & "<" & @CR)
        EndIf
    Next
EndFunc   ;==>RunAlltasks

If you see some of your code in there let me know and I'll add credit to you.

Hope everyone is having a great weekend!

John Morrison

Share this post


Link to post
Share on other sites

[*]Save selections in $tvTasks tree view - ?? don't have any idea??

Register a function with OnAutoItExitRegister() which loops through the treeview utilizing a combo of _GUICtrlTreeView_GetFirstItem/_GUICtrlTreeView_GetNext & _GUICtrlTreeView_GetChildCount/_GUICtrlTreeView_GetFirstChild/_GUICtrlTreeView_GetNextChild, saving the states (_GUICtrlTreeView_GetState) to special treeview section in an .ini file (using _GUICtrlTreeView_GetText to define the key-name?).

After you created the treeview, check if the .ini file exits, and if so loop through it with IniReadSection and check the items in the treeview respectively.

[*]Selecting an item by cursor control sets external edit box - ?? don't have any idea??

You mean checkbox? Capture the state change of chose in a WM_COMMAND function and set the treeview item state respectively.

Share this post


Link to post
Share on other sites

Register a function with OnAutoItExitRegister() which loops through the treeview utilizing a combo of _GUICtrlTreeView_GetFirstItem/_GUICtrlTreeView_GetNext & _GUICtrlTreeView_GetChildCount/_GUICtrlTreeView_GetFirstChild/_GUICtrlTreeView_GetNextChild, saving the states (_GUICtrlTreeView_GetState) to special treeview section in an .ini file (using _GUICtrlTreeView_GetText to define the key-name?).

After you created the treeview, check if the .ini file exits, and if so loop through it with IniReadSection and check the items in the treeview respectively.

You mean checkbox? Capture the state change of chose in a WM_COMMAND function and set the treeview item state respectively.

Thanks for the reply KaFu

What I ment by "saving" was "remembering" the status of the check boxes in the "task" treeview when I changed to a different entry in the "function" treeview.

I eventually solved it by using a "static" array.

If you run the script you'll find that when you click an item in the first treeview (functions) it updates the description and the second treeview (tasks).

If you click the checkboxes in the "tasks" treeview and change to another item in the function treeview and back again you'll find the checkbox states in the tasks treeview have been maintained.

I've done all that with _WM_NOTIFY for mouse controls but I'm not sure how to achieve the same thing if I use the keyboard (cursor, space) to move and set checkboxes.

Anyway here is the latest verison of the program.

;#AutoIt3Wrapper_Run_Debug_Mode=y
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
;Opt("GUIOnEventMode", 1)
Local $frmMain = GUICreate("Health and fitness for your PC", 910, 568, 194, 128)
Local $tvFuncSelect = GUICtrlCreateTreeView(8, 8, 305, 497, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES, $WS_GROUP, $WS_TABSTOP, $WS_BORDER))
Local $tvTasks = GUICtrlCreateTreeView(320, 8, 105, 81, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES, $WS_GROUP, $WS_TABSTOP, $WS_BORDER))
Local $btnRunSingleTask = GUICtrlCreateButton("Run this task", 320, 96, 105, 33, $WS_GROUP)
GUICtrlSetFont($btnRunSingleTask, 10, 800, 0, "MS Sans Serif")
GUICtrlSetTip($btnRunSingleTask, "Run the task ONLY")

Local $btnRunAll = GUICtrlCreateButton("Run All Tasks", 800, 512, 105, 25, $WS_GROUP)
GUICtrlSetFont($btnRunAll, 10, 800, 0, "MS Sans Serif")
GUICtrlSetTip($btnRunAll, "Run All selected tasks")

Local $btnSaveSelected = GUICtrlCreateButton("Save Selected", 200, 512, 113, 25, $WS_GROUP)
GUICtrlSetFont($btnSaveSelected, 10, 800, 0, "MS Sans Serif")

Local $btnLoadSelected = GUICtrlCreateButton("Load Selected", 8, 512, 113, 25, $WS_GROUP)
GUICtrlSetFont($btnLoadSelected, 10, 800, 0, "MS Sans Serif")

Local $edtDescription = GUICtrlCreateEdit("", 432, 8, 473, 121, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN))
GUICtrlSetData($edtDescription, "Task Description")

Local $btnClearSelection = GUICtrlCreateButton("Clear all", 128, 512, 65, 25, $WS_GROUP)
GUICtrlSetFont($btnClearSelection, 10, 800, 0, "MS Sans Serif")

Global $hFuncTree = GUICtrlGetHandle($tvFuncSelect) ; Used by _WM_NOTIFY function
Global $hTaskTree = GUICtrlGetHandle($tvTasks) ; Used by _WM_NOTIFY function
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
;GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

Global $hCurrentItemFuncTree ; used to save task states from each function

Global $functionArray[7][6] = [ _
        ["Restore", "", "Windows registry restore tasks", "", "", ""], _
        ["Restore,Create restore Point", "", "Use windows restore to create recovery point", "createRestorePointOSCheck()", "createRestorePointFunctions(", "createRestorePointExecute("], _
        ["Defrag", "", "Disk defragmenter tasks", "", "", ""], _
        ["Defrag,JKDefrag", "", "Disk defragmenter and optimizer for Windows", "JKDefragOSCheck()", "JKDefragFunctions(", "JKDefragExecute("], _
        ["Defrag,Windows Defrag", "", "Built in disk defragmenter and optimizer for Windows", "DefragOSCheck()", "DefragFunctions(", "DefragExecute("], _
        ["Twicks", "", "Tweaks for windows", "", "", ""], _
        ["Twicks,BSOD Disable", "", "Disable automatic restart on BSOD", "BSBOdisableOSCheck()", "BSBOdisableFunctions(", "BSBOdisableExecute("] _
        ]
loadFunctionTree($tvFuncSelect) ; Load Function  treeview

GUISetState(@SW_SHOW)


While 1
    $msg = GUIGetMsg()
    ;If $msg <> 0 Then ConsoleWrite("msg = " & $msg & @CR)
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $tvFuncSelect
            ;ConsoleWrite("msg = " & $msg & @CR)

        Case $btnRunAll
            ;RUN ALL
            RunAllTasks()
        Case $btnClearSelection
            ;Clear allselections
            _GUICtrlTreeView_BeginUpdate($tvFuncSelect)
            For $item = 0 To UBound($functionArray) - 1
                _GUICtrlTreeView_SetChecked($hFuncTree, GUICtrlGetHandle($functionArray[$item][1]), False)
            Next
            _GUICtrlTreeView_EndUpdate($tvFuncSelect)
    EndSwitch
    Sleep(10)
WEnd
#region --- Setup function tree ---
Func loadFunctionTree($tvFuncSelect)
    ; [0]Menu item - main,secondary,etc,this items name
    ; [1]Link to menu item GUICtrlCreateTreeViewItem(
    ; [2]Description of item
    ; [3]Compatible OSVersion/OSArch check function eg JKDefragOSCheck() Could be set to TRUE for ALL OS
    ; [4]Function used to fill "$tvTasks" if given a parapeter of "ALL" it will return an array of tasks (eg run, update, etc)
    ;        If given a task name eg RUN it will return the function to perform it.
    ; [5]Execute the function selected eg JKDefragExecute(
    ;       Used to "autoset" functions that can/can't run
    _ArraySort($functionArray)
    _GUICtrlTreeView_BeginUpdate($tvFuncSelect)
    For $item = 0 To UBound($functionArray) - 1
        $functionArray[$item][1] = GUICtrlCreateTreeViewItem(GetItemText($functionArray, $item), FindTreeParent($functionArray, $item, $tvFuncSelect))
        _GUICtrlTreeView_SetItemParam($tvFuncSelect, $functionArray[$item][1], $item) ; set array index for back reference
    Next
    _GUICtrlTreeView_Expand($tvFuncSelect)
    _GUICtrlTreeView_EndUpdate($tvFuncSelect)
EndFunc   ;==>loadFunctionTree

Func FindTreeParent($functionArray, $item, $tvFuncSelect)
    If $item = 0 Then Return $tvFuncSelect

    Local $searchText = StringInStr($functionArray[$item][0], ",", -1) ; Find last occurence of ","
    If $searchText = 0 Then
        ; No commas so assume it's a top level item
        Return $tvFuncSelect
    Else
        ; extract parent part for search
        $searchText = StringLeft($functionArray[$item][0], $searchText - 1)
    EndIf

    Local $ParentIndex = _ArraySearch($functionArray, $searchText, 0, $item - 1)
    If Not @error Then
        Return $functionArray[$ParentIndex][1] ; parent index
    Else
        Return $tvFuncSelect
    EndIf
EndFunc   ;==>FindTreeParent

Func GetItemText($functionArray, $item)
    If $item = 0 Then Return $functionArray[$item][0]

    Local $searchText = StringInStr($functionArray[$item][0], ",", -1) ; Find last occurence of ","
    If $searchText = 0 Then
        ; No commas so assume it's a top level item
        Return $functionArray[$item][0]
    Else
        ; extract parent part for search
        Return StringMid($functionArray[$item][0], $searchText + 1, 100)
    EndIf
EndFunc   ;==>GetItemText
#endregion --- Setup function tree ---

;-------------------------------------------------------------------------------------------------------------
#region --- Function Tree View handling ---
Func _WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)

    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    Local $iPos, $iX, $iY, $tPt, $iHitTest, $fWasChecked
    Local $hItem

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    ConsoleWrite("keyboard_1" & @CR)
    Switch $hWndFrom
        Case $hFuncTree
            ConsoleWrite("keyboard" & @CR)
            Switch $iCode
                Case $WM_KEYUP

            EndSwitch
    EndSwitch

    #cs
        If $iCode = $EN_CHANGE Then ; If we have the correct message
        Switch $iIDFrom ; See if it comes from one of the inputs
        Case $hInput1
        ; Do something
        Case $hInput2
        ; Do something else
        EndSwitch
        EndIf
    #ce
EndFunc   ;==>_WM_COMMAND

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    Local $iPos, $iX, $iY, $tPt, $iHitTest, $fWasChecked
    Local $hItem

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hFuncTree
            ;ConsoleWrite('@@@ Debug(' & @ScriptLineNumber & ') : $iCode = ' & $iCode & " <" & $NM_CLICK & "> " & _GetHitString($iCode) & @CRLF) ;### Debug Console
            Switch $iCode
                Case $NM_CLICK
                    $iPos = _WinAPI_GetMessagePos()
                    $iX = BitAND($iPos, 0xFFFF)
                    $iY = BitShift($iPos, 16)
                    $tPt = DllStructCreate($tagPOINT)
                    DllStructSetData($tPt, "X", $iX)
                    DllStructSetData($tPt, "Y", $iY)
                    _WinAPI_ScreenToClient($hFuncTree, $tPt)
                    $iX = DllStructGetData($tPt, "X")
                    $iY = DllStructGetData($tPt, "Y")

                    $tTVHTInfo = _GUICtrlTreeView_HitTestEx($hFuncTree, $iX, $iY)
                    $tvFlag = DllStructGetData($tTVHTInfo, "Flags")
                    ;                   ConsoleWrite("." & $tvFlag & " " & _GetHitString($tvFlag) & ".")
                    Switch $tvFlag
                        Case $TVHT_ONITEMSTATEICON
                            $hItem = DllStructGetData($tTVHTInfo, "Item")

                            $fWasChecked = _GUICtrlTreeView_GetChecked($hFuncTree, $hItem)

                            If $fWasChecked Then
                                _GUICtrlTreeView_BeginUpdate($hFuncTree)
                                _Uncheck($hItem)
                                _GUICtrlTreeView_EndUpdate($hFuncTree)
                            Else
                                _GUICtrlTreeView_BeginUpdate($hFuncTree)
                                _Check($hItem)
                                _GUICtrlTreeView_EndUpdate($hFuncTree)
                            EndIf
                            ;                           ConsoleWrite("_")
                            _SetTaskTreeview($hItem)
                        Case $TVHT_ONITEMLABEL, $TVHT_ONITEMSTATEICON, $TVHT_ONITEMBUTTON
                            $hItem = DllStructGetData($tTVHTInfo, "Item")
                            ;                           ConsoleWrite("-")
                            _SetTaskTreeview($hItem)

                    EndSwitch
            EndSwitch
        Case $hTaskTree
            Switch $iCode
                Case $NM_CLICK
                    $iPos = _WinAPI_GetMessagePos()
                    $iX = BitAND($iPos, 0xFFFF)
                    $iY = BitShift($iPos, 16)
                    $tPt = DllStructCreate($tagPOINT)
                    DllStructSetData($tPt, "X", $iX)
                    DllStructSetData($tPt, "Y", $iY)
                    _WinAPI_ScreenToClient($hTaskTree, $tPt)
                    $iX = DllStructGetData($tPt, "X")
                    $iY = DllStructGetData($tPt, "Y")

                    $tTVHTInfo = _GUICtrlTreeView_HitTestEx($hTaskTree, $iX, $iY)
                    $tvFlag = DllStructGetData($tTVHTInfo, "Flags")
                    ;                   ConsoleWrite("." & $tvFlag & " " & _GetHitString($tvFlag) & ".")
                    Switch $tvFlag
                        Case $TVHT_ONITEMSTATEICON
                            $hItem = DllStructGetData($tTVHTInfo, "Item")

                            ;Save state of checkbox
                            Execute($functionArray[_GUICtrlTreeView_GetItemParam($hFuncTree, $hCurrentItemFuncTree)][4] & '"SET" , "' & _GUICtrlTreeView_GetText($hFuncTree, $hItem) & '",' & _GUICtrlTreeView_GetChecked($hTaskTree, $hItem) & ')')
                            ;ConsoleWrite($functionArray[_GUICtrlTreeView_GetItemParam($hFuncTree, $hCurrentItemFuncTree)][4] & '"SET" , "' & _GUICtrlTreeView_GetText($hFuncTree, $hItem) & '",' & _GUICtrlTreeView_GetChecked($hTaskTree, $hItem) & ')')
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Func _GetHitString($iFlags)
    ;Local $iFlags = DllStructGetData($tHitTest, "Flags")
    Select
        Case BitAND($iFlags, $TVHT_NOWHERE) <> 0
            Return "In the client area, but below the last item"
        Case BitAND($iFlags, $TVHT_ONITEMICON) <> 0
            Return "On the bitmap associated with an item"
        Case BitAND($iFlags, $TVHT_ONITEMLABEL) <> 0
            Return "On the text associated with an item"
        Case BitAND($iFlags, $TVHT_ONITEMINDENT) <> 0
            Return "In the indentation associated with an item"
        Case BitAND($iFlags, $TVHT_ONITEMBUTTON) <> 0
            Return "On the button associated with an item"
        Case BitAND($iFlags, $TVHT_ONITEMRIGHT) <> 0
            Return "In the area to the right of an item"
        Case BitAND($iFlags, $TVHT_ONITEMSTATEICON) <> 0
            Return "On the state icon for a item that is in a user-defined state"
        Case BitAND($iFlags, $TVHT_ABOVE) <> 0
            Return "Above the client area"
        Case BitAND($iFlags, $TVHT_BELOW) <> 0
            Return "Below the client area"
        Case BitAND($iFlags, $TVHT_TORIGHT) <> 0
            Return "To the left of the client area"
        Case BitAND($iFlags, $TVHT_TOLEFT) <> 0
            Return "To the right of the client area"
    EndSelect
EndFunc   ;==>_GetHitString

Func _Uncheck($hItem)
    Local $hParent, $hItemCur, $iLevel

    $iLevel = _GUICtrlTreeView_Level($hFuncTree, $hItem)
    $hItemCur = _GUICtrlTreeView_GetFirstChild($hFuncTree, $hItem)
    While $hItemCur And _GUICtrlTreeView_Level($hFuncTree, $hItemCur) > $iLevel
        _GUICtrlTreeView_SetChecked($hFuncTree, $hItemCur, False)
        $hItemCur = _GUICtrlTreeView_GetNext($hFuncTree, $hItemCur)
    WEnd

    $hParent = _GUICtrlTreeView_GetParentHandle($hFuncTree, $hItem)
    If $hParent Then
        While $hParent
            If $hParent <> $hItem Then _GUICtrlTreeView_SetChecked($hFuncTree, $hParent, False)
            $hParent = _GUICtrlTreeView_GetParentHandle($hFuncTree, $hParent)
        WEnd
    EndIf
EndFunc   ;==>_Uncheck

Func _Check($hItem)
    Local $hParent, $hItemCur, $iLevel

    $iLevel = _GUICtrlTreeView_Level($hFuncTree, $hItem)
    $hItemCur = _GUICtrlTreeView_GetFirstChild($hFuncTree, $hItem)
    While $hItemCur And _GUICtrlTreeView_Level($hFuncTree, $hItemCur) > $iLevel
        _GUICtrlTreeView_SetChecked($hFuncTree, $hItemCur)
        $hItemCur = _GUICtrlTreeView_GetNext($hFuncTree, $hItemCur)
    WEnd

    $hParent = _GUICtrlTreeView_GetParentHandle($hFuncTree, $hItem)
    While $hParent
        $hItemCur = _GUICtrlTreeView_GetFirstChild($hFuncTree, $hParent)
        While $hItemCur
            If Not _GUICtrlTreeView_GetChecked($hFuncTree, $hItemCur) And $hItemCur <> $hItem Then ExitLoop 2
            $hItemCur = _GUICtrlTreeView_GetNextSibling($hFuncTree, $hItemCur)
        WEnd

        _GUICtrlTreeView_SetChecked($hFuncTree, $hParent)
        $hParent = _GUICtrlTreeView_GetParentHandle($hFuncTree, $hParent)
    WEnd
EndFunc   ;==>_Check

Func _WinAPI_GetMessagePos()
    Local $aResult = DllCall("user32.dll", "uint", "GetMessagePos")

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

#endregion --- Function Tree View handling ---

Func _SetTaskTreeview($hItem)
    Local $hTask
    Global $hCurrentItemFuncTree

    $hCurrentItemFuncTree = $hItem

    ;   ConsoleWrite(@CR & _GUICtrlTreeView_GetText($hFuncTree, $hItem) & "__" & _GUICtrlTreeView_GetItemParam($hFuncTree, $hItem) & @CR)

    ;Set description for selected function
    GUICtrlSetData($edtDescription, $functionArray[_GUICtrlTreeView_GetItemParam($hFuncTree, $hItem)][2])

    ; set tasks that element can perform
    _GUICtrlTreeView_BeginUpdate($tvTasks)
    _GUICtrlTreeView_DeleteAll($tvTasks)
    If $functionArray[_GUICtrlTreeView_GetItemParam($hFuncTree, $hItem)][4] <> "" Then
        Local $functionTasks = Execute($functionArray[_GUICtrlTreeView_GetItemParam($hFuncTree, $hItem)][4] & '"GET")')
        For $item = 0 To UBound($functionTasks) - 1
            $hTask = GUICtrlCreateTreeViewItem($functionTasks[$item][0], $tvTasks)
            _GUICtrlTreeView_SetChecked($tvTasks, $hTask, $functionTasks[$item][1]) ; set state
        Next
    EndIf
    _GUICtrlTreeView_EndUpdate($tvTasks)

EndFunc   ;==>_SetTaskTreeview

Func BSBOdisableFunctions($sTask, $element = "", $bstate = True)
    Static $functionTasks[1][2]
    Switch StringUpper($sTask)
        Case "GET"
            If $functionTasks[0][0] = "" Then
                ; first run - Initialise array
                $functionTasks[0][0] = "run"
                $functionTasks[0][1] = True
            EndIf
            Return $functionTasks
        Case "SET"
            $functionTasks[_ArraySearch($functionTasks, $element)][1] = Not $bstate
        Case "SETALL"
            ; Used to set array to past state
            ; set array equal to passed array
            $functionTasks = $element
    EndSwitch
EndFunc   ;==>BSBOdisableFunctions

Func DefragFunctions($sTask, $element = "", $bstate = True)
    Static $functionTasks[1][2]
    Switch StringUpper($sTask)
        Case "GET"
            If $functionTasks[0][0] = "" Then
                ; first run - Initialise array
                $functionTasks[0][0] = "run"
                $functionTasks[0][1] = True
            EndIf
            Return $functionTasks
        Case "SET"
            $functionTasks[_ArraySearch($functionTasks, $element)][1] = Not $bstate
        Case "SETALL"
            ; Used to set array to past state
            ; set array equal to passed array
            $functionTasks = $element
    EndSwitch
EndFunc   ;==>DefragFunctions

Func JKDefragFunctions($sTask, $element = "", $bstate = True)
    Static $functionTasks[4][2]
    Switch $sTask
        Case "GET"
            If $functionTasks[0][0] = "" Then
                ; first run - Initialise array
                $functionTasks[0][0] = "Install"
                $functionTasks[0][1] = True
                $functionTasks[1][0] = "run"
                $functionTasks[1][1] = True
                $functionTasks[2][0] = "update"
                $functionTasks[2][1] = True
                $functionTasks[3][0] = "uninstall"
                $functionTasks[3][1] = False
            EndIf
            Return $functionTasks
        Case "SET"
            $functionTasks[_ArraySearch($functionTasks, $element)][1] = Not $bstate
        Case "SETALL"
            ; Used to set array to past state
            ; set array equal to passed array
            $functionTasks = $element
    EndSwitch
EndFunc   ;==>JKDefragFunctions

Func createRestorePointFunctions($sTask, $element = "", $bstate = True)
    Static $functionTasks[1][2]
    Switch StringUpper($sTask)
        Case "GET"
            If $functionTasks[0][0] = "" Then
                ; first run - Initialise array
                $functionTasks[0][0] = "run"
                $functionTasks[0][1] = True
            EndIf
            Return $functionTasks
        Case "SET"
            $functionTasks[_ArraySearch($functionTasks, $element)][1] = Not $bstate
        Case "SETALL"
            ; Used to set array to past state
            ; set array equal to passed array
            $functionTasks = $element
    EndSwitch
EndFunc   ;==>createRestorePointFunctions

Func RunAlltasks()
    ConsoleWrite("|" & @CR)
    For $item = 0 To UBound($functionArray) - 1
        ;test if item is checked in tree view and is a runnable item
        If _GUICtrlTreeView_GetChecked($tvFuncSelect, $functionArray[$item][1]) And $functionArray[$item][4] <> "" Then
            ;TODO : read tasks tree items to work out what needs to be run
            ConsoleWrite(">" & $functionArray[$item][0] & "<" & @CR)
            Local $functionTasks = Execute($functionArray[$item][4] & '"GET")')
            For $item2 = 0 To UBound($functionTasks) - 1
                If $functionTasks[$item2][1] = True Then
                    ConsoleWrite(@TAB & "Execute(" & $functionArray[$item][5] & '"' &  $functionTasks[$item2][0] & '")' & @CR)
                EndIf
            Next
        EndIf
    Next
EndFunc   ;==>RunAlltasks

Share this post


Link to post
Share on other sites

Theres also a Treeview notification for changes by keys ($TVN_KEYDOWN), or maybe try $TVN_SELCHANGINGA or $TVN_SELCHANGEDA.

For testing and exploring messages I always use something like this:

Func MY_WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    Local $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    Local $nNotifyCode = DllStructGetData($tNMHDR, "Code")
    
    ConsoleWrite(timerinit() & @tab & $hWndFrom & @tab & $nNotifyCode & @crlf)

    Return $GUI_RUNDEFMSG
EndFunc

Throws a lot of info that can be narrowed down by adding switch / case statements.

Share this post


Link to post
Share on other sites

Theres also a Treeview notification for changes by keys ($TVN_KEYDOWN), or maybe try $TVN_SELCHANGINGA or $TVN_SELCHANGEDA.

For testing and exploring messages I always use something like this:

;<SNIP>

Throws a lot of info that can be narrowed down by adding switch / case statements.

Yeah I was playing with consolewrite but because of other settings was missing all the important stuff.

Anyway finally found that -12 is usfull. Do you know of a way of doing a reverse lookup so I can find out what -12 is?

But I think I've got it all working now. :blink:

Here is the latest verison, still has functions that need adding and others that need clenaing up but it works. ;)

#include <StructureConstants.au3>
#include <TreeViewConstants.au3>
#include <WinAPI.au3>
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_Description=Health and fitness for your PC
#AutoIt3Wrapper_Res_Fileversion=0.0.1.0
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=p
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/striponly
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****

;#AutoIt3Wrapper_Run_Debug_Mode=y

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>

#region Globals for main routine
Global $hCurrentItemFuncTree ; used to save task states from each function

Global $functionArray[100][6]
Global $functionArrayLast = -1  ; Last element used in Fucntion array (will be incremented so set to -1 for zero elements)
; = [ _
;       ["Restore", "", "Windows registry restore tasks", "", "", "")
;       ["Restore,Create restore Point", "", "Use windows restore to create recovery point", "createRestorePointOSCheck()", "createRestorePointTasks(", "createRestorePointExecute(")
;       ["Defrag", "", "Disk defragmenter tasks", "", "", "")
;       ["Defrag,JKDefrag", "", "Disk defragmenter and optimizer for Windows", "JKDefragOSCheck()", "JKDefragTasks(", "JKDefragExecute(")
;       ["Defrag,Windows Defrag", "", "Built in disk defragmenter and optimizer for Windows", "DefragOSCheck()", "DefragTasks(", "DefragExecute(")
;       ["Twicks", "", "Tweaks for windows", "", "", "")
;       ["", "", "", "", "", ""] _
;       ]
;       ["Twicks,BSOD Disable", "", "Disable automatic restart on BSOD", "BSODdisableOSCheck()", "BSODdisableTasks(", "BSODdisableExecute("] _


#endregion Globals for main routine

;section headings for functions
addFunction("Restore",  "Windows registry restore tasks")
addFunction("Defrag",  "Disk defragmenter tasks")
addFunction("Twicks",  "Tweaks for windows")


#region Plugin #include area
; This area will just have an #include for each function the shell will perform

#region #include BSODdisable.au3
addFunction("Twicks,BSOD Disable", "Disable automatic restart on BSOD", "BSODdisableOSCheck()", "BSODdisableTasks(", "BSODdisableExecute(")

 Func BSODdisableOSCheck()
     ;TODO
 EndFunc

 Func BSODdisableExecute($task)
     Switch $task ; Mush be the same as supplied by task functions
        Case "run"
            Return RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\CrashControl", "AutoReboot", "REG_DWORD", "0")
    EndSwitch
 EndFunc

Func BSODdisableTasks($sTask, $element = "", $bstate = True)
    Static $functionTasks[1][2]
    Switch StringUpper($sTask)
        Case "GET"
            If $functionTasks[0][0] = "" Then
                ; first run - Initialise array
                $functionTasks[0][0] = "run"
                $functionTasks[0][1] = True
            EndIf
            Return $functionTasks
        Case "SET"
            $functionTasks[_ArraySearch($functionTasks, $element)][1] = Not $bstate
        Case "SETALL"
            ; Used to set array to past state
            ; set array equal to passed array
            $functionTasks = $element
    EndSwitch
EndFunc   ;==>BSODdisableTasks
#endregion #include BSODdisable.au3


addFunction("Defrag,Windows Defrag",  "Built in disk defragmenter and optimizer for Windows", "DefragOSCheck()", "DefragTasks(", "DefragExecute(")
Func DefragTasks($sTask, $element = "", $bstate = True)
    Static $functionTasks[1][2]
    Switch StringUpper($sTask)
        Case "GET"
            If $functionTasks[0][0] = "" Then
                ; first run - Initialise array
                $functionTasks[0][0] = "run"
                $functionTasks[0][1] = True
            EndIf
            Return $functionTasks
        Case "SET"
            $functionTasks[_ArraySearch($functionTasks, $element)][1] = Not $bstate
        Case "SETALL"
            ; Used to set array to past state
            ; set array equal to passed array
            $functionTasks = $element
    EndSwitch
EndFunc   ;==>DefragTasks

addFunction("Defrag,JKDefrag",  "Disk defragmenter and optimizer for Windows", "JKDefragOSCheck()", "JKDefragTasks(", "JKDefragExecute(")
Func JKDefragTasks($sTask, $element = "", $bstate = True)
    Static $functionTasks[4][2]
    Switch $sTask
        Case "GET"
            If $functionTasks[0][0] = "" Then
                ; first run - Initialise array
                $functionTasks[0][0] = "Install"
                $functionTasks[0][1] = True
                $functionTasks[1][0] = "update"
                $functionTasks[1][1] = True
                $functionTasks[2][0] = "run"
                $functionTasks[2][1] = True
                $functionTasks[3][0] = "uninstall"
                $functionTasks[3][1] = False
            EndIf
            Return $functionTasks
        Case "SET"
            $functionTasks[_ArraySearch($functionTasks, $element)][1] = Not $bstate
        Case "SETALL"
            ; Used to set array to past state
            ; set array equal to passed array
            $functionTasks = $element
    EndSwitch
EndFunc   ;==>JKDefragTasks

addFunction("Restore,Create restore Point",  "Use windows restore to create recovery point", "createRestorePointOSCheck()", "createRestorePointTasks(", "createRestorePointExecute(")
Func createRestorePointTasks($sTask, $element = "", $bstate = True)
    Static $functionTasks[1][2]
    Switch StringUpper($sTask)
        Case "GET"
            If $functionTasks[0][0] = "" Then
                ; first run - Initialise array
                $functionTasks[0][0] = "run"
                $functionTasks[0][1] = True
            EndIf
            Return $functionTasks
        Case "SET"
            $functionTasks[_ArraySearch($functionTasks, $element)][1] = Not $bstate
        Case "SETALL"
            ; Used to set array to past state
            ; set array equal to passed array
            $functionTasks = $element
    EndSwitch
EndFunc   ;==>createRestorePointTasks
#endregion Plugin #include area

Func addfunction($funcMenuItem, $description, $osCheck="", $tasksFunction="", $executeFunction="")
    $functionArrayLast += 1
    ; [0]Menu item - main,secondary,etc,this items name
    ; [1]Link to menu item GUICtrlCreateTreeViewItem(
    ; [2]Description of item
    ; [3]Compatible OSVersion/OSArch check function eg JKDefragOSCheck() Could be set to TRUE for ALL OS
    ; [4]Function used to fill "$tvTasks" if given a parapeter of "ALL" it will return an array of tasks (eg run, update, etc)
    ;        If given a task name eg RUN it will return the function to perform it.
    ; [5]Execute the function selected eg JKDefragExecute(
    ;       Used to "autoset" functions that can/can't run
    $functionArray[$functionArrayLast][0] = $funcMenuItem
    $functionArray[$functionArrayLast][2] = $description
    $functionArray[$functionArrayLast][3] = $osCheck
    $functionArray[$functionArrayLast][4] = $tasksFunction
    $functionArray[$functionArrayLast][5] = $executeFunction
EndFunc


;Opt("GUIOnEventMode", 1)
Local $frmMain = GUICreate("Health and fitness for your PC", 910, 568, 194, 128)
Local $tvFuncSelect = GUICtrlCreateTreeView(8, 8, 305, 497, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES, $WS_GROUP, $WS_TABSTOP, $WS_BORDER))
Local $tvTasks = GUICtrlCreateTreeView(320, 8, 105, 81, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES, $WS_GROUP, $WS_TABSTOP, $WS_BORDER))
Local $btnRunSingleTask = GUICtrlCreateButton("Run this task", 320, 96, 105, 33, $WS_GROUP)
GUICtrlSetFont($btnRunSingleTask, 10, 800, 0, "MS Sans Serif")
GUICtrlSetTip($btnRunSingleTask, "Run the task ONLY")

Local $btnRunAll = GUICtrlCreateButton("Run All Tasks", 800, 512, 105, 25, $WS_GROUP)
GUICtrlSetFont($btnRunAll, 10, 800, 0, "MS Sans Serif")
GUICtrlSetTip($btnRunAll, "Run All selected tasks")

Local $btnSaveSelected = GUICtrlCreateButton("Save Selected", 200, 512, 113, 25, $WS_GROUP)
GUICtrlSetFont($btnSaveSelected, 10, 800, 0, "MS Sans Serif")

Local $btnLoadSelected = GUICtrlCreateButton("Load Selected", 8, 512, 113, 25, $WS_GROUP)
GUICtrlSetFont($btnLoadSelected, 10, 800, 0, "MS Sans Serif")

Local $edtDescription = GUICtrlCreateEdit("", 432, 8, 473, 121, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN))
GUICtrlSetData($edtDescription, "Task Description")

Local $btnClearSelection = GUICtrlCreateButton("Clear all", 128, 512, 65, 25, $WS_GROUP)
GUICtrlSetFont($btnClearSelection, 10, 800, 0, "MS Sans Serif")

Global $hFuncTree = GUICtrlGetHandle($tvFuncSelect) ; Used by _WM_NOTIFY function
Global $hTaskTree = GUICtrlGetHandle($tvTasks) ; Used by _WM_NOTIFY function
GUIRegisterMsg($WM_NOTIFY, "_WM_NOTIFY")
;GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

loadFunctionTree($tvFuncSelect) ; Load Function  treeview

GUISetState(@SW_SHOW)


While 1
    $msg = GUIGetMsg()
    ;If $msg <> 0 Then ConsoleWrite("msg = " & $msg & @CR)
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $tvFuncSelect
            ;ConsoleWrite("msg = " & $msg & @CR)

        Case $btnRunAll
            ;RUN ALL
            RunAllTasks()
        Case $btnClearSelection
            ;Clear allselections
            _GUICtrlTreeView_BeginUpdate($tvFuncSelect)
            For $item = 0 To $functionArrayLast
                _GUICtrlTreeView_SetChecked($hFuncTree, GUICtrlGetHandle($functionArray[$item][1]), False)
            Next
            _GUICtrlTreeView_EndUpdate($tvFuncSelect)
    EndSwitch
    Sleep(10)
WEnd
#region --- Setup function tree ---
Func loadFunctionTree($tvFuncSelect)
    ; [0]Menu item - main,secondary,etc,this items name
    ; [1]Link to menu item GUICtrlCreateTreeViewItem(
    ; [2]Description of item
    ; [3]Compatible OSVersion/OSArch check function eg JKDefragOSCheck() Could be set to TRUE for ALL OS
    ; [4]Function used to fill "$tvTasks" if given a parapeter of "ALL" it will return an array of tasks (eg run, update, etc)
    ;        If given a task name eg RUN it will return the function to perform it.
    ; [5]Execute the function selected eg JKDefragExecute(
    ;       Used to "autoset" functions that can/can't run
    _ArraySort($functionArray,0,0,$functionArrayLast)
    _GUICtrlTreeView_BeginUpdate($tvFuncSelect)
    For $item = 0 To $functionArrayLast
        $functionArray[$item][1] = GUICtrlCreateTreeViewItem(GetItemText($functionArray, $item), FindTreeParent($functionArray, $item, $tvFuncSelect))
        _GUICtrlTreeView_SetItemParam($tvFuncSelect, $functionArray[$item][1], $item) ; set array index for back reference
    Next
    _GUICtrlTreeView_Expand($tvFuncSelect)
    _GUICtrlTreeView_EndUpdate($tvFuncSelect)
EndFunc   ;==>loadFunctionTree

Func FindTreeParent($functionArray, $item, $tvFuncSelect)
    If $item = 0 Then Return $tvFuncSelect

    Local $searchText = StringInStr($functionArray[$item][0], ",", -1) ; Find last occurence of ","
    If $searchText = 0 Then
        ; No commas so assume it's a top level item
        Return $tvFuncSelect
    Else
        ; extract parent part for search
        $searchText = StringLeft($functionArray[$item][0], $searchText - 1)
    EndIf

    Local $ParentIndex = _ArraySearch($functionArray, $searchText, 0, $item - 1)
    If Not @error Then
        Return $functionArray[$ParentIndex][1] ; parent index
    Else
        Return $tvFuncSelect
    EndIf
EndFunc   ;==>FindTreeParent

Func GetItemText($functionArray, $item)
    If $item = 0 Then Return $functionArray[$item][0]

    Local $searchText = StringInStr($functionArray[$item][0], ",", -1) ; Find last occurence of ","
    If $searchText = 0 Then
        ; No commas so assume it's a top level item
        Return $functionArray[$item][0]
    Else
        ; extract parent part for search
        Return StringMid($functionArray[$item][0], $searchText + 1, 100)
    EndIf
EndFunc   ;==>GetItemText
#endregion --- Setup function tree ---

;-------------------------------------------------------------------------------------------------------------
#region --- Function Tree View handling ---

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR
    Local $iPos, $iX, $iY, $tPt, $iHitTest, $fWasChecked
    Local $hItem

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hFuncTree
            Switch $iCode
                Case -12
                    $hItem = _GUICtrlTreeView_GetSelection($hFuncTree)
                    _SetTaskTreeview($hItem)

                Case $NM_CLICK
                    $hItem = _GUICtrlTreeView_GetSelection($hFuncTree)
                    _SetTaskTreeview($hItem)

                    $iPos = _WinAPI_GetMessagePos()
                    $iX = BitAND($iPos, 0xFFFF)
                    $iY = BitShift($iPos, 16)
                    $tPt = DllStructCreate($tagPOINT)
                    DllStructSetData($tPt, "X", $iX)
                    DllStructSetData($tPt, "Y", $iY)
                    _WinAPI_ScreenToClient($hFuncTree, $tPt)
                    $iX = DllStructGetData($tPt, "X")
                    $iY = DllStructGetData($tPt, "Y")

                    $tTVHTInfo = _GUICtrlTreeView_HitTestEx($hFuncTree, $iX, $iY)
                    $tvFlag = DllStructGetData($tTVHTInfo, "Flags")
                    Switch $tvFlag
                        Case $TVHT_ONITEMSTATEICON
                            $hItem = DllStructGetData($tTVHTInfo, "Item")

                            $fWasChecked = _GUICtrlTreeView_GetChecked($hFuncTree, $hItem)

                            If $fWasChecked Then
                                _GUICtrlTreeView_BeginUpdate($hFuncTree)
                                _Uncheck($hItem)
                                _GUICtrlTreeView_EndUpdate($hFuncTree)
                            Else
                                _GUICtrlTreeView_BeginUpdate($hFuncTree)
                                _Check($hItem)
                                _GUICtrlTreeView_EndUpdate($hFuncTree)
                            EndIf
                            ;                           ConsoleWrite("_")
                            ;                           _SetTaskTreeview($hItem)
                            ;                       Case $TVHT_ONITEMLABEL, $TVHT_ONITEMSTATEICON, $TVHT_ONITEMBUTTON
                            ;                           $hItem = DllStructGetData($tTVHTInfo, "Item")
                            ;                           $hItem = _GUICtrlTreeView_GetSelection($hFuncTree)
                            ;                           ;                           ConsoleWrite("-")
                            ;                           _SetTaskTreeview($hItem)

                    EndSwitch
            EndSwitch
        Case $hTaskTree
            Switch $iCode
                Case $NM_CLICK
                    $iPos = _WinAPI_GetMessagePos()
                    $iX = BitAND($iPos, 0xFFFF)
                    $iY = BitShift($iPos, 16)
                    $tPt = DllStructCreate($tagPOINT)
                    DllStructSetData($tPt, "X", $iX)
                    DllStructSetData($tPt, "Y", $iY)
                    _WinAPI_ScreenToClient($hTaskTree, $tPt)
                    $iX = DllStructGetData($tPt, "X")
                    $iY = DllStructGetData($tPt, "Y")

                    $tTVHTInfo = _GUICtrlTreeView_HitTestEx($hTaskTree, $iX, $iY)
                    $tvFlag = DllStructGetData($tTVHTInfo, "Flags")
                    ;                   ConsoleWrite("." & $tvFlag & " " & _GetHitString($tvFlag) & ".")
                    Switch $tvFlag
                        Case $TVHT_ONITEMSTATEICON
                            $hItem = DllStructGetData($tTVHTInfo, "Item")

                            ;Save state of checkbox in task box
                            Execute($functionArray[_GUICtrlTreeView_GetItemParam($hFuncTree, $hCurrentItemFuncTree)][4] & '"SET" , "' & _GUICtrlTreeView_GetText($hFuncTree, $hItem) & '",' & _GUICtrlTreeView_GetChecked($hTaskTree, $hItem) & ')')
                            ;ConsoleWrite($functionArray[_GUICtrlTreeView_GetItemParam($hFuncTree, $hCurrentItemFuncTree)][4] & '"SET" , "' & _GUICtrlTreeView_GetText($hFuncTree, $hItem) & '",' & _GUICtrlTreeView_GetChecked($hTaskTree, $hItem) & ')')
                    EndSwitch
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_NOTIFY

Func _Uncheck($hItem)
    Local $hParent, $hItemCur, $iLevel

    $iLevel = _GUICtrlTreeView_Level($hFuncTree, $hItem)
    $hItemCur = _GUICtrlTreeView_GetFirstChild($hFuncTree, $hItem)
    While $hItemCur And _GUICtrlTreeView_Level($hFuncTree, $hItemCur) > $iLevel
        _GUICtrlTreeView_SetChecked($hFuncTree, $hItemCur, False)
        $hItemCur = _GUICtrlTreeView_GetNext($hFuncTree, $hItemCur)
    WEnd

    $hParent = _GUICtrlTreeView_GetParentHandle($hFuncTree, $hItem)
    If $hParent Then
        While $hParent
            If $hParent <> $hItem Then _GUICtrlTreeView_SetChecked($hFuncTree, $hParent, False)
            $hParent = _GUICtrlTreeView_GetParentHandle($hFuncTree, $hParent)
        WEnd
    EndIf
EndFunc   ;==>_Uncheck

Func _Check($hItem)
    Local $hParent, $hItemCur, $iLevel

    $iLevel = _GUICtrlTreeView_Level($hFuncTree, $hItem)
    $hItemCur = _GUICtrlTreeView_GetFirstChild($hFuncTree, $hItem)
    While $hItemCur And _GUICtrlTreeView_Level($hFuncTree, $hItemCur) > $iLevel
        _GUICtrlTreeView_SetChecked($hFuncTree, $hItemCur)
        $hItemCur = _GUICtrlTreeView_GetNext($hFuncTree, $hItemCur)
    WEnd

    $hParent = _GUICtrlTreeView_GetParentHandle($hFuncTree, $hItem)
    While $hParent
        $hItemCur = _GUICtrlTreeView_GetFirstChild($hFuncTree, $hParent)
        While $hItemCur
            If Not _GUICtrlTreeView_GetChecked($hFuncTree, $hItemCur) And $hItemCur <> $hItem Then ExitLoop 2
            $hItemCur = _GUICtrlTreeView_GetNextSibling($hFuncTree, $hItemCur)
        WEnd

        _GUICtrlTreeView_SetChecked($hFuncTree, $hParent)
        $hParent = _GUICtrlTreeView_GetParentHandle($hFuncTree, $hParent)
    WEnd
EndFunc   ;==>_Check

Func _WinAPI_GetMessagePos()
    Local $aResult = DllCall("user32.dll", "uint", "GetMessagePos")

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

#endregion --- Function Tree View handling ---

Func _SetTaskTreeview($hItem)
    Local $hTask
    Global $hCurrentItemFuncTree

    Static $lasthItem
    If $hItem <> $lasthItem Then
        $lasthItem = $hItem


        $hCurrentItemFuncTree = $hItem

        ;   ConsoleWrite(@CR & _GUICtrlTreeView_GetText($hFuncTree, $hItem) & "__" & _GUICtrlTreeView_GetItemParam($hFuncTree, $hItem) & @CR)

        ;Set description for selected function
        GUICtrlSetData($edtDescription, $functionArray[_GUICtrlTreeView_GetItemParam($hFuncTree, $hItem)][2])

        ; set tasks that element can perform
        _GUICtrlTreeView_BeginUpdate($tvTasks)
        _GUICtrlTreeView_DeleteAll($tvTasks)
        If $functionArray[_GUICtrlTreeView_GetItemParam($hFuncTree, $hItem)][4] <> "" Then
            Local $functionTasks = Execute($functionArray[_GUICtrlTreeView_GetItemParam($hFuncTree, $hItem)][4] & '"GET")')
            For $item = 0 To UBound($functionTasks) - 1
                $hTask = GUICtrlCreateTreeViewItem($functionTasks[$item][0], $tvTasks)
                _GUICtrlTreeView_SetChecked($tvTasks, $hTask, $functionTasks[$item][1]) ; set state
            Next
        EndIf
        _GUICtrlTreeView_EndUpdate($tvTasks)
    EndIf

EndFunc   ;==>_SetTaskTreeview


Func RunAlltasks()
    ; Run ALL the selected tasks from
    Local $result ; collects results from running task
    Local $myError ; collects @error state
    For $item = 0 To $functionArrayLast
        ;test if item is checked in tree view and is a runnable item
        If _GUICtrlTreeView_GetChecked($tvFuncSelect, $functionArray[$item][1]) And $functionArray[$item][4] <> "" Then
            Local $functionTasks = Execute($functionArray[$item][4] & '"GET")')
            For $item2 = 0 To UBound($functionTasks) - 1
                If $functionTasks[$item2][1] = True Then
                    LogState("---Running   Function: " & $functionArray[$item][5] & '"' & $functionTasks[$item2][0] & '")')
                    ;LogState("Execute(" & $functionArray[$item][5] & '"' & $functionTasks[$item2][0] & '")')
                    $result = Execute($functionArray[$item][5] & '"' & $functionTasks[$item2][0] & '")')
                    $myError = @error
                    LogState("---Completed Function: " & $functionArray[$item][5] & '"' & $functionTasks[$item2][0] & '")' & " Result: " & $result & " @error = " & $myError)
                EndIf
            Next
        EndIf
    Next
EndFunc   ;==>RunAlltasks

Func LogState($text)
    ; test version of log function
    ConsoleWrite( @HOUR & ":" & @MIN & ":" & @SEC & " > " & @TAB & $text & @CR)
EndFunc

Share this post


Link to post
Share on other sites

Anyway finally found that -12 is usfull. Do you know of a way of doing a reverse lookup so I can find out what -12 is?

; Windows Notification Message Constants
Global Const $NM_FIRST = 0
Global Const $NM_CUSTOMDRAW = $NM_FIRST - 12

:blink:, but if it works ;), hopefully it's not fired too often. For a reverse lookup of Notifications first take a look at the values in WindowsConstants.au3, additional notifications can be found in the constants file of the respective control type, in the case TreeViewConstants.au3.

Share this post


Link to post
Share on other sites

; Windows Notification Message Constants
Global Const $NM_FIRST = 0
Global Const $NM_CUSTOMDRAW = $NM_FIRST - 12

:blink:, but if it works ;), hopefully it's not fired too often. For a reverse lookup of Notifications first take a look at the values in WindowsConstants.au3, additional notifications can be found in the constants file of the respective control type, in the case TreeViewConstants.au3.

I actually searach the constants files for "- 12" and didn't find it... sigh

It was the only value that came up consistantly when using cursor control. It does come up a few times on each movment so I've added a check

Static $lasthItem
    If $hItem <> $lasthItem Then
        $lasthItem = $hItem

So there isn't a lot unnecessary work done.

Anyway it all appears to work "ok" now though I'm SURE there are better ways of doing what I'm doing as most of oti was trial and error.

THANKS for all your help!

Share this post


Link to post
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
Sign in to follow this  
Followers 0