Jump to content

Treeview Context Menu Selected Item


Recommended Posts

Hello Together,

I created an Treeview and added an Context Menu to interacte with an Item.  But after an Item was klicked with the right Mousebutton, the funktion

_GUICtrlTreeView_GetSelection($gTreeview))

only returns "0x000000".

If I open an MsgBox, and close this, the Funktion will give the right handle. Is there a way to fix this?

Case $gTreeviewContextNew
    MsgBox(0, "Stupid way to do this", "Close this", 1)
    MsgBox(0, "Stupid way to do this", _GUICtrlTreeView_GetSelection($gTreeview))

Thank you.

Link to comment
Share on other sites

Your code does not help to solve anything.

Somewhere in this forum there is a treeview with right click menu code.

Here is a copy of it:

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

Opt('MustDeclareVars', 1)

Global $cTreeView, $tNMHDR, $hWndFrom, $iIDFrom, $iCode

_Main()

Func _Main()

    Local $myArray[10][2], $nMsg, $iIndex, $sText, $Char
    Local $iTVStyles =  BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, _
    $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_Checkboxes)
    Local $mainform = GUICreate("TreeView", 400, 300)
    $cTreeView = GUICtrlCreateTreeView(10, 10, 380, 280, $iTVStyles, $WS_EX_CLIENTEDGE)

    Local $var = GUICtrlCreateContextMenu($cTreeView)
    Local $preview = GUICtrlCreateMenuItem("Preview", $var)

    For $i = 1 To UBound($myArray) - 1
        $Char = Chr(64 + $i)
        $myArray[$i][0] = "This element contains " & $Char
        $myArray[$i][1] = GUICtrlCreateTreeViewItem($myArray[$i][0], $cTreeView)
    Next

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $preview
                $iIndex = GUICtrlRead($cTreeView)
                $sText = GUICtrlRead($cTreeView, 1);_GUICtrlTreeView_GetText($hTreeView, $iIndex)
                MsgBox(0, "Preview", "Index: " & $iIndex & @CRLF & "ItemText: " & $sText)
        EndSwitch
    WEnd

EndFunc   ;==>_Main


Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $iIDFrom
        Case $cTreeView
            Switch $iCode
                Case $NM_RCLICK
                    Local $tPoint = _WinAPI_GetMousePos(True, $hWndFrom), $tHitTest
                    $tHitTest = _GUICtrlTreeView_HitTestEx($hWndFrom, DllStructGetData($tPoint, 1), DllStructGetData($tPoint, 2))
                    If BitAND(DllStructGetData($tHitTest, "Flags"), $TVHT_ONITEM) Then
                        _GUICtrlTreeView_SelectItem($hWndFrom, DllStructGetData($tHitTest, 'Item'))
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

 

Edited by Dan_555

Some of my script sourcecode

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