Share via


Visual Basic Concepts

Dragging the OLE Drag Source over the OLE Drop Target

With a manual target, you can determine and respond to the position of the source data within the target and respond to the state of the mouse buttons and the SHIFT, CTRL, and ALT keys. Where both the source and the target are manual, you can modify the default visual behavior of the mouse.

To . . . Use the . . .
Determine and respond to the position of the source object state argument of the OLEDragOver event
Respond to the state of the mouse buttons button argument of the OLEDragDrop and OLEDragOver events
Respond to the state of the SHIFT, CTRL, and ALT keys shift arguments of the OLEDragDrop and OLEDragOver events
Modify the default visual behavior of the mouse effect argument of the OLEDragOver event and the effect argument of the OLEGiveFeedback

For More Information   For more information about changing the mouse cursor, see "Dragging the OLE Drag Source over the OLE Drop Target." For more information about using the button and shift arguments, see "Using the Mouse and Keyboard to Modify Drop Effects and User Feedback."

The OLEDragOver Event State Argument

Depending upon its position, the effect argument may be changed to indicate the currently acceptable drop effect.

The state argument of the OLEDragOver event allows you to respond to the source data entering, passing over, and leaving the target control. For example, when the source data enters the target control, the state argument is set to vbEnter.

When the drag source is moved around within the boundaries of the drop target, the state argument is set to vbOver. Depending upon the position (the x and y arguments) of the mouse pointer, you may want to change the drag effect. Notice that the OLEDragOver event is generated several times a second, even when the mouse is stationary.

The state argument of the OLEDragOver event specifies when the data enters, passes over, and leaves the target control by using the following constants:

Constant Value Meaning
vbEnter 0 Data has been dragged within the range of a target.
vbLeave 1 Data has been dragged out of the range of a target.
vbOver 2 Data is still within the range of a target, and either the mouse has moved, a mouse or keyboard button has changed, or a certain system-determined amount of time has elapsed.

Providing the User with Customized Visual Feedback

If you want to modify the default visual behavior of the mouse in an OLE drag-and-drop operation, you can manipulate the OLEDragOver event on the target side and the OLEGiveFeedback event on the source side.

OLE drag and drop provides automatic visual feedback during a drag-and-drop operation. For example, when you start a drag, the mouse pointer is changed to indicate that a drag has been initiated. When you pass over objects that do not support OLE drop, the mouse pointer is changed to the "no drop" cursor.

Modifying the mouse pointer to indicate how a control will respond if the data is dropped onto it involves two steps: determining what type of data is in the DataObject object using the GetFormat method, and then setting the effect argument of the OLEDragOver event to inform the source what drop effects are allowed for this control.

The OLEDragOver Event

When a target control’s OLEDropMode property is set to Manual, the OLEDragOver event is triggered whenever dragged data passes over the control.

The effect argument of the OLEDragOver event is used to specify what action would be taken if the object were dropped. When this value is set, the source’s OLEGiveFeedback event is triggered. The OLEGiveFeedback event contains its own effect argument, which is used to provide visual feedback to the user on what action will be taken when the selection is dragged — i.e., the mouse pointer is changed to indicate a copy, move, or "no drop" action.

The effect argument of the OLEDragOver event uses the following constants to indicate the drop action:

Constant Value Description
vbDropEffectNone 0 Drop target cannot accept the data.
vbDropEffectCopy 1 Drop results in a copy. The original data is untouched by the drag source.
vbDropEffectMove 2 Drag source removes the data.

Note   The effect argument of the OLEDragOver and OLEGiveFeedback events express the same drop effects (copy, move, no drop) as the allowedeffects argument of the OLEStartDrag event. They differ only in that the OLEStartDrag event specifies which effects are allowed, and the OLEDragOver and OLEGiveFeedback use the effect argument to indicate to the source which of these actions will be taken.

The following code example queries the DataObject object for a compatible data format for the target control. If the data is compatible, the effect argument informs the source that a move will be performed if the data is dropped. If the data is not compatible, the source will be informed and a "no drop" mouse pointer will be displayed.

Private Sub txtTarget_OLEDragOver(Data As _
      VB.DataObject, Effect As Long, Button As _
      Integer, Shift As Integer, X As Single, _
      Y As Single, State As Integer)
   If Data.GetFormat(vbCFText) Then
      Effect = vbDropEffectMove And Effect
   Else
      Effect = vbDropEffectNone
   End If
End Sub

When the source data is dragged over the target, and the OLEDragOver event is triggered, the source tells the target which effects it allows (move, copy, no drop). You must then chose which single effect will occur if the data is dropped. The effect argument of the OLEDragOver event informs the source which drop action it supports, and the source then informs the user by using the OLEGiveFeedback event to modify the mouse pointer.

The OLEGiveFeedback Event

To change the default behavior of the mouse pointer based on the effect argument of the OLEDragOver event, you need to manually specify new mouse pointer values using the OLEGiveFeedback event. The source’s OLEGiveFeedback event is triggered automatically when the effect argument of the OLEDragOver event is set.

The OLEGiveFeedback event contains two arguments (effect and defaultcursors) that allow you to modify the default mouse pointers in an OLE drag-and-drop operation.

The effect argument, like the other OLE drag-and-drop events, specifies whether data is to be copied, moved, or rejected. The purpose of this argument in the OLEGiveFeedback event, however, is to allow you to provide customized visual feedback to the user by changing the mouse pointer to indicate these actions.

Constant Value Description
vbDropEffectNone 0 Drop target cannot accept the data.
vbDropEffectCopy 1 Drop results in a copy. The original data is untouched by the drag source.
vbDropEffectMove 2 Drag source removes the data.
vbDropEffectScroll &H80000000& Scrolling is about to start or is currently occurring in the target. The value is used in addition to the other values.

Note   The vbDropEffectScroll value can be used by some applications or controls to indicate that the user is causing scrolling by moving the mouse pointer near the edge of an application’s window. Scrolling is automatically supported by some but not all of the Visual Basic standard controls. You may need to program for the scroll effect if you drag data into a program that contains scroll bars — Word for Windows, for example.

The defaultcursors argument specifies whether the default OLE cursor set is used. Setting this argument to False allows you to specify your own cursors using the Screen.MousePointer property of the Screen object.

In most cases, specifying custom mouse pointers is unnecessary because the default behavior of the mouse is handled by OLE. If you decide to specify custom mouse pointers using the OLEGiveFeedback event, you need to account for every possible effect, including scrolling. It is also a good idea to program for effects that may be added later by creating an option that gives the control of the mouse pointer back to OLE if an unknown effect is encountered.

The following code example sets the effect and defaultcursors arguments and specifies custom cursors (.ico or .cur files) for the copy, move, and scroll effects by setting the MousePointer and MouseIcon properties of the Screen object. It also returns control of the mouse pointer back to OLE if an unknown effect is encountered.

Private Sub TxtSource_OLEGiveFeedback(Effect As Long, _
      DefaultCursors As Boolean)
   DefaultCursors = False
   If Effect = vbDropEffectNone Then
      Screen.MousePointer = vbNoDrop
   ElseIf Effect = vbDropEffectCopy Then
      Screen.MousePointer = vbCustom
      Screen.MouseIcon = LoadPicture("c:\copy.ico")
   ElseIf Effect = (vbDropEffectCopy Or _
         vbDropEffectScroll) Then
      Screen.MousePointer = vbCustom
      Screen.MouseIcon = _
         LoadPicture("c:\copyscrl.ico")
   ElseIf Effect = vbDropEffectMove Then
      Screen.MousePointer = vbCustom
      Screen.MouseIcon = LoadPicture("c:\move.ico")
   ElseIf Effect = (vbDropEffectMove Or _
         vbDropEffectScroll) Then
      Screen.MousePointer = vbCustom
      Screen.MouseIcon = _
         LoadPicture("c:\movescrl.ico")
   Else
      ' If some new format is added that we do not
      '   understand, allow OLE to handle it with
      '   correct defaults.
      DefaultCursors = True
   End If
End Sub

Note   You should always reset the mouse pointer in the OLECompleteDrag event if you specify a custom mouse pointer in the OLEGiveFeedback event. For more information about informing the source when data is dropped, see "Dropping the OLE Drag Source onto the OLE Drop Target."

For More Information   See "Customizing the Mouse Pointer" for information on setting the MousePointer and MouseIcon properties.