Sie sind auf Seite 1von 7

OLE Drag-and-Drop in Visual Basic 6

Peter G. Aitken

All graphical user interfaces, including Windows, make excellent use of the mouse. One
way that the mouse can be used is drag-and-drop, the ability to initiate some action by
dragging from one element on the screen to another. You might drag a file name to the
recycle bin icon to delete it, drag an image to a printer icon to print it, or drag selected
text to another location to copy it. Visual Basic provides excellent support for drag-and-
drop. One type of drag-and-drop, sometimes referred to as traditional drag-and-drop, is
limited to dragging between controls in the same application. I covered traditional drag-
and-drop in an earlier article [[ref article?]]. The second type, called OLE (for Object
Linking and Embedding) drag-and-drop, lets the user drag data between applications as
well as within an application. In this article I explain how to implement OLE drag-and-
drop in your VB 6 applications.

OLE drag-and-drop works only with applications that support it. This is the case with
most Microsoft applications and many programs from other vendors as well. If you
attempt OLE drag-and-drop with an application that does not support it, the operation
simply does not work.

The DataObject Class

The DataObject class is at the heart of OLE drag-and-drop. When a drag-and-drop


operation is started the data that is being dragged is packaged in a DataObject object.
When the item is dropped this object is available to the target, which can examine the
DataObject to determine if the data it contains is in a format that the target can accept. If
so, the data is accepted by the target. If you have Microsoft Office installed you can see
OLE drag-and-drop in action by starting Word and Excel, selecting some text in Word,
and dragging it to Excel. The text appears in the Excel worksheet cell where it was
dropped. You can use OLE drag-and-drop with more complex kind of data as well, such
as charts and images. By tradition, the drag-and-drop operation moves the data or, if the
Ctrl key is depressed, copies the data.

Onky a subset of VB controls support OLE drag-and-drop. Those that can act as a source
as well as a target are:

ComboBox
DBCombo
DBList
DirListBox
FileListBox
Image
ListBox
PictureBox
TextBox
The following components are limited to serving as a target:

CheckBox
CommandButton
Data
Form
Frame
Label
MDIForm
OptionButton
PropertyPage
UserControl
UserDocument

Preoperties and Events for Drag and Drop

There are several properties and events related to OLE drag-and-drop, some relevant to
sources and some to targets. They are described in Tables 1 and 2.

Table 1. OLE drag-and-drop related properties.

Property Relevant to Description


OLEDragMode Source Determines whether OLE drag-and-drop operations
are initiated manually (VbOLEDragManual, the
default) or automatically (vBOLEDragAutomatic).
OLEDropMode Target Specifies whether OLE drops are ignored
(vbOLEDropNone, the default), are handled
manually (vbOLEDropManual), or handled
automatically (vbOLEDropAutomatic).
OLEDragPicture Source Specifies the image displayed under the mouse
cursor during an OLE drag-and-drop operation.
Possible formats are .bmp, .dib, .jpg, .gif, .ani, .cur,
and .ico.
OLEDropEffects Target Specifies the type of drop operations supported by
the target.
OLEDropHasData Target Specifies how the target handles a drop operation.

Table 2. Events related to OLE drag-and-drop operations.

Event Fires in Fires when


OLEDragOver Target A source object is dragged over the target.
OLEDragDrop Target A source object is dropped on the target.
OLEStartDrag Source An OLE drag operation is initiated.
OLECompleteDrag Source The source is dropped on a target.
OLEGiveFeedback Source After every OLEDragOverEvent.
OLESetData Source When the source is dropped on a target but the data
has not been loaded into the DataObject.

Let's look at a simple example of OLE drag-and-Drop using the automatic settings before
getting into the details.

1. Create a new Visual Basic project and place one Text Box and two Picture Box
controls on the form.

2. Set the OLEDragMode and OLEDropMode properties to Automatic for all three
controls.

3. Run the project.

4. Launch a graphics program that supports OLE drag-and-drop. I know that PhotoShop
does - for other programs you'll have to try and see if it works.

Now you are ready to do some experimenting. Open a picture in the graphics program,
select it, then drag it to one of the Picture Box controls in the Visual Basic program.
You'll see that the image is moved to the Picture Box. If you hold down the Ctrl key
while dragging the picture should be copied rather than moved. You can also drag the
picture from one Picture Box to the other. You cannot, however, drag a picture to the Text
Box. To experiment with non-image data, launch your word processor and select some
text. You'll be able to drag the text to both the Text Box and the Picture Box on your
form.

As you have seen, automatic OLE drag-and-drop is quite easy to implement, requiring
only that you set a couple of control properties. Simplicity often is accompanied by lack
of flexibility, and that's the case here too. Automatic OLE drag-and-drop is undoubtedly
more limited than what you can accomplish with manual control.

You need to understand the DataObject class to use manual OLE drag-and-drop. During
an OLE drag-and-drop operation this class is used to store the data and information about
the data's format. Most of the OLE drag-and-drop related event procedures have an
argument that references the DataObject object that is part of the current operation. In the
event procedures, you use the object's methods to manage the data.

The DataObject class has four methods. The Clear() method removes all data and other
information from the DataObject object.

The GetData(format) method returns the data from a DataObject object as a type Variant.
The format argument is a constant that specifies the format of the data, as shown in Table
3. If you omit the format argument or pass a value of 0, the method will attempt to
determine the best format for the data.
Table 3. Format argument values for the GetData method.

Argument Value Format


0 or omitted - Automatic format detection based on
the data.
vbCFText 1 Text
vbCFBitmap 2 Bitmap
vbCFMetafile 3 Metafile
vbCFEMetafile 14 Enhanced metafile
vbCFDIB 8 Device-independent bitmap
vbCFPalette 9 Color palette
vbCFFIles 15 List of files
vbCFRTF -16639 Rich text format.

The SetData method inserts data into a DataObject object using a specified format. The
syntax is:

SetData data, format

Both arguments are optional (although you must use at least one). Data is a type Variant
containing the data to insert, and format is a constant (from Table 3) specifying the
format of the data. If you specify the data argument but not format then Visual Basic will
try to determine the format by examining the data. If you specify format without data
then the specified format is added to the list of formats the existing data is available in.

Let's look at an example. Suppose the variable X contains some text data, and myDO
refers to a DataObject object. The following code makes the data in X available in myDO
as text, as rich text, and as a bitmap:

myDO.SetData X, vbCFText
myDO.SetData , vbCFRTF
myDO.SetData , vbCFBitmap

The GetFormat() method returns a true/false value indicating whether data is available in
the specified format. The method takes a single argument, one of the format constants
from Table 3, and it returns True only if the DataFormat object contains data in the
specified format.

The drag-and-drop related events work together, and the relationship between them is
somewhat confusing. Some actions cause events to fire in both the source and target
objects. Table 4 explains how this works.

Table 4. User actions and corresponding OLE drag-and-drop events.

Action Event procedure(s) fired What your code does


Drag operation starts OLEStartDrag (source) Set the effects (move, copy) that
are permitted. Put the data being
dragged in the DataObject.
Cursor moves over a OLEDragOver (target), then In OLEDragOver: Examine the
potential target. OLEGiveFeedback (source). format of data being dragged
and set the mouse cursor to
indicate whether a drop is
permitted. In
OLEGiveFeedback: change the
appearance of the source control
if desired (optional).
Object is dropped. OLEDragDrop (target) and In OLEDragDrop: check the
then OLECompleteDrag format of the data and retrieve it
(source). if it is acceptible. In
OLECompleteDrag: erase the
data from the souce control
(only if the data is being moved
instead of copied).

The OLEStartDrag event procedure, which fires when a manual OLE drag-and-drop
operation begins, has an AllowedEffects argument which you use to specify the drag-and-
drop effects which will be permitted. The constants for this argument are listed in Table 5,
and can be combined with the Or operator. Which of the allowed operations actually
occurs (at the end of the operation when the drop is done) is determined elsewhere in
code.

Table 5. Constants for the effects argument.

Constant Value Description


vbDropEffectNone 0 Drop target cannot accept the data.
vbDropEffectCopy 1 Drop results in data being copied from source
to target.
vbDropEffectMove 2 Drop results in data being moved from
source to target.

The OLEDragOver, OLEDragDrop, OLECompleteDrag and OLEGiveFeedback event


procedures have an effects argument that is used to specify and to determine the drag-
and-drop operations permitted. You use the constants listed in Table 5 for this argument.
Code in these event procedures can read the effects argument to see what effects have
been permitted by the source, and can also change the value as needed. You'll see how
this works in the sample application to be developed later.

Putting it all Together

Now let's look at the basic steps required to implement manual OLE drag-and-drop for
Visual Basic controls. There are two things to consider: a control that serves as a source,
and a control that serves as a target. Some controls can do both, of course. In either case,
the control's OLEDragMode and OLEDropMode properties must be set to Manual. For a
target control:

1. Place code in the control's OLEDragOver event procedure that checks the format of the
data in the DataObject object. Depending on whether the available format or formats are
appropriate for the control, set the effect argument to the appropriate value
(vbDropEffectNone, vbDropEffectCopy, or vbDropEffectMove). This causes the mouse
cursor to display the appropriate symbol while dragging over the target control.

2. Place code in the control's OLEDragDrop event procedure to check the format of the
DataObject's data. The code can also check the state of the keyboard to see if the Ctrl key
is depressed indicating a Copy operation rather than a move. If the format of the data is
acceptable, and depending on other factors such as the state of the keyboard, set the effect
argument to the appropriate action (vbDropEffectNone, vbDropEffectCopy, or
vbDropEffectMove) and retrieve the data from the DataObject.

When a control is acting as an OLE drag-and-drop source here's what is required.

1. In the control's MouseMove event procedure, check the state of the mouse buttons and
if the left button is depressed initiate an OLE drag-and-drop operation by calling the
control's OLEDrag method.

2. In the control's OLEStartDrag event procedure, which is called as soon as the


OLEDrag method is executed, insert the data from the control into the DataObject object
using the SetData method (although this step can be deferred, as I'll explain soon). If
necessary, call SetData additional times to specify additional data formats. Also, set the
AllowedEffects argument as appropriate.

3. Optionally, put code in the control's OLEGiveFeedback event procedure to provide


visual feedback as the data is dragged over various parts of the screen. Information set in
a target's OLEDragOver event procedure, specifically the effects argument, is available in
the OLEGiveFeedback event procedure. Most often this event procedure is used to
change the appearance of the source control depending on where it is being dragged.

4. In the control's OLECompleteDrag event procedure place any code that is required to
complete the operation. Typically this consists of erasing the original data from the
control after a move operation. For a copy opertation this event procedure is usually not
used.

A Demonstration

The following demonstration program puts these techniques into action. While this is a
simple application, it demonstrates all of the techniques you'll need to implement full-
featured OLE drag-and-drop in your Visual Basic programs. To create the application,
start a new Visual Basic project and place two Text Box controls and one Picture Box
control on its form. Set the OLEDropMode and OLEDragMode properties to Manual for
each of these controls. Here's how the program works:

- You can drag from the first Text Box to copy its data can to a target such as the second
Text Box or another program that supports OLE drag-and-drop of text, such as Microsoft
Word. If you drag the first Text Box over the form or Picture Box, or anywhere else
where a drop is not permitted, the text in the Text Box changes to red.

- The second Text Box operates the same as the first Text Box except you can specify
whether the data is to be copied (by depressing Ctrl) or moved (no keys depressed).

- Bitmap data from another program, such as PhotoShop, can be dragged to the Picture
Box. If bitmap data is dragged over either of the Text Box controls, the "no drop" icon is
displayed.

Listing 1 shows the code for this demo's event procedures. Each procedure is commented
so you can relate it to the explanations in the text. Figure 1 shows the program after an
image was dragged from another program to the Picture Box control.

Figure 1. An image has been dragged to the Picture Box control.

Listing 1. Even procedures in the OLE drag-and-drop demo.

[[use OleDragAndDropListing1.txt]]

Das könnte Ihnen auch gefallen