Blaxxun Posted February 19, 2024 Posted February 19, 2024 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! expandcollapse popup#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
Nine Posted February 19, 2024 Posted February 19, 2024 Change $NM_CLICK with $TVN_SELCHANGEDW. At click the selection hasn't change yet. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
Solution Andreik Posted February 19, 2024 Solution Posted February 19, 2024 (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: expandcollapse popup#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 February 19, 2024 by Andreik
Blaxxun Posted February 20, 2024 Author Posted February 20, 2024 @Nine @Andreik Thank you very much for the solution. It works fine now! Thanks! 🙂
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now