Jump to content

Drag and Drop from a Treeview to another treeview


 Share

Recommended Posts

Is is possible to drag items from one tree to another?

I found this function that allows you to drag items from a listview to a treeview:

but I am having trouble adapting it to two treeviews. One reason might be because of this statement in the Help under GUICtrlSetState:

If $GUI_DROPACCEPTED is set to a visible control a drag&drop can be taken in account. The edit/input control will be set with the filename.

For other controls on reception of $GUI_EVENT_DROPPED, @GUI_DRAGID will return the controlID from where the drag start (-1 if from a file, @GUI_DRAGFILE contain the filename being dropped) and @GUI_DROPID returns the controlID of the dropped control.

Only dragging a ListviewItem will start the drag & drop process. The @GUI_DRAGID will be the ListView controlID.

Here is my attempt at adapting the above code:

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <Misc.au3>

Global $hItemHover
Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Drop Test", 491, 387, 193, 125)

$TreeView1 = GUICtrlCreateTreeView(8, 8, 233, 369, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_SHOWSELALWAYS, $WS_GROUP, $WS_TABSTOP))
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
$TreeView1_0 = GUICtrlCreateTreeViewItem("Item1", $TreeView1)
$TreeView1_1 = GUICtrlCreateTreeViewItem("Item2", $TreeView1)
$TreeView1_2 = GUICtrlCreateTreeViewItem("Item3", $TreeView1)

$TreeView2 = GUICtrlCreateTreeView(250, 8, 233, 369, BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT,$TVIS_DROPHILITED, $TVS_SHOWSELALWAYS, $TVS_TRACKSELECT, $WS_GROUP, $WS_TABSTOP))
;GUICtrlSendMsg(-1, 0x101E, 0, 50)
$TreeView2_0 = GUICtrlCreateTreeViewItem("Item4", $TreeView2)
$TreeView2_1 = GUICtrlCreateTreeViewItem("Item5", $TreeView2)
$TreeView2_2 = GUICtrlCreateTreeViewItem("Item6", $TreeView2)
GUISetState(@SW_SHOW)

GUISetOnEvent($GUI_EVENT_MOUSEMOVE, "dragEvent")
GUISetOnEvent($GUI_EVENT_DROPPED, "myDrop")
GUISetOnEvent($GUI_EVENT_CLOSE, "Close")

While 1
    sleep (50)
WEnd

Func Close()
    Exit
EndFunc

Func TreeItemFromPoint($hWnd)
    Local $tMPos = _WinAPI_GetMousePos(True, $hWnd)
    Return _GUICtrlTreeView_HitTestItem($hWnd, DllStructGetData($tMPos, 1), DllStructGetData($tMPos, 2))
EndFunc   ;==>TreeItemFromPoint

Func dragEvent()
    Local $hTree = GUICtrlGetHandle($TreeView1)
    If _IsPressed("01") Then
        $hItemHover = TreeItemFromPoint($hTree)
        If $hItemHover = 0 Then
            ;No Hover at the moment
        Else
            $aRect = _GUICtrlTreeView_DisplayRect($hTree, $hItemHover)
            $iTreeY = _WinAPI_GetMousePosY(True, $hTree)
            Switch $iTreeY
                Case $aRect[1] To $aRect[1] + Int(($aRect[3] - $aRect[1]) / 4)
                    _SendMessage($hTree, $TVM_SELECTITEM, $TVGN_DROPHILITE, 0) ;remove DropTarget
                    $fWhere = -1
                Case 1 + $aRect[1] + Int(($aRect[3] - $aRect[1]) / 3) To $aRect[1] + Int(($aRect[3] - $aRect[1]) * 2 / 3)
                    _SendMessage($hTree, $TVM_SELECTITEM, $TVGN_DROPHILITE, $hItemHover) ;add DropTarget
                    $fWhere = 0
                Case 1 + $aRect[1] + Int(($aRect[3] - $aRect[1]) * 2 / 3) To $aRect[3]
                    _SendMessage($hTree, $TVM_SELECTITEM, $TVGN_DROPHILITE, 0) ;remove DropTarget
                    $fWhere = 1
            EndSwitch
        EndIf
    Else
        _WinAPI_InvalidateRect($hTree)
    EndIf
EndFunc   ;==>dragEvent

Func myDrop()
    If $hItemHover <> 0 Then
        ; Get selected item in the listview

;$tmpItemArr = _GUICtrlTreeView_GetSelection($TreeView2)
        $tmpItem = _GUICtrlTreeView_GetText($TreeView2, _GUICtrlTreeView_GetSelection($TreeView2))

        ; Get the selected item in the treeview
        $hSelected_Item = _GUICtrlTreeView_GetSelection($TreeView1)

        $myMSG = "Dragged: " & $tmpItem & @CRLF _
                 & "Dropped: " & _GUICtrlTreeView_GetText($TreeView1, $hItemHover)
        MsgBox(0, "", $myMSG)

        ; Reset drop target
        _SendMessage(GUICtrlGetHandle($TreeView1), $TVM_SELECTITEM, $TVGN_DROPHILITE, 0) ;remove DropTarget
    Else
        MsgBox(0, "", "Oopz no drop target  ")
    EndIf
EndFunc   ;==>myDrop

Any help would be greatly appreciated.

Thanks,

Chris

Link to comment
Share on other sites

You are going to need to handle notification messages like TVN_BEGINDRAG instead of using AutoIt events.

Then you have to monitor for mouse button release and get the coordinates from that to find out where it dropped using _GuiCtrlTreeView_HitTestItem().

Not a trivial exercise.

:huh2:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...