在VB.NET中获取剪贴板文件

 

介绍 在VB6中,您可

  

 

以使用各种API调用来获取剪贴板上文件的FileDropList,以及更多的调用来获取DropEffect,它告诉您文件是从Windows文件资源管理器复制或剪切的位置。 在VB。NET,它甚至更容易从剪贴板获得FileDropList使用专门的字符串集合。但我不知道如何得到下降效果。通过查看旧方法的作用,并使用我找到的一个c#示例,我能够得到DropEffect。 要获得DropEffect,将一个对象设置为My.Computer.Clipboard。GetData("Preferred DropEffect"),然后将前4个字节读入字节数组。第一个字节是DropEffect。我没有使用其他3个字节,但旧代码抓取他们由于格式可变,所以我做了。 在VB6中获取FileDropList和DropEffect 在VB6中,您可以使用对剪贴板函数的调用来检索文件列表,然后再使用另一个调用来获得具有drop效果的数据。对MoveMemory的调用将数据置于我们可以使用的格式中。(在VB6中如何做到这一点的完整示例可以在其他地方找到)。 隐藏,收缩,复制Code

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

在VB.NET中获取FileDropList和DropEffect 在VB中得到DropEffect。NET要求从剪贴板中获取“首选DropEffect”数据,并用内存流读取它。 隐藏,收缩,复制Code

'*>-------------------------------------------------------------------
'*>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

样例应用程序 示例应用程序显示了DropEffect和放在剪贴板上的文件列表。在从文件资源管理器中剪切和复制文件之间切换,以查看DropEffect更改。 本文转载于:http://www.diyabc.com/frontweb/news186.html

posted @ 2020-08-04 03:32  Dincat  阅读(788)  评论(0编辑  收藏  举报