#include <GUIMenu.au3>
#include <GuiConstantsEx.au3>
#include <GuiTreeView.au3>
#include <WindowsConstants.au3>
GUICreate("test", 400, 300)
$iTree = GUICtrlCreateTreeView(5, 5, 390, 290)
$hTree = GUICtrlGetHandle($iTree)
$iContext = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
$hContext = GUICtrlGetHandle($iContext)
$iMenuItem1 = GUICtrlCreateMenuItem("About", $iContext)
$iMenuItem2 = GUICtrlCreateMenuItem("Open", $iContext)
Local $aArray[100]
For $i = 0 To 99
$aArray[$i] = $i
$Treeitem = GUICtrlCreateTreeViewItem("Item " & ($i + 1), $iTree)
Assign("." & GUICtrlGetHandle(-1), $i + 1)
Next
GUISetState()
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
While GUIGetMsg() <> -3
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $tNMTV, $iCode, $hItem
$tNMTV = DllStructCreate($tagNMTVDISPINFO, $ilParam)
$iCode = DllStructGetData($tNMTV, "Code")
If ($iCode = $NM_DBLCLK) Then
$hItem = _GUICtrlTreeView_GetSelection($iTree)
MsgBox(0, $hItem, Eval("." & $hItem))
ElseIf ($iCode = $NM_RCLICK) Then
Local $iX, $iY, $aXY, $tXY, $iTest, $iMenuID
; Converts screen coordinate to client coordinate.
$aXY = MouseGetPos()
$tXY = DllStructCreate("long X;long Y")
DllStructSetData($tXY, "X", $aXY[0])
DllStructSetData($tXY, "Y", $aXY[1])
_WinAPI_ScreenToClient($hTree, $tXY)
$iX = DllStructGetData($tXY, "X")
$iY = DllStructGetData($tXY, "Y")
$iTest = _GUICtrlTreeView_HitTest($hTree, $iX, $iY)
If ($iTest <> 4) Then Return ; The mouse point is not hovering on an item.
$hItem = _GUICtrlTreeView_HitTestItem($hTree, $iX, $iY)
_GUICtrlTreeView_SelectItem($hTree, $hItem)
$iIndex = Eval("." & $hItem)
$iMenuID = _GUICtrlMenu_TrackPopupMenu($hContext, $hTree, -1, -1, 1, 1, 2)
If $iMenuID = $iMenuItem1 Then MsgBox(0, "Item " & $iIndex, "About " & $iIndex)
If $iMenuID = $iMenuItem2 Then MsgBox(0, "Item " & $iIndex, "Open " & $iIndex)
$tXY = 0
EndIf
EndFunc ;==>WM_NOTIFY