Sign in to follow this  
Followers 0
MISIIM

TreeView On Select Item

9 posts in this topic

I need to get an event when an item in a treeview is selected. Do I have to do this with GUIRegisterMsg? Is there a better way?

Thanks.

Share this post


Link to post
Share on other sites



I need to get an event when an item in a treeview is selected. Do I have to do this with GUIRegisterMsg? Is there a better way?

Thanks.

I imagine that GUIRegisterMsg would probably be the best way.

Using the Beta you should be able to use the events to your advantage by modifying this from the 3.2.9.3 help:

#include <GuiConstants.au3>
#include <GuiTreeView.au3>
#include <GuiImageList.au3>

Opt('MustDeclareVars', 1)

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

Global $hTreeView

Example_Internal()
Example_External()

Func Example_Internal()

    Local $hItem[10], $iRand
    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    GUICreate("(Internal) TreeView Get Selection", 400, 300)

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

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    _GUICtrlTreeView_BeginUpdate ($hTreeView)
    For $x = 0 To 9
        $hItem[$x] = GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Item", $x), $hTreeView)
        For $y = 1 To Random(2, 10, 1)
            GUICtrlCreateTreeViewItem(StringFormat("[%02d] New Child", $y), $hItem[$x])
        Next
    Next
    _GUICtrlTreeView_EndUpdate ($hTreeView)
    
    $iRand = Random(0, 9, 1)
    _GUICtrlTreeView_SelectItem ($hTreeView, $hItem[$iRand])
    MsgBox(4160, "Information", "Selection? " & _GUICtrlTreeView_GetSelection ($hTreeView))

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

Func Example_External()

    Local $GUI, $hItem[10], $hImage, $iImage, $iRand
    Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES)
    $GUI = GUICreate("(External) TreeView Get Selection", 400, 300)

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

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    $hImage = _GUIImageList_Create (16, 16, 5, 3)
    _GUIImageList_AddIcon ($hImage, "shell32.dll", 110)
    _GUIImageList_AddIcon ($hImage, "shell32.dll", 131)
    _GUIImageList_AddIcon ($hImage, "shell32.dll", 165)
    _GUIImageList_AddIcon ($hImage, "shell32.dll", 168)
    _GUIImageList_AddIcon ($hImage, "shell32.dll", 137)
    _GUIImageList_AddIcon ($hImage, "shell32.dll", 146)
    _GUICtrlTreeView_SetNormalImageList ($hTreeView, $hImage)

    _GUICtrlTreeView_BeginUpdate ($hTreeView)
    For $x = 0 To 9
        $iImage = Random(0, 5, 1)
        $hItem[$x] = _GUICtrlTreeView_Add ($hTreeView, 0, StringFormat("[%02d] New Item", $x), $iImage, $iImage)
        For $y = 1 To Random(2, 10, 1)
            $iImage = Random(0, 5, 1)
            _GUICtrlTreeView_AddChild ($hTreeView, $hItem[$x], StringFormat("[%02d] New Child", $y), $iImage, $iImage)
        Next
    Next
    _GUICtrlTreeView_EndUpdate ($hTreeView)
    
    $iRand = Random(0, 9, 1)
    _GUICtrlTreeView_SelectItem ($hTreeView, $hItem[$iRand])
    MsgBox(4160, "Information", "Selection? " & _GUICtrlTreeView_GetSelection ($hTreeView))

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

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

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

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

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

#3 ·  Posted (edited)

this is complicated..how to get simple GUI with treeview and one selected item ?

pls help <_<

Edited by n3nE

[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites

Here's a treeview example app launcher for some common windows xp apps using non beta autoit.

(Note this uses default hard paths to some windows apps, so expect errors if the apps paths don't exist on your pc)

#include<GUIConstants.au3>

Opt("GUIOnEventMode", 1)

Global $Item[5]

$Item[0] = StringSplit("Accessibility|Games|Programs|System", "|")
$Item[1] = StringSplit("magnify.exe|narrator.exe|osk.exe", "|") 
$Item[2] = StringSplit("freecell.exe|winmine.exe|sol.exe", "|")
$Item[3] = StringSplit("calc.exe|mspaint.exe|notepad.exe", "|") 
$Item[4] = StringSplit("cmd.exe|msconfig.exe|regedt32.exe", "|")


$Gui = GuiCreate(":-)", 200, 300)
$Treeview = GUICtrlCreateTreeView(5,5, 190, 290)
$SSR = StringSplit($Item[0], "|")
$Root = $Item[0]
For $i = 1 To $Root[0]
    $tvi = GUICtrlCreateTreeViewItem($Root[$i] , $Treeview)
    GUICtrlSetImage(-1, "shell32.dll", -37, 4)
    GUICtrlSetImage(-1, "shell32.dll", -37, 2)
    $Sub = $Item[$i]
    For $j = 1 To $Sub[0]
        GUICtrlCreateTreeViewItem(StringTrimRight($Sub[$j], 4), $tvi)
        GUICtrlSetOnEvent(-1, "Event")
        GUICtrlSetImage(-1, $Sub[$j], -1, 4)
        GUICtrlSetImage(-1, $Sub[$j], -1, 2)        
    Next
Next    
GUISetOnEvent($GUI_EVENT_CLOSE, "Event", $Gui)
GUISetState(@SW_SHOW, $Gui)

While 1
    Sleep(100)
WEnd

Func Event()
    $tID = @GUI_CtrlId
    Switch $tID
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            If FileExists(@SystemDir & "\" & GuiCtrlRead($Treeview, 1) & ".exe") Then _
            ShellExecute(GuiCtrlRead($Treeview, 1) & ".exe")
    EndSwitch
EndFunc

Share this post


Link to post
Share on other sites

That is without errors but I want to select item with button <_<

#include<GUIConstants.au3>

Opt("GUIOnEventMode", 1)

Global $Item[5]

$Item[0] = StringSplit("Accessibility|Games|Programs|System", "|")

$Item[1] = StringSplit("magnify.exe|narrator.exe|osk.exe", "|")

$Item[2] = StringSplit("freecell.exe|winmine.exe|sol.exe", "|")

$Item[3] = StringSplit("calc.exe|mspaint.exe|notepad.exe", "|")

$Item[4] = StringSplit("cmd.exe|msconfig.exe|regedt32.exe", "|")

$Gui = GuiCreate(":-)", 200, 400)

$button = GUICtrlCreateButton("Select item",60,300,100,20)

$Treeview = GUICtrlCreateTreeView(5,5, 190, 290)

$SSR = StringSplit($Item[0], "|")

$Root = $Item[0]

For $i = 1 To $Root[0]

$tvi = GUICtrlCreateTreeViewItem($Root[$i] , $Treeview)

GUICtrlSetImage(-1, "shell32.dll", -37, 4)

GUICtrlSetImage(-1, "shell32.dll", -37, 2)

$Sub = $Item[$i]

For $j = 1 To $Sub[0]

GUICtrlCreateTreeViewItem(StringTrimRight($Sub[$j], 4), $tvi)

GUICtrlSetOnEvent(-1, "Event")

GUICtrlSetImage(-1, $Sub[$j], -1, 4)

GUICtrlSetImage(-1, $Sub[$j], -1, 2)

Next

Next

GUISetOnEvent($GUI_EVENT_CLOSE, "Event", $Gui)

GUISetState(@SW_SHOW, $Gui)

While 1

Sleep(100)

WEnd

Func Event()

$tID = @GUI_CtrlId

Switch $tID

Case $GUI_EVENT_CLOSE

Exit

Case Else

If FileExists(@SystemDir & "\" & GuiCtrlRead($Treeview, 1) & ".exe") Then _

ShellExecute(GuiCtrlRead($Treeview, 1) & ".exe")

EndSwitch

EndFunc


[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites

Same thing as before but added a combobox and launch button..

Select a treeview item from the combo box and it sets focus to the treeview item.

Surely you can work out how to apply the same logic of selecting a treeview item by using a button instead of a combo box.

Note you will need to run it with AutoIt Beta v3.2.9.3 or higher

#include <GUIConstants.au3>
#Include <GuiTreeView.au3>

Opt("GUIOnEventMode", 1)

Global $Item[5]
$Item[0] = 4
$Item[1] = "Accessibility|magnify.exe|narrator.exe|osk.exe"
$Item[2] = "Games|freecell.exe|winmine.exe|sol.exe"
$Item[3] = "Programs|calc.exe|mspaint.exe|notepad.exe"
$Item[4] = "System|cmd.exe|msconfig.exe|regedt32.exe"

$Gui = GUICreate(":-)", 200, 350, -1, -1)
$Trv = GUICtrlCreateTreeView(10, 10, 180, 290)
$Com = GUICtrlCreateCombo("", 65, 315, 85, 20, $CBS_DROPDOWNLIST)
GUICtrlSetOnEvent(-1, "Event")
For $i = 1 To $Item[0]
    Dim $SST = StringSplit($Item[$i], "|")
    Local $Tvi = GUICtrlCreateTreeViewItem($SST[1] , $Trv)
    GUICtrlSetImage(-1, "shell32.dll", -37, 4)
    GUICtrlSetImage(-1, "shell32.dll", -37, 2)
    GUICtrlSetOnEvent(-1, "Event")
    GUICtrlSetData($Com, $SST[1])
    For $j = 2 To $SST[0]
        GUICtrlCreateTreeViewItem(StringTrimRight($SST[$j], 4), $Tvi)
        GUICtrlSetOnEvent(-1, "Event")
        GUICtrlSetImage(-1, $SST[$j], -1, 4)
        GUICtrlSetImage(-1, $SST[$j], -1, 2)
        GUICtrlSetData($Com, StringTrimRight($SST[$j], 4))
    Next
Next
$Ico = GUICtrlCreateIcon("shell32.dll", -37, 160, 315, 20, 20)
GUICtrlSetState(-1, $GUI_DISABLE)
$But = GUICtrlCreateButton("   Launch ", 10, 305, 180, 40, $WS_CLIPSIBLINGS + $BS_LEFT)
GUICtrlSetOnEvent(-1, "Event")
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetOnEvent($GUI_EVENT_CLOSE, "Event", $Gui)
GUISetState()

While 1
    Sleep(100)
WEnd

Func Event()
    $cID = @GUI_CtrlId
    Switch $cID
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Com
            GUICtrlSetState($Trv, $GUI_FOCUS)
            _GUICtrlTreeView_SelectItem($Trv, _GUICtrlTreeView_FindItem($Trv, GUICtrlRead($Com)), $TVGN_CARET)
        Case $But
            If FileExists(@SystemDir & "\" & GuiCtrlRead($Trv, 1) & ".exe") Then _
            ShellExecute(GuiCtrlRead($Trv, 1) & ".exe")         
        Case Else
            GUICtrlSetData($Com, GUICtrlRead($Trv, 1))
            For $i = 1 To $Item[0]
                Dim $SST = StringSplit($Item[$i], "|")
                If $SST[1] = GUICtrlRead($Trv, 1) Then 
                    GUICtrlSetImage($Ico, "shell32.dll", -37)
                    GUICtrlSetState($But, $GUI_DISABLE)
                    Return
                EndIf
            Next
            GUICtrlSetImage($Ico, GuiCtrlRead($Trv, 1) & ".exe", -1)
            GUICtrlSetState($But, $GUI_ENABLE)
            Return          
    EndSwitch
EndFunc
Cheers

Share this post


Link to post
Share on other sites

That's great man...I have one more question, is possible to make like this script but with GUICtrlCreateEdit- if type cmd to be selected that item ???


[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

Share this post


Link to post
Share on other sites

Same thing as before but added a combobox and launch button..

Select a treeview item from the combo box and it sets focus to the treeview item.

Surely you can work out how to apply the same logic of selecting a treeview item by using a button instead of a combo box.

Note you will need to run it with AutoIt Beta v3.2.9.3 or higher

You'll end writing this persons script one question at a time.


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

I am newbie, i must ask for help <_<


[quote name='dbzfanatic' post='609696' date='Nov 26 2008, 08:46 AM']This is a help forum not a "write this for me" forum.[/quote](Sorry for bad English) :)

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