Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

OLEDrag, OLEStartDrag, OLECompleteDrag

These methods all belong to the drag source of an OLE Drag and Drop. OLEDrag starts a drag and drop, while OLEStartDrag and OLECompleteDrag fire at the indicated times to let you take action.

Usage

PROCEDURE OLEDrag
LPARAMETERS lDetectDrag

PROCEDURE OLEStartDrag
LPARAMETERS oDataObject, nEffect

PROCEDURE OLECompleteDrag
LPARAMETERS nEffect

Parameter

Value

Meaning

lDetectDrag

.F.

Begin dragging as soon as OLEDrag is called. This choice makes it impossible to highlight some text in a control and drag only the highlighted text, but might be appropriate for non-textual controls.

.T.

The drag doesn't begin until either the mouse moves some distance or some time has passed after the OLEDrag call. We went hunting to figure out how to change the distance or the time, but couldn't find anything. (We expected they'd be somewhere in the Control Panel, but we couldn't turn them up.) A look in the Registry didn't turn up anything promising either, so perhaps these settings really are fixed.

oDataObject

Object

An object reference to the data being dragged and to information about the data.

nEffect

(Applicable values are added together)

0

For OLEStartDrag, the drag source doesn't allow any kind of dragging.

For OLECompleteDrag, no drop was performed.

1

For OLEStartDrag, the drag source allows its data to be copied.

For OLECompleteDrag, data was copied to the drop target.

 

2

For OLEStartDrag, the drag source allows its data to be moved.

For OLECompleteDrag, data was moved to the drop target.

4

For OLEStartDrag, the drag source allows its data to be linked.

For OLECompleteDrag, data was linked to the drop target.

OLE Drag and Drop can be started either automatically by setting the drag source’s OLEDragMode property to 1-Automatic, or manually by calling the drag source’s OLEDrag method. (We guess, technically, it’s not the drag source until you do so, of course.) No matter which way you do it, the OLEStartDrag method fires, giving you a chance to take any action you’d like. OLECompleteDrag fires when the drag ends, whether or not it results in a drop.

Ever tried to highlight some text and had Windows start dragging instead? Then the lDetectDrag (which we’d rather call lWaitForDrag) parameter of OLEDrag is for you. When it’s .F., the drag begins as soon as OLEDrag is called. When lDetectDrag is .T., VFP gets smart and waits to see whether you keep the mouse down a bit or start moving it—if so, dragging starts. Best of all, .T. is the default value, so that’s what you get when you set OLEDragMode to 1-Automatic, too.

As far as we can tell, calling OLEDrag makes sense only from the MouseDown event. Using it in MouseMove (which is the solution to the dragging-too-soon problem for VFP’s native drag and drop) gives you weird cursor and highlight behavior, but doesn’t start dragging. A call to OLEDrag from any other method seems to be totally ignored.

Calling OLEDrag doesn't work for some ActiveX controls. What makes this worse is that some of them don't have an OLEDragMode property or the equivalent, which means there's no way to drag data out of them.

Dragging data from a spinner can be tricky. It's really hard to highlight the data you mean to highlight, so it can appear that you're not getting the results you wanted. We think this is related to a bug involving the Alignment property for spinners. You can read about that one under Alignment.

Like a number of the OLE Drag and Drop events, OLEStartDrag lets you change the nEffect parameter to change what follows. Setting nEffect to 0 prevents the drag. (Interestingly, issuing a NODEFAULT in OLEStartDrag doesn’t prevent the drag.) Similarly, setting nEffect to 2 allows the data to be moved rather than copied. While the whole arrangement of changing parameters that appear to be passed by value in order to change the operation strikes us as strange, the results are pretty cool. (It’s a COM thing.)

In addition to indicating the operations available, you can do something far more complex in OLEStartDrag. The data that’s dragged around and eventually dropped can come in various formats. Simple formats include text in various representations (ASCII, Unicode, etc.) and a list of files (for example, when you drag from Windows Explorer). Some data is available in multiple formats, and you can request the appropriate format for the drop target in OLEDragDrop. (See DataObject and GetFormat for more on the built-in formats.)

However, you can also define your own formats, and OLEStartDrag is the place to do it. You call the data object’s SetFormat method, giving it the name of your custom format. Then, call the data object’s SetData method to tell it what to return when that format is requested. The examples include setting a custom format.

As for OLECompleteDrag, we haven’t found a lot of use for it yet. Basically, it lets you know what really happened so you can act accordingly. We’re not sure exactly what “act accordingly” means in this case, though.

Example

* Turn on dragging with instant gratification. You might do this
* for an object where highlighting is irrelevant.
This.OLEDrag(.f.)

* Create a custom data format for OLE drag and drop.
* This format is for dragging an Image object. When dropped,
* it supplies the name of the picture it contains.
* This code goes in the Image control's OLEStartDrag event:
oDataObject.SetFormat("Picture")
oDataObject.SetData(This.Picture,"Picture")

* To retrieve the picture's name, put code like this in the
* OLEDragDrop event of the target. In this case it's a form,
* and the form's wallpaper is set to the specified picture.
IF oDataObject.GetFormat("Picture")
   This.Picture = oDataObject.GetData("Picture")
ENDIF

See Also

Alignment, DataObject, GetData, GetFormat, NoDefault, OLE drag and drop, OLEDragMode, OLEDragDrop, SetData, SetFormat