Sign in to follow this  
Followers 0
JakeJohnson74

Calling on Drag and Drop pros!

5 posts in this topic

#1 ·  Posted (edited)

Ok guys, 

I'll get into code if people really want it, my code is very long. I would need to just write an example explaining my question. 

In case my words are enough here goes :P

I have a form with many list views (7) and 2 treeviews. When I drag and drop i need to know which list view I dragged from, and which treeview I dropped on. My message loop code is failing miserably and I have tunnel vision! How can I do this?

Im trying to capture the "from" control and the "to" control but the Mousemove event is firing the whole time i'm dragging, so the hWnd changes to the tree view as soon as I leave the listview. 

So when the drop event fires, the to and from array are both set to the treeview's hWnd. Help! haha

While 1
    $idMsg = GUIGetMsg()
    Switch $idMsg
        Case $GUI_EVENT_MOUSEMOVE
            if _IsPressed("01") Then
                $aHwndFrom = DllCall("user32.dll", "hwnd", "WindowFromPoint", "uint", MouseGetPos(0), "uint", MouseGetPos(1)); <-- not correct for the drop event usage because it flips to the treeview
                dragEvent($aHwndFrom[0]) ;<--- fires ok for the drag event
            endif
        Case $GUI_EVENT_DROPPED
            $aHwndTo = DllCall("user32.dll", "hwnd", "WindowFromPoint", "uint", MouseGetPos(0), "uint", MouseGetPos(1))  
            myDrop($aHwndTo[0],$aHwndFrom[0]) ;<--- passing what is supposed to be a listview Hwnd in the "from" array, and the treeview hWnd in the "to" array but they are both set to the treeview because that is where I end up on the drop event
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Trying to figure out how to target the to and from controls is making my brain turn to mush.... probably not a good thing! 

Edited by JakeJohnson74

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Have a look at this topic: >link

My preferred route has always been registering the WM_DROPFILES msg. :)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

I knew i should have made a quicky example :) Sorry about that, I'm not dropping files though. I'm only dragging and dropping data from one of 7 listviews to one of two treeviews. So I am looking for a way to trap each control so I can refer to them when the drop event fires.

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

Global $hItemHover
Global $Debugmode = True

$Form1 = GUICreate("Test", 500, 400)
$TreeView1 = GUICtrlCreateTreeView(10, 10, 200, 100, BitOR($TVS_HASBUTTONS,$TVS_HASLINES,$TVS_LINESATROOT,$TVS_SHOWSELALWAYS))
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
$TreeView1_0 = GUICtrlCreateTreeViewItem("Item1", $TreeView1)
$TreeView1_1 = GUICtrlCreateTreeViewItem("Item2", $TreeView1)
$TreeView1_2 = GUICtrlCreateTreeViewItem("Item3", $TreeView1)

$TreeView2 = GUICtrlCreateTreeView(10, 210, 200, 100, BitOR($TVS_HASBUTTONS,$TVS_HASLINES,$TVS_LINESATROOT,$TVS_SHOWSELALWAYS))
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
$TreeView2_0 = GUICtrlCreateTreeViewItem("Item4", $TreeView2)
$TreeView2_1 = GUICtrlCreateTreeViewItem("Item5", $TreeView2)
$TreeView2_2 = GUICtrlCreateTreeViewItem("Item6", $TreeView2)

$ListView1 = GUICtrlCreateListView("LV1", 250, 10, 200, 100)
$ListView1_0 = GUICtrlCreateListViewItem("Item7", $ListView1)
$ListView1_1 = GUICtrlCreateListViewItem("Item8", $ListView1)
$ListView1_2 = GUICtrlCreateListViewItem("Item9", $ListView1)

$ListView2 = GUICtrlCreateListView("LV2", 250, 210, 200, 100)
$ListView2_0 = GUICtrlCreateListViewItem("Item10", $ListView2)
$ListView2_1 = GUICtrlCreateListViewItem("Item11", $ListView2)
$ListView2_2 = GUICtrlCreateListViewItem("Item12", $ListView2)

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_MOUSEMOVE
            if _IsPressed("01") Then
                $aHwndFrom = DllCall("user32.dll", "hwnd", "WindowFromPoint", "uint", MouseGetPos(0), "uint", MouseGetPos(1))
;~              _DebugPrint("From: "& $aHwndFrom[0],$Debugmode)
                _Drag($aHwndFrom[0])
            endif
        Case $GUI_EVENT_DROPPED
            $aHwndTo = DllCall("user32.dll", "hwnd", "WindowFromPoint", "uint", MouseGetPos(0), "uint", MouseGetPos(1))
            _DebugPrint("To: " & $aHwndTo[0] &" From: "& $aHwndFrom[0],$Debugmode)
            _Drop($aHwndTo[0],$aHwndFrom[0])
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

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

Func _Drag($hWnd)
    $hItemHover = TreeItemFromPoint($hWnd)
    If $hItemHover = 0 Then
        ;Todo
    Else
        $aRect = _GUICtrlTreeView_DisplayRect($hWnd, $hItemHover)
        $iTreeY = _WinAPI_GetMousePosY(True, $hWnd)
        Switch $iTreeY
            Case $aRect[1] To $aRect[1]+Int(($aRect[3]-$aRect[1])/4)
                _SendMessage($hWnd, $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($hWnd, $TVM_SELECTITEM, $TVGN_DROPHILITE, $hItemHover) ;add DropTarget
                $fWhere = 0
            Case 1+$aRect[1]+Int(($aRect[3]-$aRect[1])*2/3) To $aRect[3]
                _SendMessage($hWnd, $TVM_SELECTITEM, $TVGN_DROPHILITE, 0) ;remove DropTarget
                $fWhere = 1
        EndSwitch
    EndIf
EndFunc ;==>_Drag

Func _Drop($hWndTo, $hWndFrom)
    If $hItemHover <> 0 Then
        ; Get selected item in the listview
        $tmpItemArr = _GUICtrlListView_GetSelectedIndices($hWndFrom, TRUE)
        $tmpItem = _GUICtrlListView_GetItemText($hWndFrom, $tmpItemArr[1])

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

        $myMSG = "Dragged: " & $tmpItem & @CRLF _
        & "Dropped: " & _GUICtrlTreeView_GetText($hWndTo, $hItemHover)
        _DebugPrint($myMsg,$Debugmode)
        ; Reset drop target
        _SendMessage(GUICtrlGetHandle($hWndTo), $TVM_SELECTITEM, $TVGN_DROPHILITE, 0) ;remove DropTarget
    Else
        MsgBox(0, "", "No drop target")
    EndIf
EndFunc ;==>_Drop

Func _DebugPrint($sText, $isEnabled, $sLine = @ScriptLineNumber)
    if $isenabled then
        ConsoleWrite(">--------------------------------" & @CRLF & _
        "-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $sText & @CRLF & _
        ">--------------------------------" & @CRLF)
    EndIf
EndFunc ;==>_DebugPrint

Share this post


Link to post
Share on other sites

What about storing a ControlGetFocus call at the beginning and end of the drag & drop event?


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Read this for the function GUICtrlSetState for the $GUI_DROPACCEPTED state:

@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.
Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

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  
Followers 0