ll 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
. 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 examines 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. Traditionally, the drag-and-drop operation moves the data or, if the Ctrl key is depressed, copies the data.
Only a subset of VB controls support OLE drag-and-drop. Those that can act as a source as well as a target are:
The following components are limited to serving as a target: