Click here to Skip to main content
15,867,756 members
Articles / Programming Languages / Visual Basic

Getting the Clipboard File DropEffect in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.13/5 (10 votes)
19 Jan 2009CPOL1 min read 42.7K   516   17   1
How to access the file DropEffect in VB.NET in order to tell cut from paste for a file DropList.

Introduction

In VB6, you could use various API calls to get the FileDropList of files on the Clipboard as well as a couple of more calls to get the DropEffect that tells whether the files where copied or cut from the Windows File Explorer.

In VB.NET, it’s even easier to get the FileDropList from the clipboard using the specialized string collection. But I could not find how to get the DropEffect. By looking at what the old method did, and using a C# example I found, I was able to get at the DropEffect.

To get the DropEffect, set an Object to My.Computer.Clipboard.GetData("Preferred DropEffect"), and then read the first 4 bytes into a byte array. The first byte is the DropEffect. I am not using the other 3 bytes, but the old code was grabbing them due to variable formats, so I did too.

Get FileDropList and DropEffect in VB6

In VB6, you can use calls to the Clipboard functions to retrieve the file list, and then another call to get the data with the drop effect. A call to MoveMemory puts the data in a format we can use. (Full examples of how to do this in VB6 can be found elsewhere).

VB
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function IsClipboardFormatAvailable _
        Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function RegisterClipboardFormat Lib "user32" +
        Alias "RegisterClipboardFormatA" (ByVal lpString As String) As Long

Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
       (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)

   Dim hDrop         As Long
   Dim nFiles        As Long
   Dim i             As Long
   Dim desc          As String
   Dim Filename      As String
   Dim pt            As POINTAPI
   Dim lngEffect     As Long
   Dim lngFormat     As Long
   Dim hGlobal       As Long
   Const MAX_PATH    As Long = 260
   
   ' Insure desired format is there, and open clipboard.
   If IsClipboardFormatAvailable(CF_HDROP) Then
      If OpenClipboard(0&) Then
         
         ' Get handle to Dropped Filelist data, and number of files.
         hDrop = GetClipboardData(CF_HDROP)
         nFiles = DragQueryFile(hDrop, -1&, "", 0)
         
         ' Allocate space for return and working variables.
         ReDim Files(0 To nFiles - 1) As String
         Filename = Space(MAX_PATH)
         
         ' Retrieve each filename in Dropped Filelist.
         For i = 0 To nFiles - 1
            Call DragQueryFile(hDrop, i, Filename, Len(Filename))
            Files(i) = TrimNull(Filename)
         Next
         
         lngFormat = RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT)
         hGlobal = GetClipboardData(lngFormat)
      
         If (hGlobal) Then
           MoveMemory lngEffect, ByVal hGlobal, 4
           DropEffect = lngEffect
         End If

      End If
End If

Get FileDropList and DropEffect in VB.NET

Getting the DropEffect in VB.NET requires getting the “Preferred DropEffect” data off the clipboard and reading it with a memory stream.

VB
'*>-------------------------------------------------------------------
'*>Handle Droplist
'*>-------------------------------------------------------------------
If My.Computer.Clipboard.ContainsFileDropList() Then

    '*>-------------------------------------------------------------------
    '*>Get DropEffect Type
    '*>-------------------------------------------------------------------
    Dim DropEffectData(3) As Byte
    Dim DropEffectCheck As Object = _
        My.Computer.Clipboard.GetData("Preferred DropEffect")
    DropEffectCheck.Read(DropEffectData, 0, DropEffectData.Length)

    Select Case DropEffectData(0)
        Case 2
            DropEff = DragDropEffects.Move
            DropEffType = "Move"
        Case 5
            DropEff = DragDropEffects.Copy
            DropEffType = "Copy"
        Case Else
            DropEffType = "???"
    End Select

    DropEffNum = DropEffectData(0).ToString
    TextBox1.Text = String.Concat(DropEffNum, " - ", DropEffType)

    '*>-------------------------------------------------------------------
    '*>Get File Names
    '*>-------------------------------------------------------------------
    Dim FileNameCollection As Collections.Specialized.StringCollection = _
                              My.Computer.Clipboard.GetFileDropList()
    For Each FileName As String In FileNameCollection
        RichTextBox1.Text = String.Concat(RichTextBox1.Text, FileName, vbCrLf)
    Next

End If

Sample App

The sample application shows the DropEffect and the list of files you put on the Clipboard. Switch between cutting and copying files from the File Explorer to see the DropEffect change.

Image 1

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThanks a lot! Pin
Pamodh23-Aug-10 6:32
Pamodh23-Aug-10 6:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.