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
Dim sFiles() As String = Split(RichTextBox1.Text.TrimEnd, vbCrLf)
Dim DropEffectData(3) As Byte
Select Case TextBox1.Text.Substring(0, 1).ToString
Case 2
DropEffectData(0) = 2
Case 5
DropEffectData(0) = 5
Case Else
DropEffectData(0) = 5
End Select
DropEffectData(1) = 0
DropEffectData(2) = 0
DropEffectData(3) = 0
Dim mstream As Stream = New MemoryStream(4)
mstream.Write(DropEffectData, 0, DropEffectData.Length)
Dim data_object As New DataObject()
data_object.SetData("FileDrop", True, sFiles)
data_object.SetData("Preferred DropEffect", mstream)
Clipboard.Clear()
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.