Blaxxun Posted February 19 Share Posted February 19 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 Link to comment Share on other sites More sharing options...
Nine Posted February 19 Share Posted February 19 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) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Solution Andreik Posted February 19 Solution Share Posted February 19 (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 by Andreik When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Blaxxun Posted February 20 Author Share Posted February 20 @Nine @Andreik Thank you very much for the solution. It works fine now! Thanks! 🙂 Link to comment Share on other sites More sharing options...
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