Wiki Home

Drag And Drop

Namespace: DotNet
Part I - Drag from the Windows Explorer and Drop into a VFP textbox.

There are two types of Drag And Drop in VFP: VFP type is for within a VFP app, OLE is for between apps (in this case, between Explorer and VFP, so you want the OLE type.) You can also use OLE within a VFP app, so you might as well use it for everything.

There are two distinct parts of drag and drop. They are: Don't let the fact that VFP supports both confuse things. It is as distinct as the difference between shooting a gun and being shot. The terms to be aware of are Drag Source and Drop Target. For getting a file name, you want nothing to do with VFP's drag source capabilities. You want a VFP text box to be a Drop Target.

Add these together and you get: you want to create an OLE Drop Target.

Now you need to set up the drop target to deal with what is being dropped on it. In the case of dragging things from explorer, you need to know that you are getting much more than a file name. You get an array of files (you could select multiple items and drag that, so what Explorer drops is an array of items, even if it is only one item). Also, you get the name, the size, the date, and a bunch of other properties. Therefore, you get a two dimensional array.

What I did is cheat: if someone drops multiple items, I only look at the first. If they only drop one, I only look at the first. Really easy: I look at aFileName(1). It will have my filename. I don't care about the rest.

If you are doing it for yourself (like I was), you can be very relaxed about what gets dropped. If this is going to be delivered and expected not to go BOOM, you will need to make sure that only Explorer type objects are accepted. You do this in the mouse over event: if it is a good object, enable the drop target, otherwise leave it disabled.

For the next section, we need to study the timing of the events. I will use a simple example of drag and droping a file to move it into another folder (no VFP, just explorer) 1) nothing happens when the drag begins. The file is still in the original directory. 2) if the drop is not successful, still nothing happens. 3) If the drop is successful, that is, the file is copied to another folder, then the original file is deleted. The delete is the responsibility of the drag.

This is implemented by the drop target returning an effect or disposition of the drop operation to the drag source. The OLECompleteDrag method of the drag source application fires when data is dropped on the drop target or the OLE drag-and-drop operation is canceled. It get passed a parameter describing what happened. The OLECompleteDrag method can then react accordingly, now that it knows what happened. For our task, we leave the Drag Source alone.

Here are the three things you need to do to a textbox:

OLEDropMode = 1

* OLEDragOver
LPARAMETERS oDataObject, nEffect, nButton, nShift, nXCoord, nYCoord, nState
This.OLEDropHasData = 1

* OLEDragDrop
LPARAMETERS oDataObject, nEffect, nButton, nShift, nXCoord, nYCoord
local laFls(1)
oDataObject.getdata(15, @laFls)
This.value = laFls(1)

Part II: drag from VFP - drop in Windows Explorer.

So far all I have figured out is: I need an object in VFP to be an OLE Drag Source.
I guess it will supply a filename. Or an array of filename(s).

What I am hoping to accomplish is: drop images in the recycle bin. -- ?CFK

Isn't that what SetData() is for? -- Mike Helland
See also:
Contributors Carl Karsten
Category Code Samples
( Topic last updated: 2013.05.08 06:21:49 PM )