1

I have made a form like this enter image description here

there is only a label in the form, when I drag a file and drop the file in the form, the label will show the path of the files, just as shown above.

Now I am going to do something like this, I will open disk D: in explorer (MyComputer) and drag the label into the window and drop, and then the file AOP.mm will be copy to disk D:.

but when I drop the file, how can I get the target path, just like D:/dragToHere/?

manlio
  • 18,345
  • 14
  • 76
  • 126
Alfred Chen
  • 179
  • 3
  • 13
  • so you have already added code which will update the label when the file is dropped? If so then just modify that code to take the other actions you want. How are you deciding on what the target path is? – M.M Feb 11 '15 at 01:20
  • @MattMcNabb Thanks for commenting.Yes , I`ve already added code which will update the label when the file is dropped.That mean I can drag a file from C:\ and drop the file in the form,and then update the label.But now,I want a action like this ,I drag the label and drop to D:\ ,the file show in the label will be copy to D:\. – Alfred Chen Feb 11 '15 at 01:34
  • OK so you are asking for a new drag-drop operation: drag from the label onto a Windows Explorer or whatever that's already open at D:\ ? – M.M Feb 11 '15 at 01:36
  • @MattMcNabb Yes,that`s right.sorry for my poor english:( – Alfred Chen Feb 11 '15 at 01:56
  • Probably related - https://stackoverflow.com/questions/25625173/drag-and-drop-images-in-c-builder-6 – sashoalm Feb 11 '15 at 08:43
  • @sashoalm Thanks,but maybe not :) – Alfred Chen Feb 14 '15 at 08:17

1 Answers1

2

In order to drag&drop something onto Windows Explorer, you must implement the IDropSource and IDataObject interfaces, and then call the DoDragDrop() function when you are ready to start a new drag operation. Or use a wrapper component/library, such as the TDropFileSource component in Ander Melander's Drag&Drop Suite (an updated version is on Github).

For what you are attempting, all you need to do is put the source filename from your TLabel into your IDataObject object formatted using the CF_HDROP clipboard format. Or, if using Anders' components, put the filename in the TDropFileSource::Files property. Windows Explorer itself can then copy the file when a drop is performed. You do not need to manage the copy yourself. In fact, it is actually very difficult (almost impossible) to determine the folder that is being dropped onto, so you are better off not even trying to do that. The target is supposed to handle the drop, so let Windows Explorer copy the file as it knows both the source file and the target folder.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks for answering.Can you offer me your contact,I write a little demo code,but it didn`t works well,Can you help me with the code? – Alfred Chen Feb 12 '15 at 09:31
  • 1
    @AlfredChen You should ask a new question containing the demo code and a complete description of the problem (and probably a link to this question as reference). – manlio Feb 12 '15 at 16:04
  • @manlio I have open a new ask,I don`t kown why I can`t post the link here,just see it at my profile,thanks. – Alfred Chen Feb 14 '15 at 08:16