获取从“资源管理器”发送到 Clipboard 里面的文件列表

类别:.NET开发 点击:0 评论:0 推荐:

当从“资源管理器”复制/剪贴文件时,文件列表会被存放到“剪贴板”Clipboard。可以用下面的程序获取得这时的文件列表,包括是“复制”还是“剪贴”的情况。

 

在VB里面新建一个类模块,添加如下的代码即可。

 

'//本文由 Virtualalloc 原创,转载请注明出处。

Private Type POINTAPI
    x       As Long
    y       As Long
End Type

Private Type DROPFILES
    pFiles  As Long
    pt      As POINTAPI
    fNC     As Long
    fWide   As Long
End Type


'//global memory
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal ByteLen As Long)

'clipboard
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function RegisterClipboardFormat Lib "user32" Alias "RegisterClipboardFormatA" (ByVal lpString As String) As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" (ByVal hDrop As Long, ByVal iFile As Long, ByVal lpszFile As String, ByVal cch As Long) As Long

Public Enum FileDropEffectConstants
  fdDropEffectNone = 0
  fdDropEffectCopy = 1
  fdDropEffectMove = 2
  fdDropEffectLink = 4
End Enum


'//Clipboard Formats
Private Const CF_TEXT = 1
Private Const CF_BITMAP = 2
Private Const CF_METAFILEPICT = 3
Private Const CF_SYLK = 4
Private Const CF_DIF = 5
Private Const CF_TIFF = 6
Private Const CF_OEMTEXT = 7
Private Const CF_DIB = 8
Private Const CF_PALETTE = 9
Private Const CF_PENDATA = 10
Private Const CF_RIFF = 11
Private Const CF_WAVE = 12
Private Const CF_UNICODETEXT = 13
Private Const CF_ENHMETAFILE = 14
Private Const CF_HDROP = 15
Private Const CF_LOCALE = 16
Private Const CF_MAX = 17

'other clipboard formats
Private Const CFSTR_SHELLIDLIST As String = "Shell IDList Array"
Private Const CFSTR_SHELLIDLISTOFFSET As String = "Shell Object Offsets"
Private Const CFSTR_NETRESOURCES As String = "Net Resource"
Private Const CFSTR_FILEDESCRIPTOR As String = "FileGroupDescriptor"
Private Const CFSTR_FILECONTENTS As String = "FileContents"
Private Const CFSTR_FILENAME As String = "FileName"
Private Const CFSTR_PRINTERGROUP As String = "PrinterFriendlyName"
Private Const CFSTR_FILENAMEMAP As String = "FileNameMap"

Private Const CFSTR_PREFERREDDROPEFFECT As String = "Preferred DropEffect"

'global memory
Private Const GMEM_FIXED = &H0
Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_NOCOMPACT = &H10
Private Const GMEM_NODISCARD = &H20
Private Const GMEM_ZEROINIT = &H40
Private Const GMEM_MODIFY = &H80
Private Const GMEM_DISCARDABLE = &H100
Private Const GMEM_NOT_BANKED = &H1000
Private Const GMEM_SHARE = &H2000
Private Const GMEM_DDESHARE = &H2000
Private Const GMEM_NOTIFY = &H4000
Private Const GMEM_LOWER = GMEM_NOT_BANKED
Private Const GMEM_VALID_FLAGS = &H7F72
Private Const GMEM_INVALID_HANDLE = &H8000
Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)

 

Public Function ClipboardSetFiles(sfiles() As String, ByVal lcount As Long) As Boolean
'//send files to clipboard
   
    Dim sTmp        As String
    Dim dfs         As DROPFILES
    Dim hGlobal     As Long
    Dim lpGlobal    As Long
    Dim i           As Long
   
    '//Open clipboard
    If OpenClipboard(0) Then
   
        EmptyClipboard  'empty clipboard
       
        '//combine files
        For i = 0 To lcount - 1
            sTmp = sTmp & sfiles(i) & vbNullChar
        Next
        sTmp = sTmp & vbNullChar
       
        hGlobal = GlobalAlloc(GHND, Len(dfs) + Len(sTmp)) 'Allocate
       
        If hGlobal Then
           
            '//make DROPFILES
            lpGlobal = GlobalLock(hGlobal)
            dfs.pFiles = Len(dfs)
            CopyMemory ByVal lpGlobal, dfs, Len(dfs)
            CopyMemory ByVal lpGlobal + Len(dfs), ByVal sTmp, Len(sTmp)
            GlobalUnlock hGlobal
           
            ClipboardSetFiles = SetClipboardData(CF_HDROP, hGlobal) 'copy sTmp to clipboard
        End If
       
        CloseClipboard  'close
    End If
   
End Function

Public Function ClipboardGetFiles(sfiles() As String, lcount As Long, _
                Optional ByRef DropEffect As FileDropEffectConstants) As Boolean
               
'//get files from clipboard

    '//If Clipboard.GetFormat(vbCFFiles) Then........
   
    Dim hDrop        As Long
    Dim sTmp         As String
    Dim i            As Long
   
    Const MAX_PATH As Long = 260
   
    '//open clipboard
    If IsClipboardFormatAvailable(CF_HDROP) Then
        If OpenClipboard(0) Then
       
            'Get Filelist data
            hDrop = GetClipboardData(CF_HDROP)
            lcount = DragQueryFile(hDrop, -1, vbNullString, 0)
           
            'file entries
            lcount = lcount
            ReDim sfiles(lcount - 1)
           
            sTmp = String(MAX_PATH, Chr(0))
           
            'get file
            For i = 0 To lcount - 1
                DragQueryFile hDrop, i, sTmp, MAX_PATH
                sfiles(i) = NullTrim(sTmp)
            Next i
           
            Dim lngFormat As Long
            Dim lngEffect As Long
           
            lngFormat = RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT)
            hDrop = GetClipboardData(lngFormat)
           
            If (hDrop) Then
                CopyMemory lngEffect, ByVal hDrop, 4
                DropEffect = lngEffect
            End If
            'close
            Call CloseClipboard
            ClipboardGetFiles = True   'return
        End If
    End If
  
End Function


Private Function NullTrim(ByRef inString As String) As String
    Dim iPos As Integer
    iPos = InStr(inString, Chr$(0))
    If iPos > 0 Then
        NullTrim = Left$(inString, iPos - 1)
    Else
        NullTrim = inString
    End If
End Function

'//本文由 Virtualalloc 原创,转载请注明出处。

本文地址:http://com.8s8s.com/it/it44127.htm