Jump to content
Sign in to follow this  
Chimp

how to merge TreeView event handling from 2 examples in 1?

Recommended Posts

Chimp

When in a treeview you click on a node (say the + sign. Without touching any of the items but just the + sign) the corresponding subtree is expanded, but the item with focus still remains unchanged.
since I would like to take actions on the subtree items (childs)  just when someone clicks on the + sign, I searched in the forum and found 2 examples by Melba23 to intercept that kind of action and get a reference to the just expanded node (I mean the first item on the right side of the plus sign).
In the >first example is possible to catch the expanded/compressed event and also to know if a node was expanded or collapsed, (without the handle)

while in the >second example I can get the handle of the item but not if action was expanded or compressed.

how can I merge both examples and get all infos at once?

thanks


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
Danyfirex

Hi. Maybe you're looking for this:

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

Global $hTreeView, $fExpanded = 0
Global $ahRoot[5][2] = [[0, 0],[0, 0],[0, 0],[0, 0]]

$hGUI = GUICreate("Test", 220, 250)

$hTreeView = GUICtrlCreateTreeView(10, 10, 200, 200, -1, $WS_EX_CLIENTEDGE)

$ahRoot[1][0] = _GUICtrlTreeView_InsertItem($hTreeView, "RootItem1")
_GUICtrlTreeView_InsertItem($hTreeView, "SubItem1", $ahRoot[1][0])

$ahRoot[2][0] = _GUICtrlTreeView_InsertItem($hTreeView, "RootItem2")
_GUICtrlTreeView_InsertItem($hTreeView, "SubItem2", $ahRoot[2][0])

$ahRoot[3][0] = _GUICtrlTreeView_InsertItem($hTreeView, "RootItem3")
_GUICtrlTreeView_InsertItem($hTreeView, "SubItem3", $ahRoot[3][0])

$ahRoot[4][0] = _GUICtrlTreeView_InsertItem($hTreeView, "RootItem4")
_GUICtrlTreeView_InsertItem($hTreeView, "SubItem4", $ahRoot[4][0])

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

While 1
    $iMsg = GUIGetMsg()
    Select
        Case $iMsg = $GUI_EVENT_CLOSE
            Exit
        Case $fExpanded = 1
            ; node closed so reset all nodes to current states
            For $i = 1 To 4
                If _GUICtrlTreeView_GetExpanded($hTreeView, $ahRoot[$i][0]) = True Then
                    $ahRoot[$i][1] = 1 ; set expanded flag
                Else
                    $ahRoot[$i][1] = 0 ; set closed flag
                EndIf
            Next
            $fExpanded = 0
        Case $fExpanded = 2
            ; node opened so check which changed
            For $i = 1 To 4
                If _GUICtrlTreeView_GetExpanded($hTreeView, $ahRoot[$i][0]) <> $ahRoot[$i][1] Then
                    $sText = _GUICtrlTreeView_GetText($hTreeView, $ahRoot[$i][0])
                EndIf
            Next
            MsgBox(0x40000, "+ Clicked", $sText)
            $fExpanded = 0
    EndSelect
WEnd

Func WM_Notify_Events($hWndGUI, $iMsgID, $wParam, $lParam)

    #forceref $hWndGUI, $iMsgID
    Local $tagNMHDR = DllStructCreate("int;int;int;int", $lParam)

      ; Create NMTREEVIEW structure
    Local $tStruct = DllStructCreate("struct;hwnd hWndFrom;uint_ptr IDFrom;INT Code;endstruct;" & _
            "uint Action;struct;uint OldMask;handle OldhItem;uint OldState;uint OldStateMask;" & _
            "ptr OldText;int OldTextMax;int OldImage;int OldSelectedImage;int OldChildren;lparam OldParam;endstruct;" & _
            "struct;uint NewMask;handle NewhItem;uint NewState;uint NewStateMask;" & _
            "ptr NewText;int NewTextMax;int NewImage;int NewSelectedImage;int NewChildren;lparam NewParam;endstruct;" & _
            "struct;long PointX;long PointY;endstruct", $lParam)

    If @error Then Return
    Local $iEvent = DllStructGetData($tagNMHDR, 3)
    Select
        Case $wParam = $hTreeView
            Switch $iEvent
                Case $TVN_ITEMEXPANDEDW, $TVN_ITEMEXPANDEDA
                    $fExpanded = DllStructGetData($tagNMHDR, 4) ; 1 = node closed, 2 = node expanded
                    Local $hItem = DllStructGetData($tStruct, "NewhItem")
                    ConsoleWrite(  _GUICtrlTreeView_GetText($hTreeView, $hItem) & ": " & (($fExpanded  = 1) ? "node closed" : "node expanded")  & @CRLF)
            EndSwitch
    EndSelect
    $tagNMHDR = 0
    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_Notify_Events

Saludos

  • Like 1

Share this post


Link to post
Share on other sites
Chimp

thanks Danyfirex, exactly what I was trying to achieve.
not clear to me the mechanism to get events parameters via DllStructCreate. Also I was wondering why are necessary 2 DllStructCreate to get reference to that parameters. couldn't be all in only one DllStructCreate ?..... just a curiosity, nevermind.

also, why you take track in the array of the state of the nodes when you get it from the nodes itself?

#include <GUIConstantsEx.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>
#include <TreeViewConstants.au3>
#include <GuiStatusBar.au3>
#include <GUITreeView.au3>

$hMain = GUICreate("TreeView test", 300, 422, 100, 100)
Global $hTree = GUICtrlCreateTreeView(5, 5, 290, 390)
$StatusBar1 = _GUICtrlStatusBar_Create($hMain)
Local $sep = "."
For $i0 = 1 To Random(5, 8, 1)
    $cRoot = GUICtrlCreateTreeViewItem("Root " & $i0, $hTree)
    For $i1 = 1 To Random(3, 5, 1)
        $cParent1 = GUICtrlCreateTreeViewItem("1° level Child " & $i0 & $sep & $i1, $cRoot)
        For $i2 = 1 To Random(4, 6, 1)
            $cParent2 = GUICtrlCreateTreeViewItem("2° level Child " & $i0 & $sep & $i1 & $sep & $i2, $cParent1)
            For $i3 = 1 To Random(2, 5, 1)
                $cParent3 = GUICtrlCreateTreeViewItem("3° level Child " & $i0 & $sep & $i1 & $sep & $i2 & $sep & $i3, $cParent2)
                For $i4 = 1 To Random(4, 7, 1)
                    $cParent4 = GUICtrlCreateTreeViewItem("4° level Child " & $i0 & $sep & $i1 & $sep & $i2 & $sep & $i3 & $sep & $i4, $cParent3)
                Next
            Next
        Next
    Next
Next
GUIRegisterMsg($WM_NOTIFY, "_WM_Notify_Events")
GUISetState()

While 1
    $iMsg = GUIGetMsg()
    Select
        Case $iMsg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
WEnd

Func _WM_Notify_Events($hWndGUI, $iMsgID, $wParam, $lParam)
    #forceref $hWndGUI, $iMsgID
    Select
        Case $wParam = $hTree ; was event fired by $hTree ?
            Local $aNodeState[2] = ["node closed", "node expanded"]
            Local $tagNMHDR = DllStructCreate("int;int;int;int", $lParam)
            ; Create NMTREEVIEW structure
            Local $tStruct = DllStructCreate("struct;hwnd hWndFrom;uint_ptr IDFrom;INT Code;endstruct;" & _
                    "uint Action;struct;uint OldMask;handle OldhItem;uint OldState;uint OldStateMask;" & _
                    "ptr OldText;int OldTextMax;int OldImage;int OldSelectedImage;int OldChildren;lparam OldParam;endstruct;" & _
                    "struct;uint NewMask;handle NewhItem;uint NewState;uint NewStateMask;" & _
                    "ptr NewText;int NewTextMax;int NewImage;int NewSelectedImage;int NewChildren;lparam NewParam;endstruct;" & _
                    "struct;long PointX;long PointY;endstruct", $lParam)
            If @error Then Return
            Local $iEvent = DllStructGetData($tagNMHDR, 3)
            Switch $iEvent
                Case $TVN_ITEMEXPANDEDW, $TVN_ITEMEXPANDEDA
                    $fExpanded = DllStructGetData($tagNMHDR, 4) ; 1 = node closed, 2 = node expanded
                    Local $hItem = DllStructGetData($tStruct, "NewhItem")
                    _GUICtrlStatusBar_SetText($StatusBar1, _GUICtrlTreeView_GetText($hTree, $hItem) & ": " & $aNodeState[2 = $fExpanded])
            EndSwitch
            $tagNMHDR = 0
            $tStruct = 0
    EndSelect
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_Notify_Events


Ok Danyfirex, thanks again for your solution.

Saludos :)


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
Danyfirex

ups yes. You only need the NMTREEVIEW structure cuz NMHDR is inside. look this.

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

Global $hTreeView, $fExpanded = 0
Global $ahRoot[5][2] = [[0, 0],[0, 0],[0, 0],[0, 0]]

$hGUI = GUICreate("Test", 220, 250)

$hTreeView = GUICtrlCreateTreeView(10, 10, 200, 200, -1, $WS_EX_CLIENTEDGE)

$ahRoot[1][0] = _GUICtrlTreeView_InsertItem($hTreeView, "RootItem1")
_GUICtrlTreeView_InsertItem($hTreeView, "SubItem1", $ahRoot[1][0])

$ahRoot[2][0] = _GUICtrlTreeView_InsertItem($hTreeView, "RootItem2")
_GUICtrlTreeView_InsertItem($hTreeView, "SubItem2", $ahRoot[2][0])

$ahRoot[3][0] = _GUICtrlTreeView_InsertItem($hTreeView, "RootItem3")
_GUICtrlTreeView_InsertItem($hTreeView, "SubItem3", $ahRoot[3][0])

$ahRoot[4][0] = _GUICtrlTreeView_InsertItem($hTreeView, "RootItem4")
_GUICtrlTreeView_InsertItem($hTreeView, "SubItem4", $ahRoot[4][0])

GUISetState()

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

While 1
    $iMsg = GUIGetMsg()
    Select
        Case $iMsg = $GUI_EVENT_CLOSE
            Exit
        Case $fExpanded = 1
            ; node closed so reset all nodes to current states
            For $i = 1 To 4
                If _GUICtrlTreeView_GetExpanded($hTreeView, $ahRoot[$i][0]) = True Then
                    $ahRoot[$i][1] = 1 ; set expanded flag
                Else
                    $ahRoot[$i][1] = 0 ; set closed flag
                EndIf
            Next
            $fExpanded = 0
        Case $fExpanded = 2
            ; node opened so check which changed
            For $i = 1 To 4
                If _GUICtrlTreeView_GetExpanded($hTreeView, $ahRoot[$i][0]) <> $ahRoot[$i][1] Then
                    $sText = _GUICtrlTreeView_GetText($hTreeView, $ahRoot[$i][0])
                EndIf
            Next
            MsgBox(0x40000, "+ Clicked", $sText)
            $fExpanded = 0
    EndSelect
WEnd

Func WM_Notify_Events($hWndGUI, $iMsgID, $wParam, $lParam)

    #forceref $hWndGUI, $iMsgID
      ; Create NMTREEVIEW structure
    Local $tStruct = DllStructCreate("struct;hwnd hWndFrom;uint_ptr IDFrom;INT Code;endstruct;" & _
            "uint Action;struct;uint OldMask;handle OldhItem;uint OldState;uint OldStateMask;" & _
            "ptr OldText;int OldTextMax;int OldImage;int OldSelectedImage;int OldChildren;lparam OldParam;endstruct;" & _
            "struct;uint NewMask;handle NewhItem;uint NewState;uint NewStateMask;" & _
            "ptr NewText;int NewTextMax;int NewImage;int NewSelectedImage;int NewChildren;lparam NewParam;endstruct;" & _
            "struct;long PointX;long PointY;endstruct", $lParam)

    If @error Then Return
    Local $iEvent = DllStructGetData($tStruct, 3)
    Select
        Case $wParam = $hTreeView
            Switch $iEvent
                Case $TVN_ITEMEXPANDEDW, $TVN_ITEMEXPANDEDA
                    $fExpanded = DllStructGetData($tStruct, 4) ; 1 = node closed, 2 = node expanded
                    Local $hItem = DllStructGetData($tStruct, "NewhItem")
                    ConsoleWrite(  _GUICtrlTreeView_GetText($hTreeView, $hItem) & ": " & (($fExpanded  = 1) ? "node closed" : "node expanded")  & @CRLF)
            EndSwitch
    EndSelect
    $tStruct = 0
    Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_Notify_Events

All is in msdn reference

Saludos

  • Like 2

Share this post


Link to post
Share on other sites
Chimp

Hi Danyfirex

.... so you simply removed this line

Local $tagNMHDR = DllStructCreate("int;int;int;int", $lParam)

from your previous listing and it still works as well :blink:

're doing magic tricks?... :o

I'll try to undestand somethink from the link you posted.

Thanks a lot :P


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
Danyfirex

Yes, because the structure keep the index by number.

 

in the first code, both structure are same(same content) because I supplied the same pointer.

Saludos

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  

×