Jump to content

_GUICtrlTreeView_GetSelection help


atzoref
 Share

Recommended Posts

  • Moderators

atzoref,

I would do it like this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>

; Set flag
$fFlag = False

; Create handle array
Global $aItem_Handles[16] = [15]

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

$hTreeView = _GUICtrlTreeView_Create($hGUI, 10, 10, 200, 300, BitOR($TVS_HASLINES, $TVS_LINESATROOT, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

GUISetState()

; Fill Treeview
_GUICtrlTreeView_BeginUpdate($hTreeView)
$iOverview = _GUICtrlTreeView_Add($hTreeView, 0, "Overview")

For $i = 1 To 15
    $aItem_Handles[$i] = _GUICtrlTreeView_AddChild($hTreeView, $iOverview, "Item " & $i)
Next
_GUICtrlTreeView_EndUpdate($hTreeView)
_GUICtrlTreeView_Expand($hTreeView)

; Register message
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

; Loop
While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

WEnd

; Intercept the NOTIFY leassages
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    ; Read the data
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    ; See if it was our treeview
    Switch $hWndFrom
        Case $hTreeview
            ; Work out which item is under cursor
            $aPos = GUIGetCursorInfo($hGUI)
            $iIndex = _GUICtrlTreeView_HitTestItem($hTreeview, $aPos[0] - 10, $aPos[1] - 10)
            ; Look for code
            Switch $iCode
                Case $NM_CLICK
                    ; Get item text
                    For $i = 0 To $aItem_Handles[0]
                        If $aItem_Handles[$i] = $iIndex Then
                            ConsoleWrite("You clicked: " & _GUICtrlTreeView_GetText($hTreeview, $aItem_Handles[$i]) & @CRLF)
                            ExitLoop
                        EndIf
                    Next
                Case $NM_RCLICK
                    ; Click on that item
                    For $i = 0 To $aItem_Handles[0]
                        If $aItem_Handles[$i] = $iIndex Then
                            _GUICtrlTreeView_ClickItem($hTreeview, $aItem_Handles[$i])
                            ExitLoop
                        EndIf
                    Next
            EndSwitch
    EndSwitch
EndFunc

Any help? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

This is the only way?

It seems to be a little complicate for such small thing....

I just want when to use "ContextMenu" with the right click of the mouse on an item, so it change the GetSelection function

like it works when left click is pressed on the item.

Link to comment
Share on other sites

  • Moderators

atzoref,

It seems to be a little complicate for such small thing....

Often the way alas. ;)

Here is a script which both selects the item and shows a context menu when you right click on it: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <GuiMenu.au3>

; Create array for the context menu
Global $aButtons[4]

; Create handle array
Global $aItem_Handles[16] = [15]

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

$hTreeView = _GUICtrlTreeView_Create($hGUI, 10, 10, 200, 300, BitOR($TVS_HASLINES, $TVS_LINESATROOT, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

GUISetState()

; Fill Treeview
_GUICtrlTreeView_BeginUpdate($hTreeView)
$iOverview = _GUICtrlTreeView_Add($hTreeView, 0, "Overview")

For $i = 1 To 15
    $aItem_Handles[$i] = _GUICtrlTreeView_AddChild($hTreeView, $iOverview, "Item " & $i)
Next
_GUICtrlTreeView_EndUpdate($hTreeView)
_GUICtrlTreeView_Expand($hTreeView)

$hDummy = GUICtrlCreateDummy()
$mContext = GUICtrlCreateContextMenu($hDummy)
$hMenu = GUICtrlGetHandle($mContext)
$aButtons[1] = GUICtrlCreateMenuItem("Test 1", $mContext)
$aButtons[2] = GUICtrlCreateMenuItem("Test 2", $mContext)
$aButtons[3] = GUICtrlCreateMenuItem("Test 3", $mContext)

; Register message
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

; Loop
While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $aButtons[1]
            ConsoleWrite("Context Menu Item 1" & @LF)
        Case $aButtons[2]
            ConsoleWrite("Context Menu Item 2" & @LF)
        Case $aButtons[3]
            ConsoleWrite("Context Menu Item 3" & @LF)
    EndSwitch

WEnd

; Intercept the NOTIFY leassages
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    ; Clear the rightclick flag
    Static $fRtClick = False
    ; Read the data
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    ; See if it was our treeview
    Switch $hWndFrom
        Case $hTreeview
            ; Work out which item is under cursor
            $aPos = GUIGetCursorInfo($hGUI)
            $iIndex = _GUICtrlTreeView_HitTestItem($hTreeview, $aPos[0] - 10, $aPos[1] - 10)
            ; Look for code
            Switch $iCode
                Case $NM_CLICK
                    ; Get item text
                    For $i = 0 To $aItem_Handles[0]
                        If $aItem_Handles[$i] = $iIndex Then
                            ConsoleWrite("You clicked: " & _GUICtrlTreeView_GetText($hTreeview, $aItem_Handles[$i]) & @CRLF)
                            ExitLoop
                        EndIf
                    Next
                    If $fRtClick Then
                        $fRtClick = False
                        _GUICtrlMenu_TrackPopupMenu($hMenu, $hGUI)
                    EndIf
                Case $NM_RCLICK
                    ; Click on that item
                    For $i = 0 To $aItem_Handles[0]
                        If $aItem_Handles[$i] = $iIndex Then
                            _GUICtrlTreeView_ClickItem($hTreeview, $aItem_Handles[$i])
                            $fRtClick = True
                            ExitLoop
                        EndIf
                    Next
            EndSwitch
    EndSwitch
EndFunc

I hope that solves your problem. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

atzoref,

it still not change the state of the (GetSelection) with the right click on a item in the tree

Yes it does, as this code shows: ;)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <GuiMenu.au3>

; Create array for the context menu
Global $aButtons[4]

; Create handle array
Global $aItem_Handles[16] = [15]

; Create a flag to show when an item has been clicked
Global $fClick = False

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

$hTreeView = _GUICtrlTreeView_Create($hGUI, 10, 10, 200, 300, BitOR($TVS_HASLINES, $TVS_LINESATROOT, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

GUISetState()

; Fill Treeview
_GUICtrlTreeView_BeginUpdate($hTreeView)
$iOverview = _GUICtrlTreeView_Add($hTreeView, 0, "Overview")

For $i = 1 To 15
    $aItem_Handles[$i] = _GUICtrlTreeView_AddChild($hTreeView, $iOverview, "Item " & $i)
Next
_GUICtrlTreeView_EndUpdate($hTreeView)
_GUICtrlTreeView_Expand($hTreeView)

$hDummy = GUICtrlCreateDummy()
$mContext = GUICtrlCreateContextMenu($hDummy)
$hMenu = GUICtrlGetHandle($mContext)
$aButtons[1] = GUICtrlCreateMenuItem("Test 1", $mContext)
$aButtons[2] = GUICtrlCreateMenuItem("Test 2", $mContext)
$aButtons[3] = GUICtrlCreateMenuItem("Test 3", $mContext)

; Register message
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

; Loop
While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $aButtons[1]
            ConsoleWrite("Context Menu 1" & @LF)
        Case $aButtons[2]
            ConsoleWrite("Context Menu 2" & @LF)
        Case $aButtons[3]
            ConsoleWrite("Context Menu 3" & @LF)
    EndSwitch

    ; Test which item is selected at this point
    If $fClick Then
        $hItem = _GUICtrlTreeView_GetSelection($hTreeView)
        ConsoleWrite("Item Selected: " & _GUICtrlTreeView_GetText($hTreeview, $hItem) & @CRLF)
        $fClick = False
    EndIf

WEnd

; Intercept the NOTIFY leassages
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    ; Clear the rightclick flag
    Static $fRtClick = False
    ; Read the data
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    ; See if it was our treeview
    Switch $hWndFrom
        Case $hTreeview
            ; Work out which item is under cursor
            $aPos = GUIGetCursorInfo($hGUI)
            $iIndex = _GUICtrlTreeView_HitTestItem($hTreeview, $aPos[0] - 10, $aPos[1] - 10)
            ; Look for code
            Switch $iCode
                Case $NM_CLICK

                    ; Get item text
                    For $i = 0 To $aItem_Handles[0]
                        If $aItem_Handles[$i] = $iIndex Then
                            ConsoleWrite("You clicked: " & _GUICtrlTreeView_GetText($hTreeview, $aItem_Handles[$i]) & @CRLF)
                            If $fRtClick Then
                                $fRtClick = False
                                _GUICtrlMenu_TrackPopupMenu($hMenu, $hGUI)
                            EndIf
                            ; Set the flag to show we have clcked an item
                            $fClick = True
                            ExitLoop
                        EndIf
                    Next
                Case $NM_RCLICK
                    ; Click on that item
                    For $i = 0 To $aItem_Handles[0]
                        If $aItem_Handles[$i] = $iIndex Then
                            _GUICtrlTreeView_ClickItem($hTreeview, $aItem_Handles[$i])
                            $fRtClick = True
                            ExitLoop
                        EndIf
                    Next
            EndSwitch
    EndSwitch
EndFunc

When I runn that script and rightclick on an item, the code tells me that the item is indeed selected. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

In your last example th first time you click with the Right on an item,

the selection still jump to the previous selection, only from the second press is stable on the selection.

I try to implement your code in my script but I get:

Array variable has incorrect number of subscripts or subscript dimension range exceeded. - on the "IF" line.

Case $NM_RCLICK
; Click on that item
For $i = 0 To $aItem_Handles[0]
 If $aItem_Handles[$i] = $iIndex Then

I have an array with 2 coloums, when coloum [0] is with the handles ,it is something with the range.

What it is means?

; Create handle array
Global $aItem_Handles[16] = [15]
Edited by atzoref
Link to comment
Share on other sites

  • Moderators

atzoref,

I just want when to use "ContextMenu" with the right click of the mouse on an item, so it change the GetSelection function like it works when left click is pressed on the item

That is what you asked for and that is what the code above does. If you remove the context menu code, then you can see that it takes just the one click of either button to select the item. ;)

#include <GUIConstants.au3>
#include <windowsconstants.au3>
#include <guitreeview.au3>
#include <guimenu.au3>

; Create array for the context menu
Global $aButtons[4]

; Create handle array
Global $aItem_Handles[16] = [15]

; Create a flag to show when an item has been clicked
Global $fClick = False

; Create GUI
$hGUI = GUICreate("Test", 500, 500)

$hTreeView = _GUICtrlTreeView_Create($hGUI, 10, 10, 200, 300, BitOR($TVS_HASLINES, $TVS_LINESATROOT, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

GUISetState()

; Fill Treeview
_GUICtrlTreeView_BeginUpdate($hTreeView)
$iOverview = _GUICtrlTreeView_Add($hTreeView, 0, "Overview")

For $i = 1 To 15
    $aItem_Handles[$i] = _GUICtrlTreeView_AddChild($hTreeView, $iOverview, "Item " & $i)
Next
_GUICtrlTreeView_EndUpdate($hTreeView)
_GUICtrlTreeView_Expand($hTreeView)

$hDummy = GUICtrlCreateDummy()
$mContext = GUICtrlCreateContextMenu($hDummy)
$hMenu = GUICtrlGetHandle($mContext)
$aButtons[1] = GUICtrlCreateMenuItem("Test 1", $mContext)
$aButtons[2] = GUICtrlCreateMenuItem("Test 2", $mContext)
$aButtons[3] = GUICtrlCreateMenuItem("Test 3", $mContext)

; Register message
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

; Loop
While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $aButtons[1]
            ConsoleWrite("Context Menu 1" & @LF)
        Case $aButtons[2]
            ConsoleWrite("Context Menu 2" & @LF)
        Case $aButtons[3]
            ConsoleWrite("Context Menu 3" & @LF)
    EndSwitch

    ; Test which item is selected at this point
    If $fClick Then
        $hItem = _GUICtrlTreeView_GetSelection($hTreeView)
        ConsoleWrite("Item Selected: " & _GUICtrlTreeView_GetText($hTreeview, $hItem) & @CRLF)
        $fClick = False
    EndIf

WEnd

; Intercept the NOTIFY leassages
Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    ; Clear the rightclick flag
    Static $fRtClick = False
    ; Read the data
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    ; See if it was our treeview
    Switch $hWndFrom
        Case $hTreeview
            ; Work out which item is under cursor
            $aPos = GUIGetCursorInfo($hGUI)
            $iIndex = _GUICtrlTreeView_HitTestItem($hTreeview, $aPos[0] - 10, $aPos[1] - 10)
            ; Look for code
            Switch $iCode
                Case $NM_CLICK

                    ; Get item text
                    For $i = 0 To $aItem_Handles[0]
                        If $aItem_Handles[$i] = $iIndex Then
                            If $fRtClick Then
                                ConsoleWrite("You right clicked: " & _GUICtrlTreeView_GetText($hTreeview, $aItem_Handles[$i]) & @CRLF)
                                $fRtClick = False
                            Else
                                ConsoleWrite("You left clicked: " & _GUICtrlTreeView_GetText($hTreeview, $aItem_Handles[$i]) & @CRLF)
                            EndIf
                            ; Set the flag to show we have clicked an item
                            $fClick = True
                            ExitLoop
                        EndIf
                    Next
                Case $NM_RCLICK
                    ; Click on that item
                    For $i = 0 To $aItem_Handles[0]
                        If $aItem_Handles[$i] = $iIndex Then
                            _GUICtrlTreeView_ClickItem($hTreeview, $aItem_Handles[$i])
                            $fRtClick = True
                            ExitLoop
                        EndIf
                    Next
            EndSwitch
    EndSwitch
EndFunc

Happy now? :)

M23

Edited by Melba23
Added missing include

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

maba,

Thanks for spotting that - I have added it in the code above. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...