Support OLE Drag and Drop

This article first appeared in Visual Developer Magazine
(Note: This is my unedited original and may differ slightly from the published version.)
By Matt Hart

Visual Basic 5 introduced direct support for drag and drop operations through the OLEDragDrop event. Standard pasting can also be handled through this event. The DataObject passed in the OLEDragDrop event is similar to the Clipboard object. You can handle both objects in the same procedure using a Variant. The GetFormat method returns True when you "guess" the correct data format. The reason a format property isn’t given is because the Clipboard (and OLE DataObject) can contain more than one type of data. Text, for example, is usually passed as ANSI text, OEM text, and UNICODE text.

The GetOLEData procedure below shows how to handle both Clipboard and DataObject data. The GetData method returns everything except Clipboard text. Place a ListBox and a CommandButton on a Form. The CommandButton will clear the Clipboard, and the ListBox will receive the multiple dropped files. The KeyDown event of the Form is handling a Ctrl-V paste.

Private Sub GetOLEData(vData As Variant)
    Dim CF As Long, k As Long
    Do
        CF = CF + 1
        If CF = &H18 Then Exit Do
        If vData.GetFormat(CF) Then
            Select Case CF
                Case vbCFText
                    If TypeOf vData Is Clipboard Then
                        MsgBox "Text - " & CF & vbCrLf & _
                                vData.GetText
                    Else
                        MsgBox "Text - " & CF & vbCrLf & _
                               vData.GetData(CF)
                    End If
                Case vbCFBitmap, vbCFMetafile, vbCFEMetafile
                    Picture = vData.GetData(CF)
                    MsgBox "Graphic - " & CF
                Case vbCFDIB
                    MsgBox "DIB - " & CF
                Case vbCFPalette
                    MsgBox "Palette - " & CF
                Case vbCFFiles
                    List1.Clear
                    For k = 1 To vData.Files.Count
                        List1.AddItem vData.Files(k)
                    Next
                    MsgBox "Files - " & CF
                Case vbCFRTF
                    MsgBox "RTF - " & CF
                Case Else
                    MsgBox "Unknown - " & CF
            End Select
        End If
    Loop
End Sub

Private Sub Command1_Click()
    Clipboard.Clear
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim aTxt As String
    If Shift = vbCtrlMask And KeyCode = vbKeyV Then
        GetOLEData Clipboard
    End If
End Sub

Private Sub Form_Load()
    OLEDropMode = vbOLEDropManual
    KeyPreview = True
End Sub

Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, _
                             Button As Integer, Shift As Integer, _
                             X As Single, Y As Single)
    GetOLEData Data
End Sub

Back to Articles
Copyright © 2000 by Matt E. Hart, All Rights Reserved Worldwide.
Nothing on this web site may be reproduced, in any form, without express written consent.