Sign in to follow this  
Followers 0
sandyd

Get text of selected treeview item

13 posts in this topic

Hi all,

I have a treeview that is filled with computers. I would like to click one of them and have some operation done on the text within it.

How would I go about this?

e.g.

-Computers

---- Computer1

---- Computer2 <--- How can I get this text when I click the treeview item

---- Computer3

All help would be most appreciated.


----[ SandyD ]---

Share this post


Link to post
Share on other sites



Hi,

_GUICtrlTreeViewGetText ???

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Sorry should have said that I have created the items dynamically with no control IDs.

How do I know which one has been clicked?


----[ SandyD ]---

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Sorry should have said that I have created the items dynamically with no control IDs.

How do I know which one has been clicked?

Go back and give them variables so you can easily collect their control id's. Otherwise hope you know where each computer is going to be or you're pretty much SOL. If the variables would grow, just make them an array. Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

This example-script from the helpfile should work, but pressing info just prints "0".

Maybe it has something to do with the change they did with GUICtrlRead in the latest

beta... Also, the constants in the beginning of this script should be removed, as they

are already declared.

#include <GUIConstants.au3>

;~ Global Const $TV_FIRST      = 0x1100 
;~ Global Const $TVM_EXPAND    = $TV_FIRST + 2
;~ Global Const $TVE_TOGGLE    = 0x0003


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

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

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

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

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

GUISetState ()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $cancelbutton Or $msg = $GUI_EVENT_CLOSE
            ExitLoop
    
        Case $msg = $togglebutton   ; Toggle the bold painting
            If BitAnd(GUICtrlRead($generalitem), $GUI_DEFBUTTON) Then
                GUICtrlSetState($generalitem, 0)
                GUICtrlSetState($displayitem, 0)
            Else
                GUICtrlSetState($generalitem, $GUI_DEFBUTTON)
                GUICtrlSetState($displayitem, $GUI_DEFBUTTON)
            EndIf
        
        Case $msg = $infobutton
            $item = GUICtrlRead($treeview)      ; Get the controlID of the current selected treeview item
            If $item = 0 Then
                MsgBox(64, "TreeView Demo", "No item currently selected")
            Else
                $text = GUICtrlRead($item, 1) ; Get the text of the treeview item
                If $text == "" Then
                    MsgBox(16, "Error", "Error while retrieving infos about item")
                Else
                    MsgBox(64, "TreeView Demo", "Current item selected is: " & $text)  ; $advmsg[0] contains the text and $advmsg[1] the state value of the treeview item
                EndIf
            EndIf
            
        Case $msg = $statebutton
            $item = GUICtrlRead($treeview)
            If $item > 0 Then
                $hItem = GUICtrlGetHandle($item)
                GuiCtrlSendMsg($treeview, $TVM_EXPAND, $TVE_TOGGLE, $hItem)
            EndIf
            
        ; The following items will hide the other labels (1st and 2nd parameter) and then show the 'own' labels (3rd and 4th parameter)
        Case $msg = $generalitem
            GUIChangeItems($aboutlabel, $compinfo, $startlabel, $startlabel)
        
        Case $msg = $aboutitem
            GUICtrlSetState ($compinfo, $GUI_HIDE)
            GUIChangeItems($startlabel, $startlabel, $aboutlabel, $aboutlabel)
            
        Case $msg = $compitem
            GUIChangeItems($startlabel, $aboutlabel, $compinfo, $compinfo)
    EndSelect
WEnd

GUIDelete()
Exit

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

Share this post


Link to post
Share on other sites

OK, here's what I have:

; Load Treeview
$tviComputers    = GUICtrlCreateTreeViewitem("Computers", $tvMain)
$rs = $objConn.Execute("SELECT DISTINCT sc_pc_id FROM pc_scan ORDER BY sc_pc_id")
While Not $rs.Eof 
 GUICtrlCreateTreeViewitem($rs.Fields("sc_pc_id").Value, $tviComputers)
 $rs.MoveNext
WEnd


; Main Loop
While 1
 $msg = GuiGetMsg()
 Select
 Case $msg = $GUI_EVENT_CLOSE
  ExitLoop
 Case $msg = $tviComputers
  _DoShowComputers()
 EndSelect
WEnd

Can some be so helpful and explain what code I need to add to be able to then know when an item has been clicked?


----[ SandyD ]---

Share this post


Link to post
Share on other sites

1 you should have a TreeView before you have an "item" :D .

Create an array like maybe:

#include <GuiTreeView.au3> ;;;;;;;;;;;;;;;; Note Here ;;;;;;;;;;;;;;;;;
; Load Treeview
Global $atvItem[1], $iCount = 0
$SomeTreeView = GUICtrlCreateTreeView() ;;;;;;;;;;;; No TreeView ... Assuming you just left it out on purpose ;;;;;;;;;;;;;;;;;;;
$tviComputers    = GUICtrlCreateTreeViewItem("Computers", $tvMain)
$rs = $objConn.Execute ("SELECT DISTINCT sc_pc_id FROM pc_scan ORDER BY sc_pc_id")
While Not $rs.Eof
    $iCount += 1
    ReDim $atvItem[$iCount + 1]
    $atvItem[$iCount] = GUICtrlCreateTreeViewItem($rs.Fields ("sc_pc_id").Value, $tviComputers)
    $rs.MoveNext
WEnd
; Main Loop
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $tviComputers
            _DoShowComputers ()
    EndSelect
    For $xCount = 1 To UBound($atvItem) - 1
        If $msg = $atvItem[$xCount] Then
            MsgBox(64, 'Info', 'Treeview Array ' & $xCount & ' Was Clicked' & @CR & _
                    'Information: ' & _GUICtrlTreeViewGetText($SomeTreeView, $atvItem[$xCount]))
        EndIf
    Next
WEnd
Of course this is untested.

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Untested or not, it works like a charm.

Thank you very much for that, i've been trying everything and coming up with nothing.


----[ SandyD ]---

Share this post


Link to post
Share on other sites

Another method to the madness:

(Left the extra stuff in)

#include <GUIConstants.au3>
#include <TreeViewConstants.au3>
#include <GuiTreeView.au3>
Opt("MustDeclareVars", 1)

Global Const $DebugIt = 1

Global Const $WM_NOTIFY = 0x004E
Global $TreeView = -999

Global $Clicked = 0

; ListView and TreeView Events
Global Const $NM_FIRST = 0
Global Const $NM_LAST = (-99)
Global Const $NM_OUTOFMEMORY = ($NM_FIRST - 1)
Global Const $NM_CLICK = ($NM_FIRST - 2)
Global Const $NM_DBLCLK = ($NM_FIRST - 3)
Global Const $NM_RETURN = ($NM_FIRST - 4)
Global Const $NM_RCLICK = ($NM_FIRST - 5)
Global Const $NM_RDBLCLK = ($NM_FIRST - 6)
Global Const $NM_SETFOCUS = ($NM_FIRST - 7)
Global Const $NM_KILLFOCUS = ($NM_FIRST - 8)
Global Const $NM_CUSTOMDRAW = ($NM_FIRST - 12)
Global Const $NM_HOVER = ($NM_FIRST - 13)
Global Const $NM_NCHITTEST = ($NM_FIRST - 14)
Global Const $NM_KEYDOWN = ($NM_FIRST - 15)
Global Const $NM_RELEASEDCAPTURE = ($NM_FIRST - 16)
Global Const $NM_SETCURSOR = ($NM_FIRST - 17)
Global Const $NM_CHAR = ($NM_FIRST - 18)
Global Const $NM_TOOLTIPSCREATED = ($NM_FIRST - 19)

; TreeView only events
;~ If Not IsDeclared('TV_FIRST') Then Global Const $TV_FIRST = 0x1100
;~ Global Const $TVIS_EXPANDED = 0x20
;~ Global Const $TVIS_EXPANDEDONCE = 0x40
Global Const $TVM_GETITEMSTATE = ($TV_FIRST + 39)
Global Const $TVN_FIRST = -400
Global Const $TVN_LAST = -499
Global Const $TVN_SELCHANGINGA = ($TVN_FIRST - 1)
Global Const $TVN_SELCHANGINGW = ($TVN_FIRST - 50)
Global Const $TVN_SELCHANGEDA = ($TVN_FIRST - 2)
Global Const $TVN_SELCHANGEDW = ($TVN_FIRST - 51)
Global Const $TVN_GETDISPINFOA = ($TVN_FIRST - 3)
Global Const $TVN_GETDISPINFOW = ($TVN_FIRST - 52)
Global Const $TVN_SETDISPINFOA = ($TVN_FIRST - 4)
Global Const $TVN_SETDISPINFOW = ($TVN_FIRST - 53)
Global Const $TVN_ITEMEXPANDINGA = ($TVN_FIRST - 5)
Global Const $TVN_ITEMEXPANDINGW = ($TVN_FIRST - 54)
Global Const $TVN_ITEMEXPANDEDA = ($TVN_FIRST - 6)
Global Const $TVN_ITEMEXPANDEDW = ($TVN_FIRST - 55)
Global Const $TVN_BEGINDRAGA = ($TVN_FIRST - 7)
Global Const $TVN_BEGINDRAGW = ($TVN_FIRST - 56)
Global Const $TVN_BEGINRDRAGA = ($TVN_FIRST - 8)
Global Const $TVN_BEGINRDRAGW = ($TVN_FIRST - 57)
Global Const $TVN_DELETEITEMA = ($TVN_FIRST - 9)
Global Const $TVN_DELETEITEMW = ($TVN_FIRST - 58)
Global Const $TVN_BEGINLABELEDITA = ($TVN_FIRST - 10)
Global Const $TVN_BEGINLABELEDITW = ($TVN_FIRST - 59)
Global Const $TVN_ENDLABELEDITA = ($TVN_FIRST - 11)
Global Const $TVN_ENDLABELEDITW = ($TVN_FIRST - 60)
Global Const $TVN_KEYDOWN = ($TVN_FIRST - 12)
Global Const $TVN_GETINFOTIPA = ($TVN_FIRST - 13)
Global Const $TVN_GETINFOTIPW = ($TVN_FIRST - 14)
Global Const $TVN_SINGLEEXPAND = ($TVN_FIRST - 15)


Dim $h_GUI, $Msg, $TreeView
Dim $h_root1, $h_root2, $h_root3, $h_item
Dim $n_btn_gettext

$h_GUI = GUICreate("TreeView UDF Sample", 220, 250)

$TreeView = GUICtrlCreateTreeView(10, 10, 200, 200, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetImage(-1, "shell32.dll", 3, 4)
GUICtrlSetImage(-1, "shell32.dll", 4, 2)

$h_root1 = _GUICtrlTreeViewInsertItem($TreeView, "RootItem1")
_GUICtrlTreeViewSetIcon($TreeView, $h_root1, "shell32.dll", 7)

_GUICtrlTreeViewInsertItem($TreeView, "SubItem1", $h_root1)
_GUICtrlTreeViewInsertItem($TreeView, "SubItem2", $h_root1)

$h_root2 = _GUICtrlTreeViewInsertItem($TreeView, "RootItem2")
_GUICtrlTreeViewSetIcon($TreeView, $h_root2, "shell32.dll", 12)

$h_root3 = _GUICtrlTreeViewInsertItem($TreeView, "RootItem3")
_GUICtrlTreeViewInsertItem($TreeView, "SubItem3", $h_root3)
_GUICtrlTreeViewInsertItem($TreeView, "SubItem4", $h_root3)

$n_btn_gettext = GUICtrlCreateButton("Get text", 10, 220, 200, 20)

GUISetState()

;Register WM_NOTIFY  events
GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

While 1
    $Msg = GUIGetMsg()
    Select
        Case $Msg = $GUI_EVENT_CLOSE
            ExitLoop
            
        Case $Clicked = 1
            $h_item = GUICtrlSendMsg($TreeView, $TVM_GETNEXTITEM, $TVGN_CARET, 0)
            If $h_item > 0 Then MsgBox(0, "Get Text", _GUICtrlTreeViewGetText($TreeView, $h_item))
            $Clicked = 0
    EndSelect
WEnd

Exit

Func TreeView_Click()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then    _DebugPrint ("TreeView_Click")
    ;----------------------------------------------------------------------------------------------
    $Clicked = 1
EndFunc   ;==>TreeView_Click

Func TreeView_DoubleClick()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then    _DebugPrint ("TreeView_DoubleClick")
    ;----------------------------------------------------------------------------------------------
EndFunc   ;==>TreeView_DoubleClick

; WM_NOTIFY event handler
Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event, $hwndFrom, $code, $idNew, $dwFlags
    $tagNMHDR = DllStructCreate("int;int;int;int", $lParam) ;NMHDR (hwndFrom, idFrom, code)
    If @error Then Return
    $event = DllStructGetData($tagNMHDR, 3)
    Select
        Case $wParam = $TreeView
            Select
                Case $event = $NM_CLICK
                    TreeView_Click()
                Case $event = $NM_DBLCLK
                    TreeView_DoubleClick()
                Case $event = $TVN_ITEMEXPANDINGA Or $event = $TVN_ITEMEXPANDINGW
                    _DebugPrint("$TVN_ITEMEXPANDING")
                    $Clicked = 0
                Case $event = $NM_RETURN Or $event = $NM_RCLICK Or $event = $NM_RDBLCLK Or $event = $NM_SETFOCUS Or $event = $NM_KILLFOCUS
                Case $event = $NM_OUTOFMEMORY Or $event = $NM_CUSTOMDRAW Or $event = $NM_HOVER Or $event = $NM_NCHITTEST Or $event = $NM_KEYDOWN Or $event = $NM_RELEASEDCAPTURE
                Case $event = $NM_SETCURSOR Or $event = $NM_CHAR Or $event = $NM_TOOLTIPSCREATED
                Case $event = $TVN_ITEMEXPANDEDA
                    _DebugPrint("Expanded")
            EndSelect
    EndSelect
    $tagNMHDR = 0
    $event = 0
    $lParam = 0
    ; Proceed the default Autoit3 internal message commands.
    ; You also can complete let the line out.
    ; !!! But only 'Return' (without any value) will not proceed
    ; the default Autoit3-message in the future !!!
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_Notify_Events

Func _DebugPrint($s_text)
    ConsoleWrite("!===========================================================" & @LF & _
            "+===========================================================" & @LF & _
            "-->" & $s_text & @LF & _
            "+===========================================================" & @LF)
EndFunc   ;==>_DebugPrint

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

How can I trap events "treeview item selected by keybord arrow keys", and do the same stuff then ""treeview item selected by mouse click"?

Thanks, Peppe

Another method to the madness:

(Left the extra stuff in)

#include <GUIConstants.au3>
#include <TreeViewConstants.au3>
#include <GuiTreeView.au3>
Opt("MustDeclareVars", 1)

Global Const $DebugIt = 1

Global Const $WM_NOTIFY = 0x004E
Global $TreeView = -999

Global $Clicked = 0

; ListView and TreeView Events
Global Const $NM_FIRST = 0
Global Const $NM_LAST = (-99)
Global Const $NM_OUTOFMEMORY = ($NM_FIRST - 1)
Global Const $NM_CLICK = ($NM_FIRST - 2)
Global Const $NM_DBLCLK = ($NM_FIRST - 3)
Global Const $NM_RETURN = ($NM_FIRST - 4)
Global Const $NM_RCLICK = ($NM_FIRST - 5)
Global Const $NM_RDBLCLK = ($NM_FIRST - 6)
Global Const $NM_SETFOCUS = ($NM_FIRST - 7)
Global Const $NM_KILLFOCUS = ($NM_FIRST - 8)
Global Const $NM_CUSTOMDRAW = ($NM_FIRST - 12)
Global Const $NM_HOVER = ($NM_FIRST - 13)
Global Const $NM_NCHITTEST = ($NM_FIRST - 14)
Global Const $NM_KEYDOWN = ($NM_FIRST - 15)
Global Const $NM_RELEASEDCAPTURE = ($NM_FIRST - 16)
Global Const $NM_SETCURSOR = ($NM_FIRST - 17)
Global Const $NM_CHAR = ($NM_FIRST - 18)
Global Const $NM_TOOLTIPSCREATED = ($NM_FIRST - 19)

; TreeView only events
;~ If Not IsDeclared('TV_FIRST') Then Global Const $TV_FIRST = 0x1100
;~ Global Const $TVIS_EXPANDED = 0x20
;~ Global Const $TVIS_EXPANDEDONCE = 0x40
Global Const $TVM_GETITEMSTATE = ($TV_FIRST + 39)
Global Const $TVN_FIRST = -400
Global Const $TVN_LAST = -499
Global Const $TVN_SELCHANGINGA = ($TVN_FIRST - 1)
Global Const $TVN_SELCHANGINGW = ($TVN_FIRST - 50)
Global Const $TVN_SELCHANGEDA = ($TVN_FIRST - 2)
Global Const $TVN_SELCHANGEDW = ($TVN_FIRST - 51)
Global Const $TVN_GETDISPINFOA = ($TVN_FIRST - 3)
Global Const $TVN_GETDISPINFOW = ($TVN_FIRST - 52)
Global Const $TVN_SETDISPINFOA = ($TVN_FIRST - 4)
Global Const $TVN_SETDISPINFOW = ($TVN_FIRST - 53)
Global Const $TVN_ITEMEXPANDINGA = ($TVN_FIRST - 5)
Global Const $TVN_ITEMEXPANDINGW = ($TVN_FIRST - 54)
Global Const $TVN_ITEMEXPANDEDA = ($TVN_FIRST - 6)
Global Const $TVN_ITEMEXPANDEDW = ($TVN_FIRST - 55)
Global Const $TVN_BEGINDRAGA = ($TVN_FIRST - 7)
Global Const $TVN_BEGINDRAGW = ($TVN_FIRST - 56)
Global Const $TVN_BEGINRDRAGA = ($TVN_FIRST - 8)
Global Const $TVN_BEGINRDRAGW = ($TVN_FIRST - 57)
Global Const $TVN_DELETEITEMA = ($TVN_FIRST - 9)
Global Const $TVN_DELETEITEMW = ($TVN_FIRST - 58)
Global Const $TVN_BEGINLABELEDITA = ($TVN_FIRST - 10)
Global Const $TVN_BEGINLABELEDITW = ($TVN_FIRST - 59)
Global Const $TVN_ENDLABELEDITA = ($TVN_FIRST - 11)
Global Const $TVN_ENDLABELEDITW = ($TVN_FIRST - 60)
Global Const $TVN_KEYDOWN = ($TVN_FIRST - 12)
Global Const $TVN_GETINFOTIPA = ($TVN_FIRST - 13)
Global Const $TVN_GETINFOTIPW = ($TVN_FIRST - 14)
Global Const $TVN_SINGLEEXPAND = ($TVN_FIRST - 15)
Dim $h_GUI, $Msg, $TreeView
Dim $h_root1, $h_root2, $h_root3, $h_item
Dim $n_btn_gettext

$h_GUI = GUICreate("TreeView UDF Sample", 220, 250)

$TreeView = GUICtrlCreateTreeView(10, 10, 200, 200, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetImage(-1, "shell32.dll", 3, 4)
GUICtrlSetImage(-1, "shell32.dll", 4, 2)

$h_root1 = _GUICtrlTreeViewInsertItem($TreeView, "RootItem1")
_GUICtrlTreeViewSetIcon($TreeView, $h_root1, "shell32.dll", 7)

_GUICtrlTreeViewInsertItem($TreeView, "SubItem1", $h_root1)
_GUICtrlTreeViewInsertItem($TreeView, "SubItem2", $h_root1)

$h_root2 = _GUICtrlTreeViewInsertItem($TreeView, "RootItem2")
_GUICtrlTreeViewSetIcon($TreeView, $h_root2, "shell32.dll", 12)

$h_root3 = _GUICtrlTreeViewInsertItem($TreeView, "RootItem3")
_GUICtrlTreeViewInsertItem($TreeView, "SubItem3", $h_root3)
_GUICtrlTreeViewInsertItem($TreeView, "SubItem4", $h_root3)

$n_btn_gettext = GUICtrlCreateButton("Get text", 10, 220, 200, 20)

GUISetState()

;Register WM_NOTIFY  events
GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

While 1
    $Msg = GUIGetMsg()
    Select
        Case $Msg = $GUI_EVENT_CLOSE
            ExitLoop
            
        Case $Clicked = 1
            $h_item = GUICtrlSendMsg($TreeView, $TVM_GETNEXTITEM, $TVGN_CARET, 0)
            If $h_item > 0 Then MsgBox(0, "Get Text", _GUICtrlTreeViewGetText($TreeView, $h_item))
            $Clicked = 0
    EndSelect
WEnd

Exit

Func TreeView_Click()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then    _DebugPrint ("TreeView_Click")
    ;----------------------------------------------------------------------------------------------
    $Clicked = 1
EndFunc   ;==>TreeView_Click

Func TreeView_DoubleClick()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then    _DebugPrint ("TreeView_DoubleClick")
    ;----------------------------------------------------------------------------------------------
EndFunc   ;==>TreeView_DoubleClick

; WM_NOTIFY event handler
Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event, $hwndFrom, $code, $idNew, $dwFlags
    $tagNMHDR = DllStructCreate("int;int;int;int", $lParam) ;NMHDR (hwndFrom, idFrom, code)
    If @error Then Return
    $event = DllStructGetData($tagNMHDR, 3)
    Select
        Case $wParam = $TreeView
            Select
                Case $event = $NM_CLICK
                    TreeView_Click()
                Case $event = $NM_DBLCLK
                    TreeView_DoubleClick()
                Case $event = $TVN_ITEMEXPANDINGA Or $event = $TVN_ITEMEXPANDINGW
                    _DebugPrint("$TVN_ITEMEXPANDING")
                    $Clicked = 0
                Case $event = $NM_RETURN Or $event = $NM_RCLICK Or $event = $NM_RDBLCLK Or $event = $NM_SETFOCUS Or $event = $NM_KILLFOCUS
                Case $event = $NM_OUTOFMEMORY Or $event = $NM_CUSTOMDRAW Or $event = $NM_HOVER Or $event = $NM_NCHITTEST Or $event = $NM_KEYDOWN Or $event = $NM_RELEASEDCAPTURE
                Case $event = $NM_SETCURSOR Or $event = $NM_CHAR Or $event = $NM_TOOLTIPSCREATED
                Case $event = $TVN_ITEMEXPANDEDA
                    _DebugPrint("Expanded")
            EndSelect
    EndSelect
    $tagNMHDR = 0
    $event = 0
    $lParam = 0
    ; Proceed the default Autoit3 internal message commands.
    ; You also can complete let the line out.
    ; !!! But only 'Return' (without any value) will not proceed
    ; the default Autoit3-message in the future !!!
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_Notify_Events

Func _DebugPrint($s_text)
    ConsoleWrite("!===========================================================" & @LF & _
            "+===========================================================" & @LF & _
            "-->" & $s_text & @LF & _
            "+===========================================================" & @LF)
EndFunc   ;==>_DebugPrint

Share this post


Link to post
Share on other sites

#include <GUIConstants.au3>
#include <TreeViewConstants.au3>
#include <GuiTreeView.au3>
Opt("MustDeclareVars", 1)

Global Const $DebugIt = 1

Global Const $WM_NOTIFY = 0x004E
Global $TreeView = -999

Global $Clicked = 0

; ListView and TreeView Events
Global Const $NM_FIRST = 0
Global Const $NM_LAST = (-99)
Global Const $NM_OUTOFMEMORY = ($NM_FIRST - 1)
Global Const $NM_CLICK = ($NM_FIRST - 2)
Global Const $NM_DBLCLK = ($NM_FIRST - 3)
Global Const $NM_RETURN = ($NM_FIRST - 4)
Global Const $NM_RCLICK = ($NM_FIRST - 5)
Global Const $NM_RDBLCLK = ($NM_FIRST - 6)
Global Const $NM_SETFOCUS = ($NM_FIRST - 7)
Global Const $NM_KILLFOCUS = ($NM_FIRST - 8)
Global Const $NM_CUSTOMDRAW = ($NM_FIRST - 12)
Global Const $NM_HOVER = ($NM_FIRST - 13)
Global Const $NM_NCHITTEST = ($NM_FIRST - 14)
Global Const $NM_KEYDOWN = ($NM_FIRST - 15)
Global Const $NM_RELEASEDCAPTURE = ($NM_FIRST - 16)
Global Const $NM_SETCURSOR = ($NM_FIRST - 17)
Global Const $NM_CHAR = ($NM_FIRST - 18)
Global Const $NM_TOOLTIPSCREATED = ($NM_FIRST - 19)

; TreeView only events
;~ If Not IsDeclared('TV_FIRST') Then Global Const $TV_FIRST = 0x1100
;~ Global Const $TVIS_EXPANDED = 0x20
;~ Global Const $TVIS_EXPANDEDONCE = 0x40
Global Const $TVM_GETITEMSTATE = ($TV_FIRST + 39)
Global Const $TVN_FIRST = -400
Global Const $TVN_LAST = -499
Global Const $TVN_SELCHANGINGA = ($TVN_FIRST - 1)
Global Const $TVN_SELCHANGINGW = ($TVN_FIRST - 50)
Global Const $TVN_SELCHANGEDA = ($TVN_FIRST - 2)
Global Const $TVN_SELCHANGEDW = ($TVN_FIRST - 51)
Global Const $TVN_GETDISPINFOA = ($TVN_FIRST - 3)
Global Const $TVN_GETDISPINFOW = ($TVN_FIRST - 52)
Global Const $TVN_SETDISPINFOA = ($TVN_FIRST - 4)
Global Const $TVN_SETDISPINFOW = ($TVN_FIRST - 53)
Global Const $TVN_ITEMEXPANDINGA = ($TVN_FIRST - 5)
Global Const $TVN_ITEMEXPANDINGW = ($TVN_FIRST - 54)
Global Const $TVN_ITEMEXPANDEDA = ($TVN_FIRST - 6)
Global Const $TVN_ITEMEXPANDEDW = ($TVN_FIRST - 55)
Global Const $TVN_BEGINDRAGA = ($TVN_FIRST - 7)
Global Const $TVN_BEGINDRAGW = ($TVN_FIRST - 56)
Global Const $TVN_BEGINRDRAGA = ($TVN_FIRST - 8)
Global Const $TVN_BEGINRDRAGW = ($TVN_FIRST - 57)
Global Const $TVN_DELETEITEMA = ($TVN_FIRST - 9)
Global Const $TVN_DELETEITEMW = ($TVN_FIRST - 58)
Global Const $TVN_BEGINLABELEDITA = ($TVN_FIRST - 10)
Global Const $TVN_BEGINLABELEDITW = ($TVN_FIRST - 59)
Global Const $TVN_ENDLABELEDITA = ($TVN_FIRST - 11)
Global Const $TVN_ENDLABELEDITW = ($TVN_FIRST - 60)
Global Const $TVN_KEYDOWN = ($TVN_FIRST - 12)
Global Const $TVN_GETINFOTIPA = ($TVN_FIRST - 13)
Global Const $TVN_GETINFOTIPW = ($TVN_FIRST - 14)
Global Const $TVN_SINGLEEXPAND = ($TVN_FIRST - 15)
Dim $h_GUI, $Msg, $TreeView
Dim $h_root1, $h_root2, $h_root3, $h_item
Dim $n_btn_gettext

$h_GUI = GUICreate("TreeView UDF Sample", 220, 250)

$TreeView = GUICtrlCreateTreeView(10, 10, 200, 200, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetImage(-1, "shell32.dll", 3, 4)
GUICtrlSetImage(-1, "shell32.dll", 4, 2)

$h_root1 = _GUICtrlTreeViewInsertItem($TreeView, "RootItem1")
_GUICtrlTreeViewSetIcon($TreeView, $h_root1, "shell32.dll", 7)

_GUICtrlTreeViewInsertItem($TreeView, "SubItem1", $h_root1)
_GUICtrlTreeViewInsertItem($TreeView, "SubItem2", $h_root1)

$h_root2 = _GUICtrlTreeViewInsertItem($TreeView, "RootItem2")
_GUICtrlTreeViewSetIcon($TreeView, $h_root2, "shell32.dll", 12)

$h_root3 = _GUICtrlTreeViewInsertItem($TreeView, "RootItem3")
_GUICtrlTreeViewInsertItem($TreeView, "SubItem3", $h_root3)
_GUICtrlTreeViewInsertItem($TreeView, "SubItem4", $h_root3)

$n_btn_gettext = GUICtrlCreateButton("Get text", 10, 220, 200, 20)

GUISetState()

;Register WM_NOTIFY  events
GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

While 1
    $Msg = GUIGetMsg()
    Select
        Case $Msg = $GUI_EVENT_CLOSE
            ExitLoop
            
        Case $Clicked = 1
            $h_item = GUICtrlSendMsg($TreeView, $TVM_GETNEXTITEM, $TVGN_CARET, 0)
            If $h_item > 0 Then MsgBox(0, "Get Text", _GUICtrlTreeViewGetText($TreeView, $h_item))
            $Clicked = 0
    EndSelect
WEnd

Exit

Func TreeView_Click()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then    _DebugPrint ("TreeView_Click")
    ;----------------------------------------------------------------------------------------------
    $Clicked = 1
EndFunc   ;==>TreeView_Click

Func TreeView_DoubleClick()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then    _DebugPrint ("TreeView_DoubleClick")
    ;----------------------------------------------------------------------------------------------
EndFunc   ;==>TreeView_DoubleClick

Func TreeView_SELCHANGED()
    ;----------------------------------------------------------------------------------------------
    If $DebugIt Then    _DebugPrint ("TreeView_SELCHANGED")
    ;----------------------------------------------------------------------------------------------
    $Clicked = 1
EndFunc
 
; WM_NOTIFY event handler
Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event, $hwndFrom, $code, $idNew, $dwFlags
    $tagNMHDR = DllStructCreate("int;int;int;int", $lParam) ;NMHDR (hwndFrom, idFrom, code)
    If @error Then Return
    $event = DllStructGetData($tagNMHDR, 3)
    Select
        Case $wParam = $TreeView
            Select
                Case $event = $NM_CLICK
                    TreeView_Click()
                Case $event = $NM_DBLCLK
                    TreeView_DoubleClick()
                Case $event = $TVN_ITEMEXPANDINGA Or $event = $TVN_ITEMEXPANDINGW
                    _DebugPrint("$TVN_ITEMEXPANDING")
                    $Clicked = 0
                Case $event = $NM_RETURN Or $event = $NM_RCLICK Or $event = $NM_RDBLCLK Or $event = $NM_SETFOCUS Or $event = $NM_KILLFOCUS
                Case $event = $NM_OUTOFMEMORY Or $event = $NM_CUSTOMDRAW Or $event = $NM_HOVER Or $event = $NM_NCHITTEST Or $event = $NM_KEYDOWN Or $event = $NM_RELEASEDCAPTURE
                Case $event = $NM_SETCURSOR Or $event = $NM_CHAR Or $event = $NM_TOOLTIPSCREATED
                Case $event = $TVN_ITEMEXPANDEDA
                    _DebugPrint("Expanded")
                    Case $TVN_SELCHANGEDA Or $TVN_SELCHANGEDW
                          TreeView_SELCHANGED()
            EndSelect
    EndSelect
    $tagNMHDR = 0
    $event = 0
    $lParam = 0
    ; Proceed the default Autoit3 internal message commands.
    ; You also can complete let the line out.
    ; !!! But only 'Return' (without any value) will not proceed
    ; the default Autoit3-message in the future !!!
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_Notify_Events

Func _DebugPrint($s_text)
    ConsoleWrite("!===========================================================" & @LF & _
            "+===========================================================" & @LF & _
            "-->" & $s_text & @LF & _
            "+===========================================================" & @LF)
EndFunc   ;==>_DebugPrint


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

How can I trap events "treeview item selected by keybord arrow keys", and do the same stuff then ""treeview item selected by mouse click"?

Thanks, Peppe

Change this part of the script shown in the question I replied to:

Select
        Case $wParam = $TreeView
            Select
                Case $event = $NM_CLICK
                    TreeView_Click()
                Case $event = $NM_DBLCLK
                    TreeView_DoubleClick()
                Case $event = $TVN_SELCHANGEDW
                    TreeView_Click()
                Case $event = $TVN_ITEMEXPANDINGA Or $event = $TVN_ITEMEXPANDINGW
                    _DebugPrint("$TVN_ITEMEXPANDING")
                    $Clicked = 0
                Case $event = $NM_RETURN Or $event = $NM_RCLICK Or $event = $NM_RDBLCLK Or $event = $NM_SETFOCUS Or $event = $NM_KILLFOCUS
                Case $event = $NM_OUTOFMEMORY Or $event = $NM_CUSTOMDRAW Or $event = $NM_HOVER Or $event = $NM_NCHITTEST Or $event = $NM_KEYDOWN Or $event = $NM_RELEASEDCAPTURE
                Case $event = $NM_SETCURSOR Or $event = $NM_CHAR Or $event = $NM_TOOLTIPSCREATED
                Case $event = $TVN_ITEMEXPANDEDA
                    _DebugPrint("Expanded")
            EndSelect
    EndSelect

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

It works perfectly.

Thank you very much.

Peppe

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