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

原创 2004年09月13日 20:05:00

当从“资源管理器”复制/剪贴文件时,文件列表会被存放到“剪贴板”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 原创,转载请注明出处。

获取在Windows 资源管理器中执行COPY/PASTE的文件列表信息

在文件传输模块开发过程中,要实现这样的一个功能:在资源管理器中对选中的文件或者文件夹CTRL+C,在聊天窗口中按下CTRL+V,获取用户选择的文件或文件夹发送给对端用户。这其中存在一个问题:如何从剪切...
  • chenlycly
  • chenlycly
  • 2012年03月30日 16:26
  • 1438

从Windows资源管理器中拖动文件

这里我们主要简单的讨论一下,如何从Windows资源管理器中拖动一组文本,然后将它们拖放到一个文本框控件中就可将文件全部打开。为了说明这一点,以下过程可用一个文本框控件以及OLEDragOver和OL...
  • yzy912
  • yzy912
  • 2004年06月14日 17:23
  • 983

awk处理多个文件

awk处理文件。本质上是把一个文件
  • ssmale
  • ssmale
  • 2014年11月07日 15:57
  • 695

java GUI类制作的资源管理器小程序

package cn.itcast.day_19.exercise; import java.awt.BorderLayout; import java.awt.Button; import j...
  • daijunjian
  • daijunjian
  • 2012年03月06日 21:46
  • 2446

Android工具类:获取手机的文件夹及文件列表

使用方法:List> list=GetFilesUtils.getInstance().getSonNode(file); //或其他方法 使用示例: package wuwang.mypage.ac...
  • junzia
  • junzia
  • 2014年12月01日 16:30
  • 5150

JAVA 取Json字符串 里面值

import net.sf.json.JSONObject; public class TJsonTest { /** * @param args */ public static v...
  • chenyongtu110
  • chenyongtu110
  • 2015年06月01日 16:42
  • 11525

利用.NET Framework类库中的Clipboard类,实现数据的复制(剪切)和粘贴

 "复制"操作的代码    Private Sub copyToolStripMenuItem11_Click(ByVal sender As System.Object, ByVal e As Sy...
  • ivbapplication
  • ivbapplication
  • 2007年08月13日 11:06
  • 2800

clipboard使用总结

概述clipboard.js是一款轻量级的实现复制文本到剪贴板功能的JavaScript插件。通过该插件可以将输入框,文本域,DIV元素中的文本等文本内容复制到剪贴板中 clipboard.js...
  • hry2015
  • hry2015
  • 2017年04月28日 23:42
  • 15734

读取assets中的图片

今天公司有一个游戏需要改造,电脑上找不到游戏的源码,因此,只能反编译来修改,然后重新打包、签名出包。。。 自己先写一个Demo实现要实现的功能,然后打包,再进行反编译将里面的文件拷贝到原游戏反编译后...
  • jiayouhahao
  • jiayouhahao
  • 2017年02月24日 17:26
  • 885

FtpClient 获取ftp文件列表

FtpClient ftp = (FtpClient) FtpClient.create("mirrors.sohu.com"); ftp.login("Anonymous",...
  • bwlab
  • bwlab
  • 2015年09月10日 22:02
  • 10481
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:获取从“资源管理器”发送到 Clipboard 里面的文件列表
举报原因:
原因补充:

(最多只允许输入30个字)