sl23 Posted 2 hours ago Posted 2 hours ago (edited) Simple as that really. I have a treeview set up in a settings gui. I would like to be able use a right click to auto select the item before proceeding with the context menu and selecting Delete. Essentially I want to perform these actions on Right clicking a treeview item: $GUI_EVENT_PRIMARYDOWN then $GUI_EVENT_SECONDARYUP all in one right click action. I tried this in the handler, but it isn't working: Case $g_AppMenuDeleteItem MouseClick("left") Sleep(50) _AppTab_DeleteSelected() Also tried this: ; --- Right-click: select item under mouse before showing context menu --- If $msg = $GUI_EVENT_SECONDARYDOWN Then Local $cursor = GUIGetCursorInfo($hGUI) Local $ctrlPos = ControlGetPos($hGUI, "", $g_AppTreeView) If IsArray($cursor) And IsArray($ctrlPos) Then Local $relX = $cursor[0] - $ctrlPos[0] Local $relY = $cursor[1] - $ctrlPos[1] Local $item = _GUICtrlTreeView_HitTest($g_AppTreeView, $relX, $relY) If $item > 0 Then GUICtrlSendMsg($g_AppTreeView, $TVM_SELECTITEM, $TVGN_CARET, $item) $g_AppLastSelectedItem = $item EndIf EndIf EndIf Can't get it to work. Is it even possible? Thanks for your help. Edited 2 hours ago by sl23
sl23 Posted 1 hour ago Author Posted 1 hour ago Just tried moving the left click code to a function and calling it, but it refuses to change selection on richt click! there must be a way? expandcollapse popupFunc Tab_App_HandleEvents($msg) Local $selectedItem = GUICtrlRead($g_AppTreeView) ; Handle buttons and context menu Switch $msg Case $g_SaveBtn If $g_SelectedIni <> "" And $g_SelectedSection <> "" Then _AppTab_SaveFields($g_SelectedIni, $g_SelectedSection) EndIf Case $g_IgnoreBtn _AppTab_IgnoreSelected() Case $g_AppMenuDeleteItem _AppTab_HandleSelectionChange($selectedItem) ;~ MouseClick("left") ;~ Sleep(50) _AppTab_DeleteSelected() Case $g_AppFields("SymLinkCreate") ; Enable/disable symlinks field based on checkbox If GUICtrlRead($g_AppFields("SymLinkCreate")) = $GUI_CHECKED Then GUICtrlSetState($g_AppsSymlinksEdit, $GUI_ENABLE) Else GUICtrlSetState($g_AppsSymlinksEdit, $GUI_DISABLE) EndIf Case $g_AppFields("Sandboxie") ; Enable/disable SandboxName field based on checkbox If GUICtrlRead($g_AppFields("Sandboxie")) = $GUI_CHECKED Then GUICtrlSetState($g_AppFields("SandboxName"), $GUI_ENABLE) Else GUICtrlSetState($g_AppFields("SandboxName"), $GUI_DISABLE) EndIf EndSwitch _AppTab_HandleSelectionChange($selectedItem) ;~ ; Only act if the selection changed ;~ If $selectedItem <> $g_AppLastSelectedItem Then ;~ ; If a category node is selected ;~ If $selectedItem <> 0 And $g_AppCategoryTreeItems.Exists($selectedItem) Then ;~ _AppTab_ClearFields() ;~ _AppTab_DisableFields() ;~ GUICtrlSetState($g_SaveBtn, $GUI_DISABLE) ;~ GUICtrlSetState($g_IgnoreBtn, $GUI_DISABLE) ;~ $g_SelectedIni = "" ;~ $g_SelectedSection = "" ;~ ; If last selected was a valid app item and now whitespace/node: disable buttons ;~ ElseIf $g_AppLastSelectedItem <> 0 And $g_AppTreeItemToSection.Exists($g_AppLastSelectedItem) _ ;~ And ($selectedItem = 0 Or Not $g_AppTreeItemToSection.Exists($selectedItem)) Then ;~ _AppTab_ClearFields() ;~ _AppTab_Buttons_Disable() ;~ $g_SelectedIni = "" ;~ $g_SelectedSection = "" ;~ ; If selection changed to a valid app item: enable buttons ;~ ElseIf $selectedItem <> 0 And $g_AppTreeItemToSection.Exists($selectedItem) Then ;~ $g_SelectedSection = $g_AppTreeItemToSection.Item($selectedItem) ;~ $g_SelectedIni = $g_AppTreeItemToIni.Item($selectedItem) ;~ _AppTab_PopulateFields($g_SelectedIni, $g_SelectedSection) ;~ _AppTab_EnableFields() ;~ _AppTab_Buttons_Enable() ;~ ; If selection changed to whitespace/node, disable buttons ;~ Else ;~ _AppTab_ClearFields() ;~ _AppTab_Buttons_Disable() ;~ $g_SelectedIni = "" ;~ $g_SelectedSection = "" ;~ EndIf ;~ $g_AppLastSelectedItem = $selectedItem ;~ EndIf EndFunc Func _AppTab_HandleSelectionChange($selectedItem) If $selectedItem <> $g_AppLastSelectedItem Then If $selectedItem <> 0 And $g_AppCategoryTreeItems.Exists($selectedItem) Then _AppTab_ClearFields() _AppTab_DisableFields() GUICtrlSetState($g_SaveBtn, $GUI_DISABLE) GUICtrlSetState($g_IgnoreBtn, $GUI_DISABLE) $g_SelectedIni = "" $g_SelectedSection = "" ElseIf $g_AppLastSelectedItem <> 0 And $g_AppTreeItemToSection.Exists($g_AppLastSelectedItem) _ And ($selectedItem = 0 Or Not $g_AppTreeItemToSection.Exists($selectedItem)) Then _AppTab_ClearFields() _AppTab_Buttons_Disable() $g_SelectedIni = "" $g_SelectedSection = "" ElseIf $selectedItem <> 0 And $g_AppTreeItemToSection.Exists($selectedItem) Then $g_SelectedSection = $g_AppTreeItemToSection.Item($selectedItem) $g_SelectedIni = $g_AppTreeItemToIni.Item($selectedItem) _AppTab_PopulateFields($g_SelectedIni, $g_SelectedSection) _AppTab_EnableFields() _AppTab_Buttons_Enable() Else _AppTab_ClearFields() _AppTab_Buttons_Disable() $g_SelectedIni = "" $g_SelectedSection = "" EndIf $g_AppLastSelectedItem = $selectedItem EndIf EndFunc
ioa747 Posted 1 hour ago Posted 1 hour ago (edited) Local $idTreeview = GUICtrlCreateTreeView(6, 6, 100, 150, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE) Local $idButtoncontext = GUICtrlCreateContextMenu($idTreeview) Local $idMenuDelete = GUICtrlCreateMenuItem("Dellete", $idButtoncontext) Switch $msg ... Case $idMenuDelete Local $hSelection = _GUICtrlTreeView_GetSelection($idTreeView) ConsoleWrite("!Dellete Selection: " & $hSelection & @CRLF) ;_GUICtrlTreeView_Delete($idTreeView, $hSelection) Edited 1 hour ago by ioa747 I know that I know nothing
sl23 Posted 1 hour ago Author Posted 1 hour ago (edited) Thanks, but isn't that just a standard context menu creation for a TreeView? I need it to select first then show the context menu for delete. I currently have the context menu and it works fine. Everything is working ok. I just needed to enhance the right click by first selecting the item with the right mouse button, then open the context menu. is that possible? Ah, sorry i posted while you were in the middle of updating your post! Seems that is not what I wanted either actually. Thank you anyway. As you can see it is probably a niche thing? I have a tree view with category containing items. If I select a category then right click an item I am asked if I want to delete the category, which is a mistake and could be a major problem! So I wondered if it were possible to do the selction on right click before showing the context menu? Edited 37 minutes ago by sl23
sl23 Posted 53 minutes ago Author Posted 53 minutes ago (edited) Here is a test code. in order to test correctly, follow these instructions to see the error in the handling of the selection: 1. Run the script and select the Category using left mouse button to open it. 2. Right click any node and select delete. Notice that the message is asking if you want to delete the category, not the node you right clicked on! So what I am asking is, can I basically perform a left-click action using the right mouse click? This would then: 1. select the item. 2. Open the context menu. 3. selecting delete would then delete the correct item. Test code: expandcollapse popup#include <GUIConstantsEx.au3> #include <GuiTreeView.au3> $hGUI = GUICreate("TreeView Category Right Click Select Example", 300, 220) $idTreeview = GUICtrlCreateTreeView(10, 10, 150, 180, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_SHOWSELALWAYS)) $idMenu = GUICtrlCreateContextMenu($idTreeview) $idMenuDelete = GUICtrlCreateMenuItem("Delete", $idMenu) ; Add category node(s) Global $aCategories[1] $aCategories[0] = GUICtrlCreateTreeViewItem("Category 1", $idTreeview) ; Add child nodes under category $idItem1 = GUICtrlCreateTreeViewItem("Node 1", $aCategories[0]) $idItem2 = GUICtrlCreateTreeViewItem("Node 2", $aCategories[0]) $idItem3 = GUICtrlCreateTreeViewItem("Node 3", $aCategories[0]) GUISetState() While 1 $msg = GUIGetMsg() ; --- Hit-test for right mouse button down --- If $msg = $GUI_EVENT_SECONDARYDOWN Then $aCursor = GUIGetCursorInfo($hGUI) $aPos = ControlGetPos($hGUI, "", $idTreeview) If IsArray($aCursor) And IsArray($aPos) Then $relX = $aCursor[0] - $aPos[0] $relY = $aCursor[1] - $aPos[1] $itemID = _GUICtrlTreeView_HitTest($idTreeview, $relX, $relY) If $itemID > 0 Then GUICtrlSendMsg($idTreeview, 0x110B, 0x9, $itemID) ; Select the node under mouse EndIf EndIf EndIf Switch $msg Case $idMenuDelete $hSelection = _GUICtrlTreeView_GetSelection($idTreeview) ; Get the text of the selected node for confirmation Local $sNodeText = _GUICtrlTreeView_GetText($idTreeview, $hSelection) ; Check if selection is a category node (parent) Local $isCategory = False For $i = 0 To UBound($aCategories) - 1 If $hSelection = $aCategories[$i] Then $isCategory = True ExitLoop EndIf Next If $isCategory Then ; Confirm category deletion Local $answer = MsgBox(36, "Delete Category", "Are you sure you want to delete the CATEGORY: """ & $sNodeText & """ and all its items?") If $answer = 6 Then ; Yes _GUICtrlTreeView_Delete($idTreeview, $hSelection) EndIf ElseIf $hSelection <> 0 Then ; Confirm node deletion Local $answer = MsgBox(36, "Delete Node", "Are you sure you want to delete the NODE: """ & $sNodeText & """?") If $answer = 6 Then ; Yes _GUICtrlTreeView_Delete($idTreeview, $hSelection) EndIf EndIf Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd GUIDelete() Why is it that this doesn't work: MouseClick($MOUSE_CLICK_LEFT) Edited 16 minutes ago by sl23
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