NiVZ Posted March 29, 2010 Share Posted March 29, 2010 (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 March 29, 2010 by NiVZ Link to comment Share on other sites More sharing options...
99ojo Posted March 29, 2010 Share Posted March 29, 2010 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 Link to comment Share on other sites More sharing options...
NiVZ Posted March 29, 2010 Author Share Posted March 29, 2010 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 Link to comment Share on other sites More sharing options...
99ojo Posted March 29, 2010 Share Posted March 29, 2010 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 Link to comment Share on other sites More sharing options...
NiVZ Posted March 29, 2010 Author Share Posted March 29, 2010 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.;-))StefanAh,Thats better than what I have already.Thanks Stefan Link to comment Share on other sites More sharing options...
jmon Posted August 25, 2011 Share Posted August 25, 2011 Hi, I have exactly the same problem. Has anyone found a solution for this issue ? [center]www.jmontserrat.comFile Sequence UDF - _StringExtractPaths - _StringTrimPattern - GuiCtrlSetOnTop - CalendarUDF[/center] Link to comment Share on other sites More sharing options...
rover Posted August 27, 2011 Share Posted August 27, 2011 @jmonThere 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...expandcollapse popup#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... Link to comment Share on other sites More sharing options...
KotKot77 Posted May 22, 2017 Share Posted May 22, 2017 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 ... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now