Jump to content

Rename tree elements dinamically


waltzie
 Share

Recommended Posts

Check out the helpfile for _GUICtrlTreeView_EditText(). Here's a crude example.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <WinAPI.au3>

GUICreate("My GUI with treeview", 350, 215)

$treeview = GUICtrlCreateTreeView(6, 6, 338, 204, BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)

$generalitem = GUICtrlCreateTreeViewItem("General", $treeview)
GUICtrlSetColor(-1, 0x0000C0)
$displayitem = GUICtrlCreateTreeViewItem("Display", $treeview)
GUICtrlSetColor(-1, 0x0000C0)
$aboutitem = GUICtrlCreateTreeViewItem("About", $generalitem)
$compitem = GUICtrlCreateTreeViewItem("Computer", $generalitem)
$useritem = GUICtrlCreateTreeViewItem("User", $generalitem)
$resitem = GUICtrlCreateTreeViewItem("Resolution", $displayitem)
$otheritem = GUICtrlCreateTreeViewItem("Other", $displayitem)

GUICtrlSetState($generalitem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))    ; Expand the "General"-item and paint in bold
GUICtrlSetState($displayitem, BitOR($GUI_EXPAND, $GUI_DEFBUTTON))    ; Expand the "Display"-item and paint in bold

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
WEnd

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hWndTreeView, $tNMHDR, $hWndFrom, $iCode, $los, $pos
   
    $hWndTreeView = $treeview
    If Not IsHWnd($hWndTreeView) Then $hWndTreeView = GUICtrlGetHandle($hWndTreeView)
   
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
   
    Switch $hWndFrom
        Case $hWndTreeView
            Switch $iCode
                Case $NM_RCLICK
                    Local $tPOINT = _WinAPI_GetMousePos(True, $hWndFrom)
                    Local $iX = DllStructGetData($tPOINT, "X")
                    Local $iY = DllStructGetData($tPOINT, "Y")
                   
                    Local $tTVHITTESTINFO  = _GUICtrlTreeView_HitTestEx($hWndFrom, $iX, $iY)
                    Local $iFlags = DllStructGetData($tTVHITTESTINFO, "Flags")
                   
                    If BitAND($iFlags, $TVHT_ONITEM) Or BitAND($iFlags, $TVHT_ONITEMLABEL) Then
                        $hItem = DllStructGetData($tTVHITTESTINFO, "Item")
                        _GUICtrlTreeView_SelectItem($hWndFrom, $hItem, $TVGN_CARET)
                        _GUICtrlTreeView_EditText($hWndTreeView, $hItem)
                        $pos = MouseGetPos()
                        Do
                            sleep(500)
                            $loc = MouseGetPos()
                        Until $loc[0] < $pos[0]-20 Or $loc[0] > $pos[0]+20 Or $loc[1] < $pos[1]-20 Or $loc[1] > $pos[1]+20
                        _GUICtrlTreeView_EndEdit($hWndTreeView)
                    EndIf
            EndSwitch
    EndSwitch
   
    Return $GUI_RUNDEFMSG
EndFunc
You'll probably want to code some key capture stuff too to handle the Esc and Enter Key, as you'd expect them to work in a traditional GUI. This example stops the edit of an item when the mouse cursor moves 20+ pixels X or Y away from it's start place when right clicking an item. Hope this helps.

edit: Disabling the ability to edit a treeview can be accomplished by changing the control's style and revoking (or applying) $TVS_EDITLABELS flag.

Edited by spudw2k
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...