Jump to content

TreeView - Directory - Print name of current Item


Go to solution Solved by Andreik,

Recommended Posts

Posted

Hello Forum,

I have a little Script here that generates a Directory Tree View of a given path.
It works fine so far.
Only thing that i cant figure out is:
It prints the name of the previously selected Item and not of the current selected Item.
Why is that?
Thank you!

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

Global $sPath = @DesktopDir
Global $hGui = GUICreate('TreeView-Example', 400, 600)
Global $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
Global $hTreeView = _GUICtrlTreeView_Create($hGui, 10, 10, 380, 580, $iStyle, $WS_EX_CLIENTEDGE)

Opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
CreateTVItems($hTreeView, 0, $sPath)
GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd

Func CreateTVItems($hWnd, $hParent, $sPath)
    Local $aFolder, $aFiles, $hItem
    If StringRight($sPath, 1) <> '\' Then $sPath &= '\'

    $aFolder = _FileListToArray($sPath, '*', $FLTA_FOLDERS)
    If Not @error Then
        For $i = 1 To $aFolder[0]
            $hItem = _GUICtrlTreeView_AddChild($hWnd, $hParent, $aFolder[$i])
            CreateTVItems($hWnd, $hItem, $sPath & $aFolder[$i]) ; Recursive call for subfolders
        Next
    EndIf

    $aFiles = _FileListToArray($sPath, '*', $FLTA_FILES)
    If @error Then Return

    For $i = 1 To $aFiles[0]
        $hItem = _GUICtrlTreeView_AddChild($hWnd, $hParent, $aFiles[$i])
    Next
EndFunc   ;==>CreateTVItems

Func SpecialEvents()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            ConsoleWrite("Window Close Pressed > ID = " & @GUI_CtrlId & " WinHandle = " & @GUI_WinHandle & @LF)
            GUIDelete()
            Exit

        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
            ConsoleWrite("Window Minimized     > ID = " & @GUI_CtrlId & " WinHandle = " & @GUI_WinHandle & @LF)

        Case @GUI_CtrlId = $GUI_EVENT_RESTORE
            ConsoleWrite("Window Restored      > ID = " & @GUI_CtrlId & " WinHandle = " & @GUI_WinHandle & @LF)

    EndSelect
EndFunc   ;==>SpecialEvents

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom

        Case $hTreeView

            Switch $iCode

                Case $NM_CLICK

                    Local $hTVItemSel = _GUICtrlTreeView_GetSelection($hTreeView)
                    Local $sText = _GUICtrlTreeView_GetText($hTreeView, $hTVItemSel)
                    ConsoleWrite("Item: " & $hTVItemSel & "  " & $sText & @LF)

            EndSwitch

    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

 

  • Solution
Posted (edited)

Or depending of your application logic you can also use $TVN_SELCHANGINGW to get these info just before the new item it's selected. Here is the full code:

#include <File.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <StringConstants.au3>

Global $sPath = @DesktopDir
Global $hGui = GUICreate('TreeView-Example', 400, 600)
Global $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
Global $hTreeView = _GUICtrlTreeView_Create($hGui, 10, 10, 380, 580, $iStyle, $WS_EX_CLIENTEDGE)

Opt("GUIOnEventMode", 1)
GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
CreateTVItems($hTreeView, 0, $sPath)
GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd

Func CreateTVItems($hWnd, $hParent, $sPath)
    Local $aFolder, $aFiles, $hItem
    If StringRight($sPath, 1) <> '\' Then $sPath &= '\'

    $aFolder = _FileListToArray($sPath, '*', $FLTA_FOLDERS)
    If Not @error Then
        For $i = 1 To $aFolder[0]
            $hItem = _GUICtrlTreeView_AddChild($hWnd, $hParent, $aFolder[$i])
            CreateTVItems($hWnd, $hItem, $sPath & $aFolder[$i]) ; Recursive call for subfolders
        Next
    EndIf

    $aFiles = _FileListToArray($sPath, '*', $FLTA_FILES)
    If @error Then Return

    For $i = 1 To $aFiles[0]
        $hItem = _GUICtrlTreeView_AddChild($hWnd, $hParent, $aFiles[$i])
    Next
EndFunc   ;==>CreateTVItems

Func SpecialEvents()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            ConsoleWrite("Window Close Pressed > ID = " & @GUI_CtrlId & " WinHandle = " & @GUI_WinHandle & @LF)
            GUIDelete()
            Exit

        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
            ConsoleWrite("Window Minimized     > ID = " & @GUI_CtrlId & " WinHandle = " & @GUI_WinHandle & @LF)

        Case @GUI_CtrlId = $GUI_EVENT_RESTORE
            ConsoleWrite("Window Restored      > ID = " & @GUI_CtrlId & " WinHandle = " & @GUI_WinHandle & @LF)

    EndSelect
EndFunc   ;==>SpecialEvents

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = DllStructGetData($tNMHDR, "hWndFrom")
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hTreeView
            Switch $iCode
                Case $TVN_SELCHANGEDW ; or $TVN_SELCHANGINGW
                    Local $tNMTREEVIEW = DllStructCreate($tagNMTREEVIEW, $lParam)
                    ConsoleWrite('Old: ' & _GUICtrlTreeView_GetText($hTreeView, $tNMTREEVIEW.OldhItem) & @CRLF)
                    ConsoleWrite('New: ' & _GUICtrlTreeView_GetText($hTreeView, $tNMTREEVIEW.NewhItem) & @CRLF & @CRLF)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Both $TVN_SELCHANGEDW and $TVN_SELCHANGINGW gives you access to the old item and the new item.

Edited by Andreik

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...