goldenix Posted April 29, 2009 Posted April 29, 2009 (edited) 1) Try this code, you will see that it displays same info no mathr what item you click. 2) How do you get clicked item text? EIDT: Ok i can check what item is selected & then get its index in a constant loop or something, but what if I do not know how many items must be in an array? then I cant declare it. expandcollapse popup#include <GuiConstantsEx.au3> #include <GuiTreeView.au3> #include <GuiImageList.au3> #include <WindowsConstants.au3> ;~ #include <Array.au3> #Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\Desktop\Linkget\Linkget.kxf $GUI = GUICreate("", 676, 378, 203, 289) $hTreeView = _GUICtrlTreeView_Create($GUI, 2, 2, 161, 227) _GUICtrlTreeView_BeginUpdate($hTreeView) For $x = 1 To Random(2, 10, 1) $hItem = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("[%02d] New Item", $x)) Next _GUICtrlTreeView_EndUpdate($hTreeView) ;~ _ArrayDisplay($hItem,'') GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg, $iwParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview $hWndTreeview = $hTreeView If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView) $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hWndTreeview Switch $iCode Case $NM_CLICK ; The user has clicked the left mouse button within the control ConsoleWrite("$NM_CLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _ "-->IDFrom:" & @TAB & $iIDFrom & @LF & _ "-->Code:" & @TAB & $iCode & @LF) Return 0 ; zero to allow the default processing EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFY While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Edited April 30, 2009 by goldenix My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
GaryFrost Posted April 29, 2009 Posted April 29, 2009 expandcollapse popup#include <GuiConstantsEx.au3> #include <GuiTreeView.au3> #include <GuiImageList.au3> #include <WindowsConstants.au3> ;~ #include <Array.au3> Global $fClicked = False #region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\Desktop\Linkget\Linkget.kxf $GUI = GUICreate("", 676, 378, 203, 289) $hTreeView = _GUICtrlTreeView_Create($GUI, 2, 2, 161, 227) _GUICtrlTreeView_BeginUpdate($hTreeView) For $x = 1 To Random(2, 10, 1) $hItem = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("[%02d] New Item", $x)) Next _GUICtrlTreeView_EndUpdate($hTreeView) ;~ _ArrayDisplay($hItem,'') GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") GUISetState(@SW_SHOW) #endregion ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\Desktop\Linkget\Linkget.kxf Func _TreeView_GetText(ByRef $hWndTreeview) ConsoleWrite("-->Selection Text: " & _GUICtrlTreeView_GetText($hWndTreeview, _GUICtrlTreeView_GetSelection($hWndTreeview))) EndFunc ;==>_TreeView_GetText Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg, $iwParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview $hWndTreeview = $hTreeView If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView) $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hWndTreeview Switch $iCode Case $NM_CLICK ; The user has clicked the left mouse button within the control ConsoleWrite("$NM_CLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _ "-->IDFrom:" & @TAB & $iIDFrom & @LF & _ "-->Code:" & @TAB & $iCode & @LF) $fClicked = True Return 0 ; zero to allow the default processing EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFY While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case Else If $fClicked = True Then _TreeView_GetText($hTreeView) $fClicked = False EndIf EndSwitch WEnd SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference.
Moderators Melba23 Posted April 29, 2009 Moderators Posted April 29, 2009 goldenix,Use a flag within the WM_NOTIFY function. Use _Array Add to create an array of the correct size:expandcollapse popup#include <GuiConstantsEx.au3> #include <GuiTreeView.au3> #include <GuiImageList.au3> #include <WindowsConstants.au3> #include <Array.au3> Global $fClick = False Global $aItem_Handles[1] #Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\Desktop\Linkget\Linkget.kxf $GUI = GUICreate("", 676, 378, 203, 289) $hTreeView = _GUICtrlTreeView_Create($GUI, 2, 2, 161, 227) _GUICtrlTreeView_BeginUpdate($hTreeView) For $x = 1 To Random(2, 10, 1) $hItem = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("[%02d] New Item", $x)) _ArrayAdd($aItem_Handles, $hItem) $aItem_Handles[0] +=1 Next _GUICtrlTreeView_EndUpdate($hTreeView) ;~ _ArrayDisplay($hItem,'') GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch If $fClick Then $hItem = _GUICtrlTreeView_GetSelection ($hTreeView) $sText = _GUICtrlTreeView_GetText($hTreeView, $hItem) ConsoleWrite($sText & " or directly ") ConsoleWrite(_GUICtrlTreeView_GetText($hTreeView, _GUICtrlTreeView_GetSelection ($hTreeView)) & @CRLF) $fClick = False EndIf WEnd Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg, $iwParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview $hWndTreeview = $hTreeView If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView) $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hWndTreeview Switch $iCode Case $NM_CLICK; The user has clicked the left mouse button within the control $fClick = True EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFYM23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
goldenix Posted April 29, 2009 Author Posted April 29, 2009 (edited) Explain the meaning of ByRef please. this makes no sense, if I change the code a little it stops showing the right numbers. Cuz i need to change it, since i will be using on event mode 1_TreeView_GetText($hTreeView)_TreeView_GetText(ByRef $hWndTreeview)Why wont this work?expandcollapse popup#include <GuiConstantsEx.au3> #include <GuiTreeView.au3> #include <GuiImageList.au3> #include <WindowsConstants.au3> #include <Array.au3> ;~ Global $fClick = False Global $aItem_Handles[1] #Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\Desktop\Linkget\Linkget.kxf $GUI = GUICreate("", 676, 378, 203, 289) $hTreeView = _GUICtrlTreeView_Create($GUI, 2, 2, 161, 227) _GUICtrlTreeView_BeginUpdate($hTreeView) For $x = 1 To 10 $hItem = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("[%02d] New Item", $x)) _ArrayAdd($aItem_Handles, $hItem) $aItem_Handles[0] +=1 Next _GUICtrlTreeView_EndUpdate($hTreeView) GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch ;~ ToolTip($fClick,0,0) ;~ If $fClick Then ;~ ;~ $hItem = _GUICtrlTreeView_GetSelection ($hTreeView) ;~ $sText = _GUICtrlTreeView_GetText($hTreeView, $hItem) ;~ ConsoleWrite($sText & " or directly ") ;~ ConsoleWrite(_GUICtrlTreeView_GetText($hTreeView, _GUICtrlTreeView_GetSelection ($hTreeView)) & @CRLF) ;~ ;~ $fClick = False ;~ EndIf WEnd Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg, $iwParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview $hWndTreeview = $hTreeView If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView) $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hWndTreeview Switch $iCode Case $NM_CLICK; The user has clicked the left mouse button within the control $hItem = _GUICtrlTreeView_GetSelection ($hTreeView) $sText = _GUICtrlTreeView_GetText($hTreeView, $hItem) ConsoleWrite($sText & " or directly ") ConsoleWrite(_GUICtrlTreeView_GetText($hTreeView, _GUICtrlTreeView_GetSelection ($hTreeView)) & @CRLF) ;~ $fClick = True EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFY Edited April 29, 2009 by goldenix My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Moderators Melba23 Posted April 29, 2009 Moderators Posted April 29, 2009 goldenix,If you look carefully, you will see that you always get the previously selected item in this case - I presume this is because the selection has not been updated within the WM_NOTIFY function. If you want to use OnEvent mode, try using Adlib to get the selected item:expandcollapse popup#include <GuiConstantsEx.au3> #include <GuiTreeView.au3> #include <GuiImageList.au3> #include <WindowsConstants.au3> #include <Array.au3> Global $fClick = False Global $aItem_Handles[1] AdLibEnable("Get_Text") #Region ### START Koda GUI section ### Form=C:\Documents and Settings\Administrator\Desktop\Linkget\Linkget.kxf $GUI = GUICreate("", 676, 378, 203, 289) $hTreeView = _GUICtrlTreeView_Create($GUI, 2, 2, 161, 227) _GUICtrlTreeView_BeginUpdate($hTreeView) For $x = 1 To 10 $hItem = _GUICtrlTreeView_Add($hTreeView, 0, StringFormat("[%02d] New Item", $x)) _ArrayAdd($aItem_Handles, $hItem) $aItem_Handles[0] +=1 Next _GUICtrlTreeView_EndUpdate($hTreeView) GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Func Get_Text() If $fClick Then $hItem = _GUICtrlTreeView_GetSelection ($hTreeView) $sText = _GUICtrlTreeView_GetText($hTreeView, $hItem) ConsoleWrite($sText & " or directly ") ConsoleWrite(_GUICtrlTreeView_GetText($hTreeView, _GUICtrlTreeView_GetSelection ($hTreeView)) & @CRLF) $fClick = False EndIf EndFunc Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam) #forceref $hWnd, $iMsg, $iwParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview $hWndTreeview = $hTreeView If Not IsHWnd($hTreeView) Then $hWndTreeview = GUICtrlGetHandle($hTreeView) $tNMHDR = DllStructCreate($tagNMHDR, $ilParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hWndTreeview Switch $iCode Case $NM_CLICK; The user has clicked the left mouse button within the control $fClick = True EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFYM23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
goldenix Posted April 29, 2009 Author Posted April 29, 2009 (edited) goldenix,If you look carefully, you will see that you always get the previously selected item in this case - I presume this is because the selection has not been updated within the WM_NOTIFY function. If you want to use OnEvent mode, try using Adlib to get the selected itemYes I did notice that, but I was unable to figure Out why was this happening. It made no sense.I still do not fully understand why do we need those switches $fClick = False/True? And why wont it work if we put consolewrite into WM_NOTIFY function? No mather how I look at it its, its no different than if we use switches.you said that its probably because the selection has not been updated within the WM_NOTIFY function. But what is the WM_NOTIFY function? what exactely it does & how does it work? Edited April 29, 2009 by goldenix My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Moderators Melba23 Posted April 30, 2009 Moderators Posted April 30, 2009 goldenix,I wil try and explain - although I start by admitting that I do not have a deep knowledge of these areas. However, I am sure someone will correct me if make major errors! :-)The _NOTIFY functions trap Windows messages - that is the internal messages that Windows flashes aroud the system to tell everything what is going on. Examples would be: which window has focus, whether the mouse has moved or clicked, an edit box has changed content..... There is a (non-exhaustive) list in the Help file under "Appendix - Windows Message Codes". Using GUIRegisterMsg, we can intercept these messages to determine when an event has occurred which we would not normally be able to detect. In this case we looked for the click on a TreeView column:$tNMHDR = DllStructCreate($tagNMHDR, $ilParam) ; Get the latest message information $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) ; What sent it? $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") ; What is the message? Switch $hWndFrom Case $hWndTreeview ; If it was sent by the Treeview Switch $iCode Case $NM_CLICK ; If it was a mouseclickNow at this point all Windows knows is that a mouse click has happened on the TreeView - in particular AutoIt has not yet had time to do anything with it. This explains why you were getting the previously selected item when you tried to run the _GUICtrlTreeView_GetSelection function at this point - it had not been updated by this new click.As to why ConsoleWrite, etc, often do not work in the _NOTIFY function I cannot say. But I do know that the Help file states that : "the return to the system should be as fast as possible !!!". So I have tended to use the function to set a flag which I can deal with in slower time later in the code (as long as the logic permits this). There is nothing to prevent you having more complex code within the function - but the results are sometimes not what you expect, as you have seen, and you can hang the script if you delay too long (by opening a MsgBox for example).The flag ($fClick here) is set to False when created. It is then set to True by the _NOTIFY function to indicate that the event we are checking for has taken place. This flag is then picked up in the code (either in a GUIGetMsg loop or by Adlib as in the second example) and acted upon. It is important to reset the flag to False or it will trigger every time! Incidentally, you can reduce the Adlib overhead by only enabling the Adlib function when you are expecting something to happen on the TreeView - sorry, I did not mention that in my last post.I hope that is clearer. If you have further questions, I will try to answer - but as I stated earlier, I am hitting the boundaries of my knowledge here.M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
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