Visual Basic Drag & Drop, Part Two
As I said last week in Part I, drag and drop is everywhere in the Windows operating environment. You can drag files to and from Windows Explorer, the desktop, folders, and icons and shortcuts representing other applications, and while it's common throughout Windows applications in general, it's rare in Visual Basic programs. Last week you learned how to respond when files are dragged onto a program's icon, when objects are dragged within a single program. This week you'll see what happens when objects are dragged from one application to another.
OLE Drag & DropAllowing a program to drag and drop with other applications is a little more complicated. The source and destination applications must perform some negotiation to make the drag and drop work. The programs communicate behind the scenes using Object Linking and Embedding (OLE) so this technique is called OLE drag and drop.
The easiest way to make a control accept OLE drops is to set its OLEDropMode property to 2 - Automatic. In that case, you are done! Visual Basic and the Windows operating system automatically handle all of the details of dragging and dropping.
Program OLEDrag1 uses this technique to drag and drop images. The OLEDragMode properties of the picDragFrom controls at the top are set to 0 - Automatic. The OLEDropMode properties of the picDragTo controls at the bottom are set to 2 - Automatic. The program contains no Visual Basic code whatsoever.
Click and drag pictures from the top set of PictureBoxes into the bottom set or into other applications that can draw pictures. Click and drag pictures from other applications into the bottom PictureBoxes.
To gain greater control over the OLE drag and drop process, set the drop destination control's OLEDropMode property to 1 - Manual. When something is dropped on the control, its OLEDragDrop even handler receives a reference to a DataObject object that describes the data available from the drag source. The program should examine this object to see if the data comes in a format that the control can understand. For example, a PictureBox can verify that the data is a bitmap. The program can then use the DataObject's GetData method to retrieve the data.
Program OLEDrag2 uses this method to handle more data types than the previous example. The picDragTo control's OLEDragDrop event handler uses the DataObject's GetFormat method to determine whether the data is available in bitmap form. If it is, the program copies the bitmap into the control.
If the bitmap format is not available, the program determines whether the data contains a list of file names. If it does, the program examines the first file name to see if it ends with bmp, gif, jpg, or jpeg. If it does, the program uses LoadPicture to load the graphic file into the control.
In program OLEDrag2, the OLEDragMode properties of the picDragFrom controls at the top are set to 0 - Automatic. The OLEDropMode properties of the picDragTo controls at the bottom are set to 2 - Automatic. The program's only source code is shown below.
' Get the dropped picture if the source is a bitmap ' or a graphic file. Private Sub picDragTo_OLEDragDrop(Index As Integer, Data As DataObject, _ Effect As Long, Button As Integer, Shift As Integer, _ X As Single, Y As Single) Dim ext As String ' See if we know what to do with the data. If Data.GetFormat(vbCFBitmap) Then ' Copy the bitmap. picDragTo(Index).Picture = Data.GetData(vbCFBitmap) Effect = vbDropEffectCopy ElseIf Data.GetFormat(vbCFFiles) Then ' See if this is a file name ending in ' bmp, gif, jpg, or jpeg. ext = LCase$(Right$(Data.Files(1), 4)) If (ext = ".bmp") Or _ (ext = ".gif") Or _ (ext = ".jpg") Or _ (ext = "jpeg") _ Then ' Load the file. picDragTo(Index).Picture = LoadPicture(Data.Files(1)) Effect = vbDropEffectCopy Else ' Tell the source we did nothing. Effect = vbDropEffectNone End If Else ' Tell the source we did nothing. Effect = vbDropEffectNone End If End Sub
Drag and ConquerUsing the techniques described in this article, you can add drag start, drag within a program, and drag between programs to your Visual Basic applications. By adding an extra level of sophistication to your programs, you can place them among the ranks of the Windows elite.
Rod Stephens is a former senior member of GTE's technical staff. A programmer for over 13 years, Rod has designed and implemented more than a half dozen award-winning commercial applications, and author over 80 articles and seven books on Visual Basic Programming. Be sure to visit his VB education site, VB-Helper, at www.vb-helper.com!