Sign in to follow this  
Followers 0
NiVZ

Right click context menu for Treeview items?

8 posts in this topic

#1 ·  Posted (edited)

Hello,

I've pasted some code below.

What I want to do is add a context menu, so when you right click on any of the items in the treeview a menu should pop-up saying 'Preview'. If you click on Preview I want it to MsgBox the Handle of the menu item that was clicked.

Can anyone advise me on the easiest way to do this?

I've looked at GUICtrlCreateContextMenu and GUIRegisterMsg()

Thanks,

NiVZ

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

$mainform = GUICreate("TreeView", 400, 300)

$TreeView = GUICtrlCreateTreeView(10, 10, 380, 280, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_Checkboxes), $WS_EX_CLIENTEDGE)

Dim $myArray[10][2]

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

GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    
WEnd
Edited by NiVZ

Share this post


Link to post
Share on other sites



Hi,

something like this:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>
#Include <GuiTreeView.au3> ; only needed if you want to use _GUICtrlTreeView_GetText

$mainform = GUICreate("TreeView", 400, 300)

$TreeView = GUICtrlCreateTreeView(10, 10, 380, 280, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_Checkboxes), $WS_EX_CLIENTEDGE)
$var = GUICtrlCreateContextMenu($TreeView)
$preview = GUICtrlCreateMenuItem("Preview", $var)

Dim $myArray[10][2]

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

GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $preview
            MsgBox (0, "Preview", "Handle: " & GUICtrlRead ($TreeView) & @CRLF & "Item: " & _GUICtrlTreeView_GetText ($TreeView, GUICtrlRead ($TreeView)))
    EndSwitch
    
WEnd

;-))

Stefan

Share this post


Link to post
Share on other sites

Hi Stefan,

Thats the effect I'm after, but right clicking on any of the values always returns the details for the first item :(

NiVZ

Share this post


Link to post
Share on other sites

Indeed,

i don't know how to change behaviour.

You have to click item first with left mouse, then right mouse. Then details will change.

;-))

Stefan

Share this post


Link to post
Share on other sites

Indeed,

i don't know how to change behaviour.

You have to click item first with left mouse, then right mouse. Then details will change.

;-))

Stefan

Ah,

Thats better than what I have already.

Thanks Stefan

Share this post


Link to post
Share on other sites

@jmon

There are examples of this on the forum, I know some Yashied code has this.

...mumbles something about the resurrection of a 17 month old thread...

#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

I see fascists...

Share this post


Link to post
Share on other sites

#8 ·  Posted

Nice trick guys ... It's still working !!!

I don't know why the TreeView has this strange behavior. When you right-click on an item, it appears to be selected but once you click on the context menu item, the selection goes back to the previous item ... ???

Anyway, thanks again for the workaround ...

 

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