Jump to content
Sign in to follow this  
goldenix

[Solved]_GUICtrlTreeView_GetText

Recommended Posts

goldenix

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.

#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 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]

Share this post


Link to post
Share on other sites
GaryFrost

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

 

Share this post


Link to post
Share on other sites
Melba23

goldenix,

Use a flag within the WM_NOTIFY function. Use _Array Add to create an array of the correct size:

#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_NOTIFY

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
goldenix

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?

#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 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]

Share this post


Link to post
Share on other sites
Melba23

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:

#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_NOTIFY

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
goldenix

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

Yes 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 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]

Share this post


Link to post
Share on other sites
Melba23

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 mouseclick

Now 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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.