#include <GUIConstantsEx.au3>
#include <GUITreeView.au3>
#include <WindowsConstants.au3>
$hGUI = GUICreate("TreeView Edit Text", 400, 300)
$iTreeView = GUICtrlCreateTreeView(10, 10, 380, 240, BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_DISABLEDRAGDROP), $WS_EX_CLIENTEDGE)
For $i = 1 To 10
GUICtrlCreateTreeViewItem('Item ' & $i, $iTreeView)
Next
$iRename_Bttn = GUICtrlCreateButton('Rename', 10, 270, 60, 20)
$iESC_Dummy = GUICtrlCreateDummy()
$iEnter_Dummy = GUICtrlCreateDummy()
Dim $aAccelKeys[2][2] = [['{ESC}', $iESC_Dummy], ['{ENTER}', $iEnter_Dummy]]
GUISetAccelerators($aAccelKeys)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUISetState(@SW_SHOW, $hGUI)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $iRename_Bttn
$hItem = _GUICtrlTreeView_GetSelection($iTreeView)
_GUICtrlTreeView_EditText($iTreeView, $hItem)
Case $iESC_Dummy
_GUICtrlTreeView_EndEdit($iTreeView, True)
Case $iEnter_Dummy
_GUICtrlTreeView_EndEdit($iTreeView, False)
EndSwitch
WEnd
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview
$hWndTreeview = $iTreeView
If Not IsHWnd($iTreeView) Then $hWndTreeview = GUICtrlGetHandle($iTreeView)
$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
$iCode = DllStructGetData($tNMHDR, "Code")
Switch $hWndFrom
Case $hWndTreeview
Switch $iCode
Case $TVN_BEGINLABELEDITA, $TVN_BEGINLABELEDITW
Case $TVN_ENDLABELEDITA, $TVN_ENDLABELEDITW
Local $tInfo = DllStructCreate($tagNMHDR & ";" & $tagTVITEMEX, $ilParam)
If DllStructGetData($tInfo, "Text") <> 0 Then
Local $tBuffer = DllStructCreate("wchar Text[" & DllStructGetData($tInfo, "TextMax") & "]", DllStructGetData($tInfo, "Text"))
_GUICtrlTreeView_SetText($hWndTreeview, _GUICtrlTreeView_GetSelection($hWndTreeview), DllStructGetData($tBuffer, "Text"))
EndIf
EndSwitch
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc