Jump to content

Editable TreeView is possible


GaryFrost
 Share

Recommended Posts

Probably some room for improvement.

Note: You must press Enter for the change to stick!, $TVS_EDITLABELS must be used in the style

#include <GUIConstants.au3>
Global Const $TV_FIRST = 0x1100
Global Const $TVS_EDITLABELS = 0x8
Global Const $TVGN_CARET = 0x9
Global Const $TVM_GETNEXTITEM = ($TV_FIRST + 10)
Global Const $TVM_EDITLABELA = ($TV_FIRST + 14)
Global Const $TVM_ENDEDITLABELNOW = ($TV_FIRST + 22)

_Main()

Func _Main()
    Local $editCtrl = "Edit1";name of the Edit control that pops up when edit a listitem....
    Local $editFlag = 0, $treeview, $Gui, $start, $diff, $doubleclicked, $msg
    
    $Gui = GUICreate("My GUI with treeview", 350, 212)
    
    $treeview = GUICtrlCreateTreeView(6, 6, 100, 150, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_EDITLABELS), $WS_EX_CLIENTEDGE)
    $generalitem = GUICtrlCreateTreeViewItem("General", $treeview)
    $displayitem = GUICtrlCreateTreeViewItem("Display", $treeview)
    $aboutitem = GUICtrlCreateTreeViewItem("About", $generalitem)
    $compitem = GUICtrlCreateTreeViewItem("Computer", $generalitem)
    $useritem = GUICtrlCreateTreeViewItem("User", $generalitem)
    $resitem = GUICtrlCreateTreeViewItem("Resolution", $displayitem)
    $otheritem = GUICtrlCreateTreeViewItem("Other", $displayitem)
    
    $startlabel = GUICtrlCreateLabel("TreeView Demo", 190, 90, 100, 20)
    $aboutlabel = GUICtrlCreateLabel("This little scripts demonstates the using of a treeview-control.", 190, 70, 100, 60)
    GUICtrlSetState(-1, $GUI_HIDE); Hides the "aboutlabel"-text during initialization
    $compinfo = GUICtrlCreateLabel("Name:" & @TAB & @ComputerName & @LF & "OS:" & @TAB & @OSVersion & @LF & "SP:" & @TAB & @OSServicePack, 120, 30, 200, 80)
    GUICtrlSetState(-1, $GUI_HIDE); Hides the "compinfo"-text during initialization
    
    $okbutton = GUICtrlCreateButton("OK", 100, 185, 70, 20)
    $cancelbutton = GUICtrlCreateButton("Cancel", 180, 185, 70, 20)
    
    GUISetState()
    $dll = DllOpen("user32.dll")
    While 1
        $msg = GUIGetMsg()
        _MonitorEditState($Gui, $editCtrl, $editFlag, $treeview, $dll, $msg, $start, $diff, $doubleclicked)
        Select
            Case $msg = $cancelbutton Or $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $okbutton
                $item = GUICtrlRead($treeview)    ; Get the controlID of the current selected treeview item
                If $item = 0 Then
                    MsgBox(64, "TreeView Demo", "No item currently selected")
                Else
                    $advmsg = GUICtrlRead($item, 1); Get advanced infos about the treeview item
                    If ($advmsg[0] == 0) Then
                        MsgBox(16, "Error", "Error while retrieving infos about item")
                    Else
                        MsgBox(64, "TreeView Demo", "Current item selected is: " & $advmsg[0]); $advmsg[0] contains the text and $advmsg[1] the state value of the treeview item
                    EndIf
                EndIf
                
            ; The following items will hide the other labels (1st and 2nd parameter) and then show the 'own' labels (3rd and 4th parameter)
            Case $msg = $generalitem
                GUIChangeItems($aboutlabel, $compinfo, $startlabel, $startlabel)
                
            Case $msg = $aboutitem
                GUICtrlSetState($compinfo, $GUI_HIDE)
                GUIChangeItems($startlabel, $startlabel, $aboutlabel, $aboutlabel)
                
            Case $msg = $compitem
                GUIChangeItems($startlabel, $aboutlabel, $compinfo, $compinfo)
        EndSelect
    WEnd
    DllClose($dll)
    
    GUIDelete()
    
    Exit
EndFunc  ;==>_Main

Func GUIChangeItems($hidestart, $hideend, $showstart, $showend)
    Local $idx
    
    For $idx = $hidestart To $hideend
        GUICtrlSetState($idx, $GUI_HIDE)
    Next
    For $idx = $showstart To $showend
        GUICtrlSetState($idx, $GUI_SHOW)
    Next
EndFunc  ;==>GUIChangeItems

Func _MonitorEditState(ByRef $h_gui, ByRef $editCtrl, ByRef $editFlag, ByRef $treeview, ByRef $dll, ByRef $msg, ByRef $start, ByRef $diff, ByRef $doubleclicked)
    If $msg = $GUI_EVENT_PRIMARYDOWN Then
        Local $focus = ControlGetFocus(WinGetTitle($h_gui))
        Local $pos = ControlGetPos($h_gui, "", $focus)
        Local $mpos = MouseGetPos()
        If ControlCommand($h_gui, "", $editCtrl, "IsVisible", "") Then
            If $mpos[0] < $pos[0] Or $mpos[0] > $pos[0] + $pos[2] Or _
                    $mpos[1] < $pos[1] Or $mpos[1] > $pos[1] + $pos[3] Then
                CancelEdit($treeview)
                $editFlag = 0
            EndIf
        Else
            If (StringInStr($focus, "SysTreeView3")) Then
                
                $diff = TimerDiff($start)
            ; Read the current mouse-doubleclick-settings from registry
                $mousespeed = RegRead("HKCU\Control Panel\Mouse", "DoubleClickSpeed")
                If $mousespeed = "" Then $mousespeed = 700
                
                If $diff < $mousespeed And $doubleclicked = 0 Then
                    Rename($treeview)
                    $doubleclicked = 1
                Else
                    $doubleclicked = 0
                EndIf
                $start = TimerInit()
            EndIf
        EndIf
    EndIf
    
    Local $pressed = _IsPressedMod($dll)
    If $editFlag And $pressed = 13 Then; pressed enter
        Update($h_gui, $editCtrl, $treeview)
    ElseIf $editFlag And $pressed = 27 Then; pressed esc
        CancelEdit($treeview)
        $editFlag = 0
    ElseIf Not $editFlag And $pressed = 113 Then; pressed f2
        Rename($treeview)
        $editFlag = 1
    EndIf
    Sleep(50)
    
    If ControlCommand($h_gui, "", $editCtrl, "IsVisible", "") Then
        If $editFlag = 0 Then
            $editFlag = 1
            Rename($treeview)
        EndIf
    Else
        $editFlag = 0
    EndIf
    
EndFunc  ;==>_MonitorEditState

Func Rename(ByRef $treeview)
    Local $itemIndex = GUICtrlSendMsg($treeview, $TVM_GETNEXTITEM, $TVGN_CARET, 0)
    GUICtrlSendMsg($treeview, $TVM_EDITLABELA, 0, $itemIndex)
EndFunc  ;==>Rename

Func Update(ByRef $Gui, ByRef $editCtrl, ByRef $treeview)
    Local $newText = ControlGetText($Gui, "", $editCtrl)
    Local $item = GUICtrlRead($treeview)
    GUICtrlSetData($item, $newText)
    HotKeySet("{Esc}")
    GUICtrlSendMsg($treeview, $TVM_ENDEDITLABELNOW, False, 0)
EndFunc  ;==>Update

Func CancelEdit(ByRef $treeview)
    GUICtrlSendMsg($treeview, $TVM_ENDEDITLABELNOW, True, 0)
    HotKeySet("{Esc}")
EndFunc  ;==>CancelEdit

Func _IsPressedMod($dll = "user32.dll")
    Local $aR, $bRv, $hexKey, $i
    For $i = 8 To 128
        $hexKey = '0x' & Hex($i, 2)
        $aR = DllCall($dll, "int", "GetAsyncKeyState", "int", $hexKey)
        If $aR[0] <> 0 Then Return $i
    Next
    Return 0
EndFunc  ;==>_IsPressedMod
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

gary,

Thanks for this.

See in my Array2D GUI I have used it to;

1. double click on a row to put the row in your GUI.

2. Double click on an item there to edit it.

I presume we can't edit a "cell" to save the doubling up?

Thanks, Randall

Array2D.au3

EDIT -Woops -Sorry; wrong post should be on "ListView" edit; but same point...

Edited by randallc
Link to comment
Share on other sites

@Gafrost

I tested it and it works. But I have problems with the clicking speed.

Sometime I need to click it 5 times or more to get it in the editable mode.

This was also the case with the Editable ListView. Which you seemed to have solved.

Can you do the seem with this ?

Thanks

Link to comment
Share on other sites

Treeview is a little different than listview on how it behaves, so give this a try

#include <GUIConstants.au3>
Global Const $TV_FIRST = 0x1100
Global Const $TVS_EDITLABELS = 0x8
Global Const $TVGN_CARET = 0x9
Global Const $TVM_GETNEXTITEM = ($TV_FIRST + 10)
Global Const $TVM_EDITLABELA = ($TV_FIRST + 14)
Global Const $TVM_ENDEDITLABELNOW = ($TV_FIRST + 22)

_Main()

Func _Main()
   Local $editCtrl = "Edit1";name of the Edit control that pops up when edit a listitem....
   Local $editFlag = 0, $treeview, $Gui, $start, $diff, $doubleclicked, $msg
   
   $Gui = GUICreate("My GUI with treeview", 350, 212)
   
   $treeview = GUICtrlCreateTreeView(6, 6, 100, 150, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_EDITLABELS), $WS_EX_CLIENTEDGE)
   $generalitem = GUICtrlCreateTreeViewItem("General", $treeview)
   $displayitem = GUICtrlCreateTreeViewItem("Display", $treeview)
   $aboutitem = GUICtrlCreateTreeViewItem("About", $generalitem)
   $compitem = GUICtrlCreateTreeViewItem("Computer", $generalitem)
   $useritem = GUICtrlCreateTreeViewItem("User", $generalitem)
   $resitem = GUICtrlCreateTreeViewItem("Resolution", $displayitem)
   $otheritem = GUICtrlCreateTreeViewItem("Other", $displayitem)
   
   $startlabel = GUICtrlCreateLabel("TreeView Demo", 190, 90, 100, 20)
   $aboutlabel = GUICtrlCreateLabel("This little scripts demonstates the using of a treeview-control.", 190, 70, 100, 60)
   GUICtrlSetState(-1, $GUI_HIDE); Hides the "aboutlabel"-text during initialization
   $compinfo = GUICtrlCreateLabel("Name:" & @TAB & @ComputerName & @LF & "OS:" & @TAB & @OSVersion & @LF & "SP:" & @TAB & @OSServicePack, 120, 30, 200, 80)
   GUICtrlSetState(-1, $GUI_HIDE); Hides the "compinfo"-text during initialization
   
   $okbutton = GUICtrlCreateButton("OK", 100, 185, 70, 20)
   $cancelbutton = GUICtrlCreateButton("Cancel", 180, 185, 70, 20)
   $selected = ""
   GUISetState()
   $dll = DllOpen("user32.dll")
   While 1
      _MonitorEditState($Gui, $editCtrl, $editFlag, $treeview, $dll, $msg)
      $msg = GUIGetMsg()
      Select
         Case $msg = $cancelbutton Or $msg = $GUI_EVENT_CLOSE
            ExitLoop
         Case $msg = $okbutton
            $item = GUICtrlRead($treeview)   ; Get the controlID of the current selected treeview item
            If $item = 0 Then
               MsgBox(64, "TreeView Demo", "No item currently selected")
            Else
               $advmsg = GUICtrlRead($item, 1); Get advanced infos about the treeview item
               If ($advmsg[0] == 0) Then
                  MsgBox(16, "Error", "Error while retrieving infos about item")
               Else
                  MsgBox(64, "TreeView Demo", "Current item selected is: " & $advmsg[0]); $advmsg[0] contains the text and $advmsg[1] the state value of the treeview item
               EndIf
            EndIf
            
           ; The following items will hide the other labels (1st and 2nd parameter) and then show the 'own' labels (3rd and 4th parameter)
         Case $msg = $generalitem
            GUIChangeItems($aboutlabel, $compinfo, $startlabel, $startlabel)
            
         Case $msg = $aboutitem
            GUICtrlSetState($compinfo, $GUI_HIDE)
            GUIChangeItems($startlabel, $startlabel, $aboutlabel, $aboutlabel)
            
         Case $msg = $compitem
            GUIChangeItems($startlabel, $aboutlabel, $compinfo, $compinfo)
      EndSelect
   WEnd
   DllClose($dll)
   
   GUIDelete()
   
   Exit
EndFunc  ;==>_Main

Func GUIChangeItems($hidestart, $hideend, $showstart, $showend)
   Local $idx
   
   For $idx = $hidestart To $hideend
      GUICtrlSetState($idx, $GUI_HIDE)
   Next
   For $idx = $showstart To $showend
      GUICtrlSetState($idx, $GUI_SHOW)
   Next
EndFunc  ;==>GUIChangeItems

Func _MonitorEditState(ByRef $h_gui, ByRef $editCtrl, ByRef $editFlag, ByRef $treeview, ByRef $dll, ByRef $msg)
   
   Local $focus = ControlGetFocus(WinGetTitle($h_gui))
   If $msg = $GUI_EVENT_PRIMARYDOWN And (StringInStr($focus, "SysTreeView3")) Then
      If $editFlag Then
         CancelEdit($treeview)
         $editFlag = 0
      Else
         Rename($treeview)
         $editFlag = 1
      EndIf
   ElseIf $msg = $GUI_EVENT_PRIMARYDOWN And $editFlag Then
      CancelEdit($treeview)
      $editFlag = 0
   EndIf
   
   Local $pressed = _IsPressedMod($dll)
   If $editFlag And $pressed = 13 Then; pressed enter
      Update($h_gui, $editCtrl, $treeview)
      $editFlag = 0
   ElseIf $editFlag And $pressed = 27 Then; pressed esc
      CancelEdit($treeview)
      $editFlag = 0
   ElseIf Not $editFlag And $pressed = 113 Then; pressed f2
      Rename($treeview)
      $editFlag = 1
   EndIf
   Sleep(50)
   
EndFunc  ;==>_MonitorEditState

Func Rename(ByRef $treeview)
   Local $itemIndex = GUICtrlSendMsg($treeview, $TVM_GETNEXTITEM, $TVGN_CARET, 0)
   GUICtrlSendMsg($treeview, $TVM_EDITLABELA, 0, $itemIndex)
    HotKeySet("{Esc}","_Cancel")
EndFunc  ;==>Rename

Func Update(ByRef $Gui, ByRef $editCtrl, ByRef $treeview)
   Local $newText = ControlGetText($Gui, "", $editCtrl)
   Local $item = GUICtrlRead($treeview)
   GUICtrlSetData($item, $newText)
   HotKeySet("{Esc}")
   GUICtrlSendMsg($treeview, $TVM_ENDEDITLABELNOW, False, 0)
EndFunc  ;==>Update

Func _Cancel()
; dummy function
EndFunc

Func CancelEdit(ByRef $treeview)
   GUICtrlSendMsg($treeview, $TVM_ENDEDITLABELNOW, True, 0)
   HotKeySet("{Esc}")
EndFunc  ;==>CancelEdit

Func _IsPressedMod($dll = "user32.dll")
   Local $aR, $bRv, $hexKey, $i
   For $i = 8 To 128
      $hexKey = '0x' & Hex($i, 2)
      $aR = DllCall($dll, "int", "GetAsyncKeyState", "int", $hexKey)
      If $aR[0] <> 0 Then Return $i
   Next
   Return 0
EndFunc  ;==>_IsPressedMod
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

  • 4 years later...

Old topic, but here's an updated version of the example that works with 3.4.0.0, if anyone is wondering...

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

_Main()

Func _Main()
   Local $editCtrl = "Edit1";name of the Edit control that pops up when Edit a listitem....
   Local $editFlag = 0, $treeview, $Gui, $start, $diff, $doubleclicked, $msg

   $Gui = GUICreate("My GUI with treeview", 350, 212)

   $Treeview = GUICtrlCreateTreeView(6, 6, 100, 150, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_EDITLABELS), $WS_EX_CLIENTEDGE)
   $generalitem = GUICtrlCreateTreeViewItem("General", $treeview)
   $displayitem = GUICtrlCreateTreeViewItem("Display", $treeview)
   $aboutitem = GUICtrlCreateTreeViewItem("About", $generalitem)
   $compitem = GUICtrlCreateTreeViewItem("Computer", $generalitem)
   $useritem = GUICtrlCreateTreeViewItem("User", $generalitem)
   $resitem = GUICtrlCreateTreeViewItem("Resolution", $displayitem)
   $otheritem = GUICtrlCreateTreeViewItem("Other", $displayitem)

   $startlabel = GUICtrlCreateLabel("Treeview Demo", 190, 90, 100, 20)
   $aboutlabel = GUICtrlCreateLabel("This little scripts demonstates the using of a treeview-control.", 190, 70, 100, 60)
   GUICtrlSetState(-1, $GUI_HIDE); Hides the "aboutlabel"-text during initialization
   $compinfo = GUICtrlCreateLabel("Name:" & @TAB & @ComputerName & @LF & "OS:" & @TAB & @OSVersion & @LF & "SP:" & @TAB & @OSServicePack, 120, 30, 200, 80)
   GUICtrlSetState(-1, $GUI_HIDE); Hides the "compinfo"-text during initialization

   $okbutton = GUICtrlCreateButton("OK", 100, 185, 70, 20)
   $cancelbutton = GUICtrlCreateButton("Cancel", 180, 185, 70, 20)
   $selected = ""
   GUISetState()
   $dll = DllOpen("user32.dll")
   While 1
      _MonitorEditState($Gui, $editCtrl, $editFlag, $treeview, $dll, $msg)
      $msg = GUIGetMsg()
      Select
         Case $msg = $cancelbutton Or $msg = $GUI_EVENT_CLOSE
            ExitLoop
         Case $msg = $okbutton
            $item = GUICtrlRead($treeview)   ; Get the controlID of the current selected Treeview item
            If $item = 0 Then
               MsgBox(64, "Treeview Demo", "No item currently selected")
            Else
               $advmsg = GUICtrlRead($item); Get advanced infos about the Treeview item
               $advmsg1 = GUICtrlRead($item, 1); Get advanced infos about the Treeview item
               If ($advmsg == 0) Then
                  MsgBox(16, "Error", "Error while retrieving infos about item")
               Else
                  MsgBox(64, "Treeview Demo", "Current item selected is: " & $advmsg1); $advmsg[0] contains the text and $advmsg[1] the state value of the Treeview item
               EndIf
            EndIf

           ; The following items will hide the other labels (1st and 2nd parameter) and then show the 'own' labels (3rd and 4th parameter)
         Case $msg = $generalitem
            GUIChangeItems($aboutlabel, $compinfo, $startlabel, $startlabel)

         Case $msg = $aboutitem
            GUICtrlSetState($compinfo, $GUI_HIDE)
            GUIChangeItems($startlabel, $startlabel, $aboutlabel, $aboutlabel)

         Case $msg = $compitem
            GUIChangeItems($startlabel, $aboutlabel, $compinfo, $compinfo)
      EndSelect
   WEnd
   DllClose($dll)

   GUIDelete()

   Exit
EndFunc  ;==>_Main

Func GUIChangeItems($hidestart, $hideend, $showstart, $showend)
   Local $idx

   For $idx = $hidestart To $hideend
      GUICtrlSetState($idx, $GUI_HIDE)
   Next
   For $idx = $showstart To $showend
      GUICtrlSetState($idx, $GUI_SHOW)
   Next
EndFunc  ;==>GUIChangeItems

Func _MonitorEditState(ByRef $h_gui, ByRef $editCtrl, ByRef $editFlag, ByRef $treeview, ByRef $dll, ByRef $msg)

   Local $focus = ControlGetFocus(WinGetTitle($h_gui))
   If $msg = $GUI_EVENT_PRIMARYDOWN And (StringInStr($focus, "SysTreeView3")) Then
      If $editFlag Then
         CancelEdit($treeview)
         $editFlag = 0
      Else
         Rename($treeview)
         $editFlag = 1
      EndIf
   ElseIf $msg = $GUI_EVENT_PRIMARYDOWN And $editFlag Then
      CancelEdit($treeview)
      $editFlag = 0
   EndIf

   Local $pressed = _IsPressedMod($dll)
   If $editFlag And $pressed = 13 Then; pressed enter
      Update($h_gui, $editCtrl, $treeview)
      $editFlag = 0
   ElseIf $editFlag And $pressed = 27 Then; pressed esc
      CancelEdit($treeview)
      $editFlag = 0
   ElseIf Not $editFlag And $pressed = 113 Then; pressed f2
      Rename($treeview)
      $editFlag = 1
   EndIf
   Sleep(50)

EndFunc  ;==>_MonitorEditState

Func Rename(ByRef $treeview)
   Local $itemIndex = GUICtrlSendMsg($treeview, $TVM_GETNEXTITEM, $TVGN_CARET, 0)
   GUICtrlSendMsg($treeview, $TVM_EDITLABELA, 0, $itemIndex)
    HotKeySet("{Esc}","_Cancel")
EndFunc  ;==>Rename

Func Update(ByRef $Gui, ByRef $editCtrl, ByRef $treeview)
   Local $newText = ControlGetText($Gui, "", $editCtrl)
   Local $item = GUICtrlRead($treeview)
   GUICtrlSetData($item, $newText)
   HotKeySet("{Esc}")
   GUICtrlSendMsg($treeview, $TVM_ENDEDITLABELNOW, False, 0)
EndFunc  ;==>Update

Func _Cancel()
; dummy function
EndFunc

Func CancelEdit(ByRef $treeview)
   GUICtrlSendMsg($treeview, $TVM_ENDEDITLABELNOW, True, 0)
   HotKeySet("{Esc}")
EndFunc  ;==>CancelEdit

Func _IsPressedMod($dll = "user32.dll")
   Local $aR, $bRv, $hexKey, $i
   For $i = 8 To 128
      $hexKey = '0x' & Hex($i, 2)
      $aR = DllCall($dll, "int", "GetAsyncKeyState", "int", $hexKey)
      If $aR[0] <> 0 Then Return $i
   Next
   Return 0
EndFunc  ;==>_IsPressedMod
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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