Jump to content

Recommended Posts

Posted (edited)

I thought I worked with an edit-in-place treeview a few years back but I just couldn't find it. With the help of @eltorro 


This might be another example in the forum. I hope this helps someone out in the future (his embedded rectangle finder which you can use separately as his UDF):
 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <EditConstants.au3>
#include <TreeViewConstants.au3>

Global $hGUI = GUICreate("TreeView Edit-in-Place (F2) to edit", 400, 300)
Global $idTreeView = GUICtrlCreateTreeView(10, 10, 250, 250, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT))
Global $hTreeView = GUICtrlGetHandle($idTreeView)

Global $hRoot = _GUICtrlTreeView_Add($hTreeView, 0, "Root")
_GUICtrlTreeView_AddChild($hTreeView, $hRoot, "Child 1")
_GUICtrlTreeView_AddChild($hTreeView, $hRoot, "Child 2")
_GUICtrlTreeView_AddChild($hTreeView, $hRoot, "Child 3")

Global $idInput = GUICtrlCreateInput("", 0, 0, 0, 0, $ES_AUTOHSCROLL)
GUICtrlSetState($idInput, $GUI_HIDE)
GUISetState(@SW_SHOW, $hGUI)

Global $hEditItem = 0

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While True
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit

        ; Catch ENTER when editing
        Case $idInput
            If _IsPressed("0D") Then ; Enter key
                _SaveEdit()
            EndIf
    EndSwitch
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
    Local $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    Local $hFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    Local $iCode = DllStructGetData($tNMHDR, "Code")

    If $hFrom = $hTreeView Then
        Switch $iCode
            Case $TVN_KEYDOWN
                Local $tInfo = DllStructCreate($tagNMHDR & ";word wVKey;uint flags", $lParam)
                Local $nVKey = DllStructGetData($tInfo, "wVKey")

                If $nVKey = 113 Then ; F2
                    _StartEdit()
                EndIf
        EndSwitch
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

Func _StartEdit()
    ; Get the currently selected TreeView item handle
    $hEditItem = _GUICtrlTreeView_GetSelection($hTreeView)
    If $hEditItem = 0 Then Return

    ; Get the rectangle of the item
    Local $aRect = _GuiCtrlTreeViewGetItemRect($idTreeView, $hEditItem, True, 1)
    If @error Or Not IsArray($aRect) Then Return

    ; Compute width and height
    Local $width = $aRect[2] - $aRect[0]
    Local $height = $aRect[3] - $aRect[1]

    ; Get current text of item, Set input box text and position
    Local $sText = _GUICtrlTreeView_GetText($hTreeView, $hEditItem)
    GUICtrlSetData($idInput, $sText)
    GUICtrlSetPos($idInput, $aRect[0], $aRect[1], $width, $height)
    GUICtrlSetState($idInput, $GUI_SHOW)
    GUICtrlSetState($idInput, $GUI_FOCUS)
    GUICtrlSendMsg($idInput, $EM_SETSEL, 0, -1)

    ; Hook events to save on Enter or losing focus
    GUISetOnEvent($GUI_EVENT_CLOSE, "_SaveAndHideInput") ; optional, just in case
    GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "_CheckEnter") ; detect Enter key

    ; Loss of focus
    GUICtrlSetOnEvent($idInput, "_SaveAndHideInput")
EndFunc

;--------------------------------------------
Func _CheckEnter()
    Local $aKeys = GUIGetCursorInfo() ; quick hack to see if Enter pressed
    If _IsPressed("0D") Then _SaveAndHideInput()
EndFunc

Func _SaveAndHideInput()
    If $hEditItem = 0 Then Return
    Local $sNew = GUICtrlRead($idInput)
    _GUICtrlTreeView_SetText($hTreeView, $hEditItem, $sNew)
    GUICtrlSetState($idInput, $GUI_HIDE)
    $hEditItem = 0
EndFunc


Func _SaveEdit()
    If $hEditItem = 0 Then Return

    Local $sNew = GUICtrlRead($idInput)
    If $sNew <> "" Then
        _GUICtrlTreeView_SetText($hTreeView, $hEditItem, $sNew)
    EndIf

    GUICtrlSetState($idInput, $GUI_HIDE)
    $hEditItem = 0
EndFunc

Func _IsPressed($sHexKey)
    Local $aR = DllCall("user32.dll", "short", "GetAsyncKeyState", "int", Dec($sHexKey))
    If Not @error And BitAND($aR[0], 0x8000) <> 0 Then
        Return True
    EndIf
    Return False
EndFunc

Func _GuiCtrlTreeViewGetItemRect($i_treeview, $h_item, $b_flag = True, $i_mode = 2)
;===============================================================================
;
; Description:       _GUICtrlTreeViewGetItemRect
; Parameter(s):      $i_treeview        - controlID
;                    $h_item            - item ID/handle to get the bounding rect of
;                    $b_flag            - If set to True, rect returned is just the text area
;                                      else the rect extends out.
;                    $i_mode            - Mode 0 rect relative to Control x,y,r,b
;                                    - Mode 1 Absoulte points relatvie to window.x,y,r,b
;                                    - Mode 2 (Default) Rect relative to Window x,y,w,h
; Requirement:        None
; Return Value(s):    Returns the item rect on success or 0 otherwise
; User CallTip:
; Author(s):        eltorro <steve@ocotillo.sytes.net>
; Note(s):            Gets the bounding rectangle of a treeview item.
;
;===============================================================================
    Local $st_Rect = DllStructCreate("int;int;int;int")
    If @error Then Return 0
    Local $retval[4], $TVM_GETITEMRECT = (0x1100 + 4)
    DllStructSetData($st_Rect, 1, $h_item)
    If GUICtrlSendMsg($i_treeview, $TVM_GETITEMRECT, $b_flag, DllStructGetPtr($st_Rect)) = 0 Then Return 0

    For $x = 0 To 3
        $retval[$x] = DllStructGetData($st_Rect, $x + 1)
    Next
    If $i_mode = 0 Then Return $retval

    Local $tv_pos = ControlGetPos("", "", $i_treeview)
    If @error Then Return 0
    Switch $i_mode
        Case 1
            $retval[0] += $tv_pos[0]
            $retval[1] += $tv_pos[1]
            $retval[2] += $tv_pos[0]
            $retval[3] += $tv_pos[1]
        Case 2
            $retval[2] -= $retval[0]
            $retval[3] -= $retval[1]
            $retval[0] += $tv_pos[0]
            $retval[1] += $tv_pos[1]
    EndSwitch

    Return $retval
EndFunc


F2 to edit.


Mods can move this if it's in the wrong location or I'll repost if you prefer.
 

 

Edited by NassauSky

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...