Well after reading around I think I found a solution. I did not find any functions that did what wanted at GuiTreeView Management. So I looked around and ended up Creating an event with GUIRegisterMsg of type $WM_NOTIFY. After finding similar problems and their solution code. However, it does seem a bit hacky to me though? Any other better solutions?
#include <GuiMenu.au3>
#include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>
Global $idUI = GUICreate("Reproducer", 210, 482, -1, -1, BitOR($WS_CAPTION, $WS_SYSMENU), $WS_EX_COMPOSITED)
Global $TreeStyles = BitOR($TVS_HASLINES, $TVS_SHOWSELALWAYS, $TVS_HASBUTTONS, $WS_VSCROLL, $WS_HSCROLL, $TVS_DISABLEDRAGDROP, $TVS_LINESATROOT)
Global $idItemTree = GUICtrlCreateTreeView(5, 5, 200, 472, $TreeStyles, $WS_EX_CLIENTEDGE)
;lets add 6 items
Global $idBrowsers= GUICtrlCreateTreeViewItem("Browsers", $idItemTree)
Global $idFirefox = GUICtrlCreateTreeViewItem("Firefox", $idBrowsers)
Global $idChrome = GUICtrlCreateTreeViewItem("Chrome", $idBrowsers)
Global $idFruit = GUICtrlCreateTreeViewItem("Fruit", $idItemTree)
Global $idOranges = GUICtrlCreateTreeViewItem("Oranges", $idFruit)
Global $idPears = GUICtrlCreateTreeViewItem("Pears", $idFruit)
GUICtrlSetImage($idBrowsers, "imageres.dll", 5)
GUICtrlSetImage($idFruit, "imageres.dll", 5)
GUICtrlSetImage($idFirefox, "shell32.dll", 278)
GUICtrlSetImage($idChrome, "shell32.dll", 278)
GUICtrlSetImage($idOranges, "shell32.dll", 278)
GUICtrlSetImage($idPears, "shell32.dll", 278)
_GUICtrlTreeView_Expand($idItemTree)
_GUICtrlTreeView_Sort($idItemTree)
;Let create the context menu
Global $idTreeContext = GUICtrlCreateContextMenu($idItemTree)
Global $idAddMenu = GUICtrlCreateMenuItem("Add New Item", $idTreeContext)
Global $idDeleteMenu = GUICtrlCreateMenuItem("Delete Selected", $idTreeContext)
GUISetState(@SW_SHOW, $idUI)
;Let register events
Opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "terminate", $idUI)
GUICtrlSetOnEvent($idAddMenu , "add_item")
GUICtrlSetOnEvent($idDeleteMenu , "delete_item")
;Register a windows event/msg
GUIRegisterMsg($WM_NOTIFY , "RIGHTSEL_WM_NOTIFY")
Func terminate()
Exit
EndFunc
Func delete_item()
Local $idSelItem = _GUICtrlTreeView_GetSelection($idItemTree)
MsgBox(0,"Selected Item", "The selected item is:"&_GUICtrlTreeView_GetText($idItemTree, $idSelItem))
;delete it just an example don't worry about childern objects
_GUICtrlTreeView_Delete($idItemTree, $idSelItem)
EndFunc
Func add_item()
Local $sName = InputBox("New Item", "Enter a name for the new item", "", "", 100, 125)
If (@error=0) Then
Local $idNew=GUICtrlCreateTreeViewItem($sName, $idItemTree)
GUICtrlSetImage($idNew, "shell32.dll", 278)
EndIf
EndFunc
Func RIGHTSEL_WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
;Notsure what DLLStructure functions do exactly but I feel it can be
;Performed with bitwise and bit shifting operators
$tNMHDR = DllStructCreate($tagNMHDR, $lParam)
$hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
IF (($iIDFrom=$idItemTree) AND ($iCode=$NM_RCLICK)) Then
Local $iMouseX =_WinAPI_GetMousePosX (True, $hWndFrom)
Local $iMouseY =_WinAPI_GetMousePosY (True, $hWndFrom)
Local $hItemSel =_GUICtrlTreeView_HitTestItem($hWndFrom, $iMouseX, $iMouseY)
Local $iHitStat =_GUICtrlTreeView_HitTest ($hWndFrom, $iMouseX, $iMouseY)
;Only update the selected item if right click is in the objects area
If (NOT(64 < $iHitStat) AND ($iHitStat > 1)) Then
If IsPtr($hItemSel)Then
_GUICtrlTreeView_SelectItem($hWndFrom, $hItemSel)
_GUICtrlTreeView_SetFocused($hWndFrom, $hItemSel, True)
EndIf
EndIf
EndIF
Return $GUI_RUNDEFMSG
EndFunc
;do other stuff
while 1
Sleep(10)
WEnd