Jump to content

_GuiCtrlTreeView_Add and OnEvent [SOLVED]


Recommended Posts

_GUICtrlTreeView_BeginUpdate($tree)
    $root = _GUICtrlTreeView_Add($tree, 0, "PC - "& $pcname)
    _GUICtrlTreeView_SetIcon($tree,$root,"%SystemRoot%\system32\SHELL32.dll",15)
_GUICtrlTreeView_EndUpdate($tree)

I do not only want to expand this tree when I click the item, I also want to trigger an OnEvent funtion when I click it so I can update the child treeviewtems (which are retrieved via TCP/IP).

GuiCtrlSetOnEvent($root,"function") doesn't work, probably because the _GuiCtrlTreeView functions in autoit are some kind of UDF.

I tried WM_NOTIFY(), but that only registers the click/rightclick etc, not the item clicked on.

I was trying all kind of workarounds with mouse position lookup functions etc, but if someone knows a cleaner way to do this, I would be very happy!!

Thanks!

Edited by okdewit
Link to comment
Share on other sites

.........

..........

I tried WM_NOTIFY(), but that only registers the click/rightclick etc, not the item clicked on.

........

Thanks!

Hi, use the WM_NOTIFY() to get the click and _GUICtrlTreeView_HitTestEx() or _GUICtrlTreeView_HitTestItem() to find what treeview item has been clicked.
#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

$Debug_TV = False

Global $hTreeView

_Main()

Func _Main()

    Local $GUI, $hItem
    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    $GUI = GUICreate("(UDF Created) TreeView Create", 400, 300)

    $hTreeView = _GUICtrlTreeView_Create($GUI, 2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE)
    GUISetState()

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    _GUICtrlTreeView_BeginUpdate($hTreeView)
    For $x = 1 To Random(2, 10, 1)
        $hItem = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("[%02d] New Item", $x))
        For $y = 1 To Random(2, 10, 1)
            _GUICtrlTreeView_AddChild($hTreeView, $hItem, StringFormat("[%02d] New Child", $y))
        Next
    Next
    _GUICtrlTreeView_EndUpdate($hTreeView)

    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc   ;==>_Main

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
                    Local $tPoint, $tTVHEx, $TVhItem, $mX, $mY
                    $tPoint = _WinAPI_GetMousePos(True, $hWnd)
                    $mX = DllStructGetData($tPoint, "X")
                    $mY = DllStructGetData($tPoint, "Y")
                    $tTVHEx = _GUICtrlTreeView_HitTestEx($hWndTreeview, $mX, $mY)
                    $TVhItem = DllStructGetData($tTVHEx, "Item")
                    Switch DllStructGetData($tTVHEx, "Flags")
                        Case $TVHT_ONITEM
                            _DebugPrint("$NM_CLICK" & @LF & "--> $TVHT_ONITEM Item Handle:" & @TAB & $TVhItem)                          
                        Case $TVHT_ONITEMBUTTON
                            _DebugPrint("$NM_CLICK" & @LF & "--> $TVHT_ONITEMBUTTON Item Handle:" & @TAB & $TVhItem)
                        Case $TVHT_ONITEMICON
                            _DebugPrint("$NM_CLICK" & @LF & "--> $TVHT_ONITEMICON Item Handle:" & @TAB & $TVhItem)
                        Case $TVHT_ONITEMLABEL 
                            _DebugPrint("$NM_CLICK" & @LF & "--> $TVHT_ONITEMLABEL Item Handle:" & @TAB & $TVhItem)
                    EndSwitch       
;~                  Return 1 ; nonzero to not allow the default processing
                    Return 0 ; zero to allow the default processing
            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

Cheers

Edited by smashly
Link to comment
Share on other sites

Thanks for the quick response!

Looks perfect. going to try that as soon as I'm on a windows PC again :)

--------------------------------

EDIT: Changed

$tPoint = _WinAPI_GetMousePos(True, $hWnd)

to:

$tPoint = _WinAPI_GetMousePos(True, $hWndTreeview)

Otherwise the mouseposition was wrong and it returned 1024 (Right of Control) instead of the 1 or 4 I wanted (Label/Icon)

---------------------------------

For anyone interested, all the cases are described in GuiTreeView.au3:

Func _GUICtrlTreeView_HitTest($hWnd, $iX, $iY)
    Local $iFlags, $iResult, $tHitTest

    $tHitTest = _GUICtrlTreeView_HitTestEx($hWnd, $iX, $iY)
    $iFlags = DllStructGetData($tHitTest, "Flags")
    If BitAND($iFlags, $TVHT_NOWHERE) <> 0 Then $iResult = BitOR($iResult, 1)
    If BitAND($iFlags, $TVHT_ONITEMICON) <> 0 Then $iResult = BitOR($iResult, 2)
    If BitAND($iFlags, $TVHT_ONITEMLABEL) <> 0 Then $iResult = BitOR($iResult, 4)
    If BitAND($iFlags, $TVHT_ONITEMINDENT) <> 0 Then $iResult = BitOR($iResult, 8)
    If BitAND($iFlags, $TVHT_ONITEMBUTTON) <> 0 Then $iResult = BitOR($iResult, 16)
    If BitAND($iFlags, $TVHT_ONITEMRIGHT) <> 0 Then $iResult = BitOR($iResult, 32)
    If BitAND($iFlags, $TVHT_ONITEMSTATEICON) <> 0 Then $iResult = BitOR($iResult, 64)
    If BitAND($iFlags, $TVHT_ABOVE) <> 0 Then $iResult = BitOR($iResult, 128)
    If BitAND($iFlags, $TVHT_BELOW) <> 0 Then $iResult = BitOR($iResult, 256)
    If BitAND($iFlags, $TVHT_TORIGHT) <> 0 Then $iResult = BitOR($iResult, 512)
    If BitAND($iFlags, $TVHT_TOLEFT) <> 0 Then $iResult = BitOR($iResult, 1024)
    Return $iResult
EndFunc   ;==>_GUICtrlTreeView_HitTest
Edited by okdewit
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...