.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
获取从“资源管理器”发送到 Clipboard 里面的文件列表

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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




相关文章

相关软件