Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Setting the Clipboard File DropList with DropEffect in VB.Net

0.00/5 (No votes)
20 Jan 2009 1  
How you Set the File Drop Effect so you can cut files from VB.Net to the file explorer

Introduction

In VB6, it is possible to use your app to put a list of files on the windows clipboard along with data telling if you want to cut or copy those files. Then the Windows file explorer can then take that list and copy or move those files. A lot of API calls are used to accomplish this.

In VB.Net, it is also possible to put this list of files, called a file drop list, on the clipboard as well as the DropEffect which tells Windows File explorer if the file list should be moved (a cut) or copied. You do not need all the API calls that were used in VB6 to accomplish it.

Set DropEffect in VB6:

In VB6 the file droplist is set with API calls to OpenClipboard, CopyMem, and SetClipboardData. The drop Effect is set with MoveMemory and RegisterClipboardFormat. Below is the function that is called in VB6 to set the drop effect. It is called with a vbDropEffectCopy to request a copy, and a vbDropEffectMove to request a cut.

Public Function SetFilesDropEffect(ByVal lEffect As Long) As Boolean
    Dim lMemoryHandle As Long
    Dim lMemoryPointer As Long
    Dim lFormat As Long
    Dim lReturn As Long
    ' Open the clipboard.
    If OpenClipboard(0) Then
        ' Get global memory pointer to hold the lEffect variable
        lMemoryHandle = GlobalAlloc(GHND, Len(lEffect))
        ' If we have a memory handle
        If lMemoryHandle Then
            ' Lock the memory while we initialize it.
            lMemoryPointer = GlobalLock(lMemoryHandle)
            ' Copy the lEffect variable into the global memory.
            Call MoveMemory(ByVal lMemoryPointer, lEffect, Len(lEffect))
            ' Unlock the memory
            Call GlobalUnlock(lMemoryHandle)
            ' Register the format
            lFormat = RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT)
            ' Copy the format to the clipboard.
            lReturn = SetClipboardData(lFormat, lMemoryHandle)
            ' Check if we succeed
            If lReturn Then
                SetFilesDropEffect = True
            End If
        End If
        ' Close the clipboard.
        Call CloseClipboard
    End If
End Function

Set FileDropList and DropEffect in VB.Net:

In VB.Net, the Clipboard.SetFileDropList function can be used to place a list of files on the clipboard. It does not however have any way to set the DropEffect to tell File Explorer that you want to cut files instead of copy. So everything you put on the clip board this way is a copy.

If you use the SetDataObject to write a correctly created DataObject to the clipboard, you can pass the DropEffect as well as the DropFile List.

Setting the DropEffect in VB.Net requires creating a Data Object and writing it to the clipboard with Clipboard.SetDataObject. The Data Object needs to contain both the File DropList and DropEffect. They are put into the Object with the SetData method.

1) You need a String array of the filenames. It needs to be trimmed so there are no trailing spaces.
2) The DropEffectData is a 4 byte long byte array. The first byte is either 2 for move or 5 for copy. The remaining bytes are zeros.
3) Turn the DropEffectData into a 4 byte memory stream. This is the format of the Preferred Drop Effect data.
4) Create the Data Object.
5) Using SetData write the File DropList and DropEffect memory stream to the Data Object.
6) Clear the clipboard (this isn't required).
7) Write the data object to the clipboard with SetDataObject.

Imports System.IO

        '*>---------------------------------------------------
        '*>Set Drop File Names in String Array
        '*>---------------------------------------------------
        Dim sFiles() As String = Split(RichTextBox1.Text.TrimEnd, vbCrLf)

        '*>---------------------------------------------------
        '*>Set Drop File Effect in Memory Stream
        '*>---------------------------------------------------
        Dim DropEffectData(3) As Byte
        Select Case TextBox1.Text.Substring(0, 1).ToString
            Case 2 'Move
                DropEffectData(0) = 2
            Case 5 'Copy
                DropEffectData(0) = 5
            Case Else 'Copy
                DropEffectData(0) = 5
        End Select
        DropEffectData(1) = 0
        DropEffectData(2) = 0
        DropEffectData(3) = 0

        '*>---------------------------------------------------
        '*>Create Memory Stream From DropEffectData
        '*>---------------------------------------------------
        Dim mstream As Stream = New MemoryStream(4)
        mstream.Write(DropEffectData, 0, DropEffectData.Length)

        '*>---------------------------------------------------
        '*>Create Data Object with DropList and DropEffect
        '*>---------------------------------------------------
        Dim data_object As New DataObject()
        data_object.SetData("FileDrop", True, sFiles)
        data_object.SetData("Preferred DropEffect", mstream)

        '*>---------------------------------------------------
        '*>Clear Clipboard
        '*>---------------------------------------------------
        Clipboard.Clear()

        '*>---------------------------------------------------
        '*>Put Data Object on Clipboard
        '*>---------------------------------------------------
        Clipboard.SetDataObject(data_object, True)

Sample App

The Sample app will read the clipboard. If it finds a File DropList (from a File Explorer cut or paste), it will display the contents and DropEffect. The Set File DropList button will then Enable.

Clicking the Set File Droplist will execute the code above which Copies the data from the form and puts it back on the clipboard. You can then finish the cut or paste back in Explorer.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here