Jump to content

Add & Remove tree elements dinamically


waltzie
 Share

Recommended Posts

Hi,

I'v try to make a dinamic tree.

Add is not a problem, but search & delete selected items is an other story ...

Here is an example that need to be filled with the REMOVE function.

Any help is really appreciated :)

Thanks mutch

#Include <GUIConstants.au3>
#include <GuiConstantsEx.au3>
#Include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
; Create GUI
    $gui = GUICreate("Tree example", 400, 400,-1,-1,$WS_TILEDWINDOW)
        $maintree = GUICtrlCreateTreeView(10, 10, 120, 150)
        $aboutitem = GUICtrlCreateTreeViewItem("About", $maintree)
        $miscitem = GUICtrlCreateTreeViewItem("Misc", $aboutitem)
        $aboutitemMenu = GUICtrlCreateContextMenu($aboutitem)
        $aboutitemMenu_add = GUICtrlCreateMenuItem("&Add leaf", $aboutitemMenu)
        $miscitemMenu = GUICtrlCreateContextMenu($miscitem)
        $miscitemMenu_remove = GUICtrlCreateMenuItem("&Remove leaf", $miscitemMenu)
    GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
    Local $i = 0
; GUI MESSAGE LOOP
    While 1
        $msg = GuiGetMsg() 
        Select 
            Case $msg = $miscitemMenu_remove
                if $i >0 Then
                ;????????
                ;
                ; PUT HERE THE CODE TO DELETE SELECTED ITEM AND/OR WORK WITH IT BY CONTEXT MENU!
                ;
                ;????????
                ;_GUICtrlTreeView_Delete($hWndFrom, $hItem)
                $i=$i-1
                EndIf
            case $msg = $aboutitemMenu_add
                $newleaf=GUICtrlCreateTreeViewItem("Leaf-" & $i, $aboutitem)
                $newleafMenu = GUICtrlCreateContextMenu($newleaf)
                $newleafMenu_remove = GUICtrlCreateMenuItem("&Remove leaf", $newleafMenu)
                $i=$i+1
            case $msg = $GUI_EVENT_CLOSE
                Exit
        EndSelect
; wait till gui is closed
    WEnd
    
    Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hWndTreeView, $tNMHDR, $hWndFrom, $iCode
    $hWndTreeView = $maintree
    If Not IsHWnd($hWndTreeView) Then $hWndTreeView = GUICtrlGetHandle($hWndTreeView)
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndTreeView
            Local $tPOINT = _WinAPI_GetMousePos(True, $hWndFrom)
            Local $iX = DllStructGetData($tPOINT, "X")
            Local $iY = DllStructGetData($tPOINT, "Y")
            Local $tTVHITTESTINFO  = _GUICtrlTreeView_HitTestEx($hWndFrom, $iX, $iY)
            Local $iFlags = DllStructGetData($tTVHITTESTINFO, "Flags")
            Switch $iCode
                Case $NM_RCLICK
                    If BitAND($iFlags, $TVHT_ONITEM) Or BitAND($iFlags, $TVHT_ONITEMLABEL) Then
                        $hItem = DllStructGetData($tTVHITTESTINFO, "Item")
                        _GUICtrlTreeView_SelectItem($hWndFrom, $hItem, $TVGN_CARET)
                EndIf
            Case  $NM_DBLCLK
                    If BitAND($iFlags, $TVHT_ONITEM) Or BitAND($iFlags, $TVHT_ONITEMLABEL) Then
                        $hItem = DllStructGetData($tTVHITTESTINFO, "Item")
                        MsgBox(0,"Double Click",_GUICtrlTreeView_GetText($hWndFrom, $hItem))
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc
Edited by waltzie
Link to comment
Share on other sites

@waltzie

Quick example:

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

Global $hItem = 0

$gui = GUICreate("Tree example", 400, 400)

$maintree = GUICtrlCreateTreeView(10, 10, 150, 200)

$aboutitem = GUICtrlCreateTreeViewItem("Parent", $maintree)

For $i = 1 To 10
    GUICtrlCreateTreeViewItem("Child " & $i, $aboutitem)
Next

$cDummyMenu = GUICtrlCreateDummy()

$aboutitemMenu = GUICtrlCreateContextMenu($cDummyMenu)
$hMenu = GUICtrlGetHandle($aboutitemMenu)

$aboutitemMenu_add = GUICtrlCreateMenuItem("Add Item", $aboutitemMenu)

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $cDummyMenu
            _TrackPopupMenu($hMenu, 0, MouseGetPos(0), MouseGetPos(1), $gui)
        Case $msg = $aboutitemMenu_add
            If GUICtrlRead($aboutitemMenu_add, 1) = "Add Item" Then
                GUICtrlCreateTreeViewItem("New Item", $aboutitem)
            Else
                _GUICtrlTreeView_Delete($maintree, $hItem)
            EndIf
    EndSelect
WEnd

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)
    Local $hWndTreeView, $tNMHDR, $hWndFrom, $iCode
    
    $hWndTreeView = $maintree
    If Not IsHWnd($hWndTreeView) Then $hWndTreeView = GUICtrlGetHandle($hWndTreeView)
    
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    Switch $hWndFrom
        Case $hWndTreeView
            Switch $iCode
                Case $NM_RCLICK
                    Local $tPOINT = DllStructCreate("int X;int Y")
                    
                    DllStructSetData($tPOINT, "X", MouseGetPos(0))
                    DllStructSetData($tPOINT, "Y", MouseGetPos(1))
                    
                    _ScreenToClient($hWndTreeView, DllStructGetPtr($tPOINT))
                    Local $iX = DllStructGetData($tPOINT, 1)
                    Local $iY = DllStructGetData($tPOINT, 2)
                    
                    Local $tTVHITTESTINFO  = _GUICtrlTreeView_HitTestEx($hWndTreeView, $iX, $iY)
                    Local $iFlags = DllStructGetData($tTVHITTESTINFO, "Flags")
                    
                    If BitAND($iFlags, $TVHT_ONITEM) Or BitAND($iFlags, $TVHT_ONITEMLABEL) Then
                        $hItem = DllStructGetData($tTVHITTESTINFO, "Item")
                        _GUICtrlTreeView_SelectItem($hWndTreeView, $hItem, $TVGN_CARET)
                        GUICtrlSetData($aboutitemMenu_add, "Delete Item")
                    Else
                        $hItem = 0
                        GUICtrlSetData($aboutitemMenu_add, "Add Item")
                    EndIf
                    
                    GUICtrlSendToDummy($cDummyMenu)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _ScreenToClient($hWnd, $pPoint)
    Local $aRet = DllCall("user32.dll", "int", "ScreenToClient", _
                                                "hwnd", $hWnd, _
                                                "ptr", $pPoint)
    Return $aRet[0]
EndFunc   ;==>_ScreenToClient

Func _TrackPopupMenu($hMenu, $iFlags, $iX, $iY, $hWnd)
    Local $aRet = DllCall("User32.dll", "int", "TrackPopupMenu", _
                                                "hwnd", $hMenu, _
                                                "int", $iFlags, _
                                                "int", $iX, _
                                                "int", $iY, _
                                                "int", 0, _
                                                "hwnd", $hWnd, _
                                                "hwnd", 0)
    Return $aRet[0]
EndFunc   ;==>_TrackPopupMenu
Link to comment
Share on other sites

Just One Word

GREAT MAN!

Tnxvm

:)

But ... how can Handle more than ONE tree root ????

Thanks.

but ... How can i manage more than One same level root?

Delete work well, but how to add root context menu "Add" for each Tree root item?

(no dummy context menu, exist something for Contextmenu press like Case $NM_RCLICK )

This is the Example that need to add functionality to add/remove on $NM_RCLICK

Thanks mutch for any help

WaltZie

#include <GUIConstantsEx.au3>

#include <WindowsConstants.au3>

#include <GuiTreeView.au3>

Global $hItem = 0

$gui = GUICreate("Tree example", 400, 400)

$maintree = GUICtrlCreateTreeView(10, 10, 150, 200)

For $l = 1 to 2

$Parent = GUICtrlCreateTreeViewItem("Parent " & $l, $maintree)

$ParentMenu = GUICtrlCreateContextMenu($Parent)

$ParentMenu_AddItem = GUICtrlCreateMenuItem("&Add", $ParentMenu)

For $i = 1 To 3

$Item=GUICtrlCreateTreeViewItem("Child " & $i, $Parent)

$ItemMenu = GUICtrlCreateContextMenu($Item)

$ItemMenu_Remove = GUICtrlCreateMenuItem("&Remove", $ItemMenu)

Next

Next

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

Exit

EndSelect

WEnd

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)

Local $hWndTreeView, $tNMHDR, $hWndFrom, $iCode

$hWndTreeView = $maintree

If Not IsHWnd($hWndTreeView) Then $hWndTreeView = GUICtrlGetHandle($hWndTreeView)

$tNMHDR = DllStructCreate($tagNMHDR, $lParam)

$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))

$iCode = DllStructGetData($tNMHDR, "Code")

Switch $hWndFrom

Case $hWndTreeView

Switch $iCode

Case $NM_RCLICK

Local $tPOINT = DllStructCreate("int X;int Y")

DllStructSetData($tPOINT, "X", MouseGetPos(0))

DllStructSetData($tPOINT, "Y", MouseGetPos(1))

_ScreenToClient($hWndTreeView, DllStructGetPtr($tPOINT))

Local $iX = DllStructGetData($tPOINT, 1)

Local $iY = DllStructGetData($tPOINT, 2)

Local $tTVHITTESTINFO = _GUICtrlTreeView_HitTestEx($hWndTreeView, $iX, $iY)

Local $iFlags = DllStructGetData($tTVHITTESTINFO, "Flags")

;~

;~ HOW TO RETRUVE INFO ABOUT CONTEXT MENU CHOOSE & ITEM SELECTED?

;~ .......

EndSwitch

Return $GUI_RUNDEFMSG

EndFunc ;==>WM_NOTIFY

Func _ScreenToClient($hWnd, $pPoint)

Local $aRet = DllCall("user32.dll", "int", "ScreenToClient", _

"hwnd", $hWnd, _

"ptr", $pPoint)

Return $aRet[0]

EndFunc ;==>_ScreenToClient

Func _TrackPopupMenu($hMenu, $iFlags, $iX, $iY, $hWnd)

Local $aRet = DllCall("User32.dll", "int", "TrackPopupMenu", _

"hwnd", $hMenu, _

"int", $iFlags, _

"int", $iX, _

"int", $iY, _

"int", 0, _

"hwnd", $hWnd, _

"hwnd", 0)

Return $aRet[0]

EndFunc ;==>_TrackPopupMenu

Edited by waltzie
Link to comment
Share on other sites

Try this:

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

Global $hItem = 0, $hTreeView_Parent = 0

$gui = GUICreate("Tree example", 400, 400)

$maintree = GUICtrlCreateTreeView(10, 10, 150, 200)

$aboutitem = GUICtrlCreateTreeViewItem("Parent", $maintree)

For $i = 1 To 10
    GUICtrlCreateTreeViewItem("Child " & $i, $aboutitem)
Next

$hParent2 = GUICtrlCreateTreeViewItem("Parent2", $maintree)

For $i = 1 To 10
    GUICtrlCreateTreeViewItem("Child " & $i, $hParent2)
Next

$cDummyMenu = GUICtrlCreateDummy()

$aboutitemMenu = GUICtrlCreateContextMenu($cDummyMenu)
$hMenu = GUICtrlGetHandle($aboutitemMenu)

$aboutitemMenu_add = GUICtrlCreateMenuItem("Add Item", $aboutitemMenu)

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $cDummyMenu
            _TrackPopupMenu($hMenu, 0, MouseGetPos(0), MouseGetPos(1), $gui)
        Case $msg = $aboutitemMenu_add
            If GUICtrlRead($aboutitemMenu_add, 1) = "Add Item" Then
                _GUICtrlTreeView_InsertItem($maintree, "New Item", $hTreeView_Parent)
            Else
                _GUICtrlTreeView_Delete($maintree, $hItem)
            EndIf
    EndSelect
WEnd

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)
    Local $hWndTreeView, $tNMHDR, $hWndFrom, $iCode
    
    $hWndTreeView = $maintree
    If Not IsHWnd($hWndTreeView) Then $hWndTreeView = GUICtrlGetHandle($hWndTreeView)
    
    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    
    Switch $hWndFrom
        Case $hWndTreeView
            Switch $iCode
                Case $NM_RCLICK
                    Local $tPOINT = DllStructCreate("int X;int Y")
                    
                    DllStructSetData($tPOINT, "X", MouseGetPos(0))
                    DllStructSetData($tPOINT, "Y", MouseGetPos(1))
                    
                    _ScreenToClient($hWndTreeView, DllStructGetPtr($tPOINT))
                    Local $iX = DllStructGetData($tPOINT, 1)
                    Local $iY = DllStructGetData($tPOINT, 2)
                    
                    Local $tTVHITTESTINFO  = _GUICtrlTreeView_HitTestEx($hWndTreeView, $iX, $iY)
                    Local $iFlags = DllStructGetData($tTVHITTESTINFO, "Flags")
                    
                    If BitAND($iFlags, $TVHT_ONITEM) Or BitAND($iFlags, $TVHT_ONITEMLABEL) Then
                        $hItem = DllStructGetData($tTVHITTESTINFO, "Item")
                        _GUICtrlTreeView_SelectItem($hWndTreeView, $hItem, $TVGN_CARET)
                        
                        If _GUICtrlTreeView_GetParentHandle($hWndTreeView, $hItem) = 0 Then
                            $hTreeView_Parent = $hItem
                            GUICtrlSetData($aboutitemMenu_add, "Add Item")
                        Else
                            GUICtrlSetData($aboutitemMenu_add, "Delete Item")
                        EndIf
                        
                        GUICtrlSendToDummy($cDummyMenu)
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _ScreenToClient($hWnd, $pPoint)
    Local $aRet = DllCall("user32.dll", "int", "ScreenToClient", _
                                                "hwnd", $hWnd, _
                                                "ptr", $pPoint)
    Return $aRet[0]
EndFunc   ;==>_ScreenToClient

Func _TrackPopupMenu($hMenu, $iFlags, $iX, $iY, $hWnd)
    Local $aRet = DllCall("User32.dll", "int", "TrackPopupMenu", _
                                                "hwnd", $hMenu, _
                                                "int", $iFlags, _
                                                "int", $iX, _
                                                "int", $iY, _
                                                "int", 0, _
                                                "hwnd", $hWnd, _
                                                "hwnd", 0)
    Return $aRet[0]
EndFunc   ;==>_TrackPopupMenu

:)

Link to comment
Share on other sites

Yes Yes YESS!!!! :lmao:

Thanks lot bro ... you are Magnificent :)

To make it perfect ....

How can i use MORE THAN ONE DUMMY to have differents entries menu?

Thanks

Solved :think:

...

...

$cDummyMenu = GUICtrlCreateDummy()

$aboutitemMenu = GUICtrlCreateContextMenu($cDummyMenu)

$hMenu = GUICtrlGetHandle($aboutitemMenu)

$aboutitemMenu_add = GUICtrlCreateMenuItem("Add Item", $aboutitemMenu)

$cDummyMenuItems = GUICtrlCreateDummy()

$aboutitemMenuItems = GUICtrlCreateContextMenu($cDummyMenuItems)

$hMenu_Items = GUICtrlGetHandle($aboutitemMenuItems)

$aboutitemMenuItems_rem = GUICtrlCreateMenuItem("Remove", $aboutitemMenuItems)

...

...

Case $msg = $cDummyMenu

_TrackPopupMenu($hMenu, 0, MouseGetPos(0), MouseGetPos(1), $gui)

Case $msg = $cDummyMenuItems

_TrackPopupMenu($hMenu_Items, 0, MouseGetPos(0), MouseGetPos(1), $gui)

...

...

Case $msg = $aboutitemMenu_add

_GUICtrlTreeView_InsertItem($maintree, "New Item", $hTreeView_Parent,0,1,1)

Case $msg = $aboutitemMenuItems_rem

_GUICtrlTreeView_Delete($maintree, $hItem)

...

...

If _GUICtrlTreeView_GetParentHandle($hWndTreeView, $hItem) = 0 Then ;sono in un processo (root tree)

$hTreeView_Parent = $hItem

;GUICtrlSetData($aboutitemMenu_add, "Add Item")

GUICtrlSendToDummy($cDummyMenuItems)

Else ;sono in un ramo di processo (item tree)

;GUICtrlSetData($aboutitemMenu_add, "Remove")

GUICtrlSendToDummy($cDummyMenuItems)

EndIf

...

Edited by waltzie
Link to comment
Share on other sites

Hi,

to change Icon on Items & tree roots i find lots of troubles...

Here is your example modified using 2 dummy meny & icons.

Changing Icons using seems that change it ONLY WHEN NOT FOCUSED!

Can you help me to sove this issue ?

Thanks mutch

WaltZie

#include <GUIConstantsEx.au3>

#include <WindowsConstants.au3>

#include <GuiTreeView.au3>

Global $hItem = 0, $hTreeView_Parent = 0

$gui = GUICreate("Tree example", 400, 400)

$maintree = GUICtrlCreateTreeView(10, 10, 150, 200)

$hImage = _GUIImageList_Create(16, 16, 5, 3)

_GUIImageList_AddIcon($hImage, "shell32.dll", 165)

_GUIImageList_AddIcon($hImage, "shell32.dll", 137)

_GUIImageList_AddIcon($hImage, "shell32.dll", 131)

_GUICtrlTreeView_SetNormalImageList($maintree, $hImage)

$aboutitem = GUICtrlCreateTreeViewItem("Parent", $maintree)

For $i = 1 To 2

GUICtrlCreateTreeViewItem("Child " & $i, $aboutitem)

Next

$hParent2 = GUICtrlCreateTreeViewItem("Parent2", $maintree)

For $i = 1 To 3

GUICtrlCreateTreeViewItem("Child " & $i, $hParent2)

Next

$cDummyMenu = GUICtrlCreateDummy()

$aboutitemMenu = GUICtrlCreateContextMenu($cDummyMenu)

$hMenu = GUICtrlGetHandle($aboutitemMenu)

$aboutitemMenu_add = GUICtrlCreateMenuItem("Add Item", $aboutitemMenu)

;---------------------------------------------

$cDummyMenu2 = GUICtrlCreateDummy()

$aboutitemMenu2 = GUICtrlCreateContextMenu($cDummyMenu2)

$hMenu2 = GUICtrlGetHandle($aboutitemMenu2)

$aboutitemMenu_remove = GUICtrlCreateMenuItem("Remove Item", $aboutitemMenu2)

$aboutitemMenu_play = GUICtrlCreateMenuItem("Play Item", $aboutitemMenu2)

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

Exit

Case $msg = $cDummyMenu

_TrackPopupMenu($hMenu, 0, MouseGetPos(0), MouseGetPos(1), $gui)

case $msg = $cDummyMenu2

_TrackPopupMenu($hMenu2, 0, MouseGetPos(0), MouseGetPos(1), $gui)

Case $msg = $aboutitemMenu_add

If GUICtrlRead($aboutitemMenu_add, 1) = "Add Item" Then

_GUICtrlTreeView_InsertItem($maintree, "New Item", $hTreeView_Parent,0,2,2)

Else

;more menues on dummy

EndIf

case $msg = $aboutitemMenu_remove

If GUICtrlRead($aboutitemMenu_remove, 1) = "Remove Item" Then

_GUICtrlTreeView_Delete($maintree, $hItem)

Else

;more menues on dummy2

EndIf

case $msg = $aboutitemMenu_play

If GUICtrlRead($aboutitemMenu_play, 1) = "Play Item" Then

GUICtrlSetData($aboutitemMenu_play, "Stop Item")

_GUICtrlTreeView_SetImageIndex($maintree, $hItem, 1)

msgbox(4096,"","Play")

Else

GUICtrlSetData($aboutitemMenu_play, "Play Item")

_GUICtrlTreeView_SetImageIndex($maintree, $hItem, 2)

msgbox(4096,"","Stop")

endif

EndSelect

WEnd

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)

Local $hWndTreeView, $tNMHDR, $hWndFrom, $iCode

$hWndTreeView = $maintree

If Not IsHWnd($hWndTreeView) Then $hWndTreeView = GUICtrlGetHandle($hWndTreeView)

$tNMHDR = DllStructCreate($tagNMHDR, $lParam)

$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))

$iCode = DllStructGetData($tNMHDR, "Code")

Switch $hWndFrom

Case $hWndTreeView

Switch $iCode

Case $NM_RCLICK

Local $tPOINT = DllStructCreate("int X;int Y")

DllStructSetData($tPOINT, "X", MouseGetPos(0))

DllStructSetData($tPOINT, "Y", MouseGetPos(1))

_ScreenToClient($hWndTreeView, DllStructGetPtr($tPOINT))

Local $iX = DllStructGetData($tPOINT, 1)

Local $iY = DllStructGetData($tPOINT, 2)

Local $tTVHITTESTINFO = _GUICtrlTreeView_HitTestEx($hWndTreeView, $iX, $iY)

Local $iFlags = DllStructGetData($tTVHITTESTINFO, "Flags")

If BitAND($iFlags, $TVHT_ONITEM) Or BitAND($iFlags, $TVHT_ONITEMLABEL) Then

$hItem = DllStructGetData($tTVHITTESTINFO, "Item")

_GUICtrlTreeView_SelectItem($hWndTreeView, $hItem, $TVGN_CARET)

If _GUICtrlTreeView_GetParentHandle($hWndTreeView, $hItem) = 0 Then

$hTreeView_Parent = $hItem

GUICtrlSendToDummy($cDummyMenu)

;give control to root entity menu

Else

;give control to item menu

GUICtrlSendToDummy($cDummyMenu2)

EndIf

EndIf

EndSwitch

EndSwitch

Return $GUI_RUNDEFMSG

EndFunc ;==>WM_NOTIFY

Func _ScreenToClient($hWnd, $pPoint)

Local $aRet = DllCall("user32.dll", "int", "ScreenToClient", _

"hwnd", $hWnd, _

"ptr", $pPoint)

Return $aRet[0]

EndFunc ;==>_ScreenToClient

Func _TrackPopupMenu($hMenu, $iFlags, $iX, $iY, $hWnd)

Local $aRet = DllCall("User32.dll", "int", "TrackPopupMenu", _

"hwnd", $hMenu, _

"int", $iFlags, _

"int", $iX, _

"int", $iY, _

"int", 0, _

"hwnd", $hWnd, _

"hwnd", 0)

Return $aRet[0]

EndFunc ;==>_TrackPopupMenu

Link to comment
Share on other sites

@waltzie

Hello! :)

Use the _GUICtrlTreeView_SetSelectedImageIndex function.

Example:

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

Global $hItem = 0, $hTreeView_Parent = 0

$gui = GUICreate("Tree example", 400, 400)

$maintree = GUICtrlCreateTreeView(10, 10, 150, 200)

$hImage = _GUIImageList_Create(16, 16, 5, 3)
_GUIImageList_AddIcon($hImage, "shell32.dll", 165)
_GUIImageList_AddIcon($hImage, "shell32.dll", 137)
_GUIImageList_AddIcon($hImage, "shell32.dll", 131)

_GUICtrlTreeView_SetNormalImageList($maintree, $hImage)

$aboutitem = GUICtrlCreateTreeViewItem("Parent", $maintree)
For $i = 1 To 2
    GUICtrlCreateTreeViewItem("Child " & $i, $aboutitem)
Next

$hParent2 = GUICtrlCreateTreeViewItem("Parent2", $maintree)
For $i = 1 To 3
    GUICtrlCreateTreeViewItem("Child " & $i, $hParent2)
Next

$cDummyMenu = GUICtrlCreateDummy()
$aboutitemMenu = GUICtrlCreateContextMenu($cDummyMenu)
$hMenu = GUICtrlGetHandle($aboutitemMenu)
$aboutitemMenu_add = GUICtrlCreateMenuItem("Add Item", $aboutitemMenu)
;---------------------------------------------
$cDummyMenu2 = GUICtrlCreateDummy()
$aboutitemMenu2 = GUICtrlCreateContextMenu($cDummyMenu2)
$hMenu2 = GUICtrlGetHandle($aboutitemMenu2)
$aboutitemMenu_remove = GUICtrlCreateMenuItem("Remove Item", $aboutitemMenu2)
$aboutitemMenu_play = GUICtrlCreateMenuItem("Play Item", $aboutitemMenu2)

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $cDummyMenu
            _TrackPopupMenu($hMenu, 0, MouseGetPos(0), MouseGetPos(1), $gui)
        Case $msg = $cDummyMenu2
            _TrackPopupMenu($hMenu2, 0, MouseGetPos(0), MouseGetPos(1), $gui)
        Case $msg = $aboutitemMenu_add
            If GUICtrlRead($aboutitemMenu_add, 1) = "Add Item" Then
                _GUICtrlTreeView_InsertItem($maintree, "New Item", $hTreeView_Parent, 0, 2, 2)
            Else
                ;more menues on dummy
            EndIf
        Case $msg = $aboutitemMenu_remove
            If GUICtrlRead($aboutitemMenu_remove, 1) = "Remove Item" Then
                _GUICtrlTreeView_Delete($maintree, $hItem)
            Else
                ;more menues on dummy2
            EndIf
        Case $msg = $aboutitemMenu_play
            If GUICtrlRead($aboutitemMenu_play, 1) = "Play Item" Then
                GUICtrlSetData($aboutitemMenu_play, "Stop Item")
                _GUICtrlTreeView_BeginUpdate($maintree)
                _GUICtrlTreeView_SetImageIndex($maintree, $hItem, 1)
                _GUICtrlTreeView_SetSelectedImageIndex($maintree, $hItem, 1)
                _GUICtrlTreeView_EndUpdate($maintree)
            Else
                GUICtrlSetData($aboutitemMenu_play, "Play Item")
                _GUICtrlTreeView_BeginUpdate($maintree)
                _GUICtrlTreeView_SetImageIndex($maintree, $hItem, 2)
                _GUICtrlTreeView_SetSelectedImageIndex($maintree, $hItem, 2)
                _GUICtrlTreeView_EndUpdate($maintree)
            EndIf
    EndSelect
WEnd

Func WM_NOTIFY($hWnd, $msg, $wParam, $lParam)
    Local $hWndTreeView, $tNMHDR, $hWndFrom, $iCode

    $hWndTreeView = $maintree
    If Not IsHWnd($hWndTreeView) Then $hWndTreeView = GUICtrlGetHandle($hWndTreeView)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndTreeView
            Switch $iCode
                Case $NM_RCLICK
                    Local $tPOINT = DllStructCreate("int X;int Y")

                    DllStructSetData($tPOINT, "X", MouseGetPos(0))
                    DllStructSetData($tPOINT, "Y", MouseGetPos(1))

                    _ScreenToClient($hWndTreeView, DllStructGetPtr($tPOINT))
                    Local $iX = DllStructGetData($tPOINT, 1)
                    Local $iY = DllStructGetData($tPOINT, 2)

                    Local $tTVHITTESTINFO = _GUICtrlTreeView_HitTestEx($hWndTreeView, $iX, $iY)
                    Local $iFlags = DllStructGetData($tTVHITTESTINFO, "Flags")

                    If BitAND($iFlags, $TVHT_ONITEM) Or BitAND($iFlags, $TVHT_ONITEMLABEL) Then
                        $hItem = DllStructGetData($tTVHITTESTINFO, "Item")
                        _GUICtrlTreeView_SelectItem($hWndTreeView, $hItem, $TVGN_CARET)

                        If _GUICtrlTreeView_GetParentHandle($hWndTreeView, $hItem) = 0 Then
                            $hTreeView_Parent = $hItem
                            GUICtrlSendToDummy($cDummyMenu)
                            ;give control to root entity menu
                        Else
                            ;give control to item menu
                            GUICtrlSendToDummy($cDummyMenu2)
                        EndIf


                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func _ScreenToClient($hWnd, $pPoint)
    Local $aRet = DllCall("user32.dll", "int", "ScreenToClient", _
            "hwnd", $hWnd, _
            "ptr", $pPoint)
    Return $aRet[0]
EndFunc   ;==>_ScreenToClient

Func _TrackPopupMenu($hMenu, $iFlags, $iX, $iY, $hWnd)
    Local $aRet = DllCall("User32.dll", "int", "TrackPopupMenu", _
            "hwnd", $hMenu, _
            "int", $iFlags, _
            "int", $iX, _
            "int", $iY, _
            "int", 0, _
            "hwnd", $hWnd, _
            "hwnd", 0)
    Return $aRet[0]
EndFunc   ;==>_TrackPopupMenu

:lmao:

Link to comment
Share on other sites

@waltzie

Hello! :)

.....

:lmao:

Thanks, but there is still a little bug...

If i set a Item to play, next time that i rightclick on each other (ever not played) items i see in dummy meny STOP.

SOLVED!

In WM_NOTIFY on $NM_RCLICK

add:

If _GUICtrlTreeView_GetParentHandle($hWndTreeView, $hItem) = 0 Then

$hTreeView_Parent = $hItem

GUICtrlSendToDummy($cDummyMenu)

;give control to root entity menu

Else

if _GUICtrlTreeView_GetSelectedImageIndex($hWndTreeView, $hItem) = 2 Then

GUICtrlSetData($aboutitemMenuItems_Play, "Play Item")

Else

GUICtrlSetData($aboutitemMenuItems_Play, "Stop Item")

EndIf

;give control to item menu

GUICtrlSendToDummy($cDummy2)

EndIf

Edited by waltzie
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...