|
本帖最后由 joforn 于 2021-4-2 11:08 编辑
这是一段共享了很长时间的古老代码了,原来不支持64位系统,有些小伙伴自己不会修改成兼容64位代码,在私信中问,一下子又没找不到他的帖子,只好重新发送共享
- '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- '>>>>>>>> Author: Joforn <<<<<<<<<<<<<<<<<<
- '>>>>>>>> Email: [email]Joforn@sohu.com[/email] <<<<<<<<<<<<<<<<<<
- '>>>>>>>> QQ: 42978116 <<<<<<<<<<<<<<<<<<
- '>>>>>>>> Create time: 04/11/2012 <<<<<<<<<<<<<<<<<<
- '>>>>>>>> Last time : 04/02/2021 <<<<<<<<<<<<<<<<<<
- '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- Option Explicit
- '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- '>\\\\\\\\\\\\\\\\\\\\\\\ API函数定义开始 /////////////////////////<
- '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- #If VBA7 Then
- Private Declare PtrSafe Function RegisterClipboardFormat Lib "user32.dll" Alias "RegisterClipboardFormatW" (ByVal lpString As LongPtr) As Long
- Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hWnd As LongPtr) As Long
- Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
- Private Declare PtrSafe Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As LongPtr) As Long
- Private Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
- Private Declare PtrSafe Function GlobalLock Lib "KERNEL32" (ByVal hMem As LongPtr) As LongPtr
- Private Declare PtrSafe Function GlobalUnlock Lib "KERNEL32" (ByVal hMem As LongPtr) As Long
- Private Declare PtrSafe Function GlobalAlloc Lib "KERNEL32" (ByVal flags As Long, ByVal Size As Long) As LongPtr
- Private Declare PtrSafe Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (ByVal hpvDest As LongPtr, ByVal hpvSource As LongPtr, ByVal cbCopy As Long)
- #Else
- Private Declare Function RegisterClipboardFormat Lib "user32.dll" Alias "RegisterClipboardFormatW" (ByVal lpString As Long) As Long
- Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
- Private Declare Function CloseClipboard Lib "user32" () As Long
- 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 GlobalLock Lib "KERNEL32" (ByVal hMem As Long) As Long
- Private Declare Function GlobalUnlock Lib "KERNEL32" (ByVal hMem As Long) As Long
- Private Declare Function GlobalAlloc Lib "KERNEL32" (ByVal flags As Long, ByVal Size As Long) As Long
- Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (Byval hpvDest As Long,Byval hpvSource As Long, ByVal cbCopy As Long)
- #End If
- '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- '>\\\\\\\\\\\\\\\\\\\\\\\ API函数定义结束 /////////////////////////<
- '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- Private Const CF_HDROP As Long = 15&
- Private Const DROPEFFECT_COPY As Long = 1
- Private Const DROPEFFECT_MOVE As Long = 2
- Private Const GMEM_ZEROINIT As Long = &H40
- Private Const GMEM_MOVEABLE As Long = &H2
- Private Const GMEM_DDESHARE As Long = &H2000
- '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- '>\\\\\\\\\\\\\\\\\\\\\\\ 结构定义开始 /////////////////////////<
- '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- 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
- '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- '>\\\\\\\\\\\\\\\\\\\\\\\ 结构定义结束 ////////////////////////<
- '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
- Public Function CutOrCopyFiles(FileList As Variant, Optional ByVal CopyMode As Boolean = True) As Boolean
- Dim uDropEffect As Long, I As Long
- Dim dropFiles As dropFiles
- Dim uGblLen As Long, uDropFilesLen As Long
- #If VBA7 Then
- Dim hGblFiles As LongPtr
- Dim hGblEffect As LongPtr
- Dim mPtr As LongPtr
- #Else
- Dim hGblFiles As Long
- Dim hGblEffect As Long
- Dim mPtr As Long
- #End If
- Dim FileNames As String
-
- If OpenClipboard(0) Then
- EmptyClipboard
- FileNames = GetFileListString(FileList)
- If Len(FileNames) Then
- uDropEffect = RegisterClipboardFormat(StrPtr("Preferred DropEffect"))
- hGblEffect = GlobalAlloc(GMEM_ZEROINIT Or GMEM_MOVEABLE Or GMEM_DDESHARE, Len(uDropEffect))
- mPtr = GlobalLock(hGblEffect)
- I = IIf(CopyMode, DROPEFFECT_COPY, DROPEFFECT_MOVE)
- CopyMemory mPtr, VarPtr(I), Len(I)
- GlobalUnlock hGblEffect
- SetClipboardData uDropEffect, hGblEffect
-
- uDropFilesLen = LenB(dropFiles)
- With dropFiles
- .pFiles = uDropFilesLen
- .fWide = CLng(True)
- End With
- uGblLen = uDropFilesLen + LenB(FileNames) + 8
- hGblFiles = GlobalAlloc(GMEM_ZEROINIT Or GMEM_MOVEABLE Or GMEM_DDESHARE, uGblLen)
- mPtr = GlobalLock(hGblFiles)
- CopyMemory mPtr, VarPtr(dropFiles), uDropFilesLen
- mPtr = mPtr + uDropFilesLen
- hGblEffect = StrPtr(FileNames)
- I = LenB(FileNames)
- CopyMemory mPtr, hGblEffect, I
-
- GlobalUnlock hGblFiles
- SetClipboardData CF_HDROP, hGblFiles
- End If
- CloseClipboard
- End If
- End Function
- Private Function GetFileListString(FileList As Variant) As String
- Dim I As Long
-
- On Error GoTo GetFileListStringLOOP
- Select Case VarType(FileList)
- Case vbString
- GetFileListString = Trim$(FileList)
- Case &H2008
- For I = LBound(FileList) To UBound(FileList)
- FileList(I) = Trim$(FileList(I))
- If Len(FileList(I)) Then GetFileListString = GetFileListString & FileList(I) & vbNullChar
- Next I
- If Len(GetFileListString) Then GetFileListString = Left$(GetFileListString, Len(GetFileListString) - 1)
- End Select
- GetFileListStringLOOP:
- End Function
- Sub Test2Files()
- Dim FileList() As String
-
- ReDim FileList(0 To 1)
- FileList(0) = "C:\Windows\regedit.exe"
- FileList(1) = "C:\Windows\system32\gpedit.msc"
- CutOrCopyFiles FileList
- End Sub
- Sub Test1File()
- CutOrCopyFiles "C:\Windows\regedit.exe"
- End Sub
复制代码
运行Test1File或Test2Files后,在Explorer中点右键粘贴可见效果。
|
评分
-
7
查看全部评分
-
|