This is the default behaviour of a treeview, no bug
Right click notifications from the treeview must be hit tested for the item under the mouse
so the item can be selected.
#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
Global $MenuParent, $gui, $contextmenu, $i, $n, $textitem, $Tree, $Child
Global $Tree, $tNMHDR, $hWndFrom, $iIDFrom, $iCode, $fOnItem = False
Main()
While 1
Sleep(100)
WEnd
Func Main()
$gui = GUICreate("Treeview Demo", 300, 500, -1, -1)
$Tree = GUICtrlCreateTreeView(-1, -1, 300, 500)
For $n = 1 to 9
$Parent = GUICtrlCreateTreeViewItem("Parent " & $n,$Tree)
For $i = 1 to 3
$Child = GUICtrlCreateTreeViewItem("Child " & $n & "-" & $i,$Parent)
contextMenu($Child)
Next
Next
GUISetOnEvent($GUI_EVENT_CLOSE, 'Close')
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW)
EndFunc
Func contextMenu($MenuParent)
$contextmenu = GUICtrlCreateContextMenu($MenuParent)
$textitem = GUICtrlCreateMenuItem("Delete", $contextmenu)
GUICtrlSetOnEvent ( $textitem, "Delete" )
EndFunc
Func Delete()
;If $fOnItem Then ;optionally block menu return from right click over non-item area
;$fOnItem = False
_GUICtrlTreeView_Delete($Tree, _GUICtrlTreeView_GetSelection ($Tree))
;EndIf
EndFunc
Func Close()
Exit
EndFunc
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 $Tree
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 ;optionally block menu return from right click over non-item area
;$fOnItem = True
Local $hItem = DllStructGetData($tHitTest, 'Item')
If IsPtr($hItem) Then
_GUICtrlTreeView_SelectItem($hWndFrom, $hItem)
EndIf
;EndIf
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_NOTIFY