Search code examples
c#winformseventsdrag-and-drop

Perform dragdrop implementation after DoDragDrop method is called


I've been struggling with this for quite a while

My application contains a list view, populated with file-names, which are located on a server.

I'm trying to implement drag and drop functionality, so the user can drag files from my application into his/her's local computer.

In order to do this, first i'm downloading the files into a temporary location, and then calling my application's DoDragDrop() method.

The problem is that I want to perform the download process only after the DoDragDrop method is called.

I've tried every event related to drag drop methods (GiveFeedback, ItemDrag, etc...) but nothing works

so basically what I need is an event, raised after the DoDragDrop is done

any ideas??


Solution

  • Not sure how to do this in .NET, but in regular Win32 programming, the object that implements the IDataObject interface can optionally implement the IAsyncOperation interface as well. The IDropTarget can then use that interface to perform the drag-n-drop in a background thread so that the source and target are not blocked during the actual transfer. The only gotcha is that the target, not the source, decides whether to take advantage of this or not.

    The alternative is to use an "optimized move" transfer, where the IDataObject provides the filenames and the IDropTarget moves the files directly.

    MSDN has details on that: Handling Shell Data Transfer Scenarios.

    Of course, this still means you have to download the files before beginning the drag-n-drop. There really is no way to perform a drag-n-drop to determine the target and then perform the download afterwards. What you could do, though, is have the IDataObject hold CFSTR_FILEDESCRIPTOR and CFSTR_FILECONTENTS entries (described here: Shell Clipboard Formats), where the CFSTR_FILEDESCRIPTOR is filled in from information you used to populate your ListView, and the CFSTR_FILECONTENTS uses IStream interfaces whose implementations perform the download during the actual drop operation rather than prior to it. At least that way, you are only downloading the files that the target actually wants and can skip the rest.

    Couple that with IAsyncOperation, and that may give you the final effect you are looking for.