分享
 
 
 

VB与Windows资源管理器互拷文件

王朝vb·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

VB与Windows资源管理器互拷文件

通过VB编程来拷贝或移动文件的原理可能大家都十分清楚,可以利用Windows API

SHFileOperation来进行操作,也可以利用VB内置的函数来操作。但是利用这些方法编

写的程序只能在程序内部执行文件的操作。这里我要向大家介绍如何通过VB编程将程序

中的文件操作同Windows的资源管理器中的拷贝、剪切操作连接起来。

在Windows的资源管理器中,选中一个或多个文件,在文件上单击鼠标右键,在弹

出菜单中选复制。再切换到另外的目录,单击鼠标右键,点粘贴。就执行了一次文件的

拷贝操作,那么Windows在拷贝过程中执行了什么操作,是否将整个文件拷贝到剪贴版

上了呢?当然没有。实际上,windows只是将一个文件结构拷贝到了剪贴版,这个结构

如下:

tDropFile+文件1文件名+vbNullChar文件2文件名+vbNullChar...+文件N文件名+vbNullChar

其中tDropFile是一个DROPFILES结构,这个结构在Windows API中有定义。在粘贴文件

时,利用API函数 DragQueryFile 就可以获得拷贝到剪贴版的文件全路径名,然后就

可以根据获得的文件名执行文件拷贝函数,实现对文件的粘贴操作。

下面通过具体的程序来介绍:

1、在工程文件中加入一个Module,然后在Module中加入如下代码:

Option Explicit

Private Type POINTAPI

x As Long

y As Long

End Type

Private Type SHFILEOPSTRUCT

hwnd As Long

wFunc As Long

pFrom As String

pTo As String

fFlags As Integer

fAnyOperationsAborted As Long

hNameMappings As Long

lpszProgressTitle As String

End Type

Private Declare Function SHFileOperation Lib "shell32.dll" Alias _

"SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

'剪贴版处理函数

Private Declare Function EmptyClipboard Lib "user32" () 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 GetClipboardData Lib "user32" (ByVal wFormat _

As Long) As Long

Private Declare Function IsClipboardFormatAvailable Lib "user32" _

(ByVal wFormat As Long) As Long

Private Declare Function DragQueryFile Lib "shell32.dll" Alias _

"DragQueryFileA" (ByVal hDrop As Long, ByVal UINT As Long, _

ByVal lpStr As String, ByVal ch As Long) As Long

Private Declare Function DragQueryPoint Lib "shell32.dll" (ByVal _

hDrop As Long, lpPoint As POINTAPI) As Long

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 CopyMem Lib "kernel32" Alias "RtlMoveMemory" _

(Destination As Any, Source As Any, ByVal Length As Long)

'剪贴版数据格式定义

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

' 内存操作定义

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)

Private Const FO_COPY = &H2

Private Type DROPFILES

pFiles As Long

pt As POINTAPI

fNC As Long

fWide As Long

End Type

Public Function clipCopyFiles(Files() As String) As Boolean

Dim data As String

Dim df As DROPFILES

Dim hGlobal As Long

Dim lpGlobal As Long

Dim i As Long

'清除剪贴版中现存的数据

If OpenClipboard(0&) Then

Call EmptyClipboard

For i = LBound(Files) To UBound(Files)

data = data & Files(i) & vbNullChar

Next i

data = data & vbNullChar

'为剪贴版拷贝操作分配相应大小的内存

hGlobal = GlobalAlloc(GHND, Len(df) + Len(data))

If hGlobal Then

lpGlobal = GlobalLock(hGlobal)

df.pFiles = Len(df)

'将DropFiles结构拷贝到内存中

Call CopyMem(ByVal lpGlobal, df, Len(df))

'将文件全路径名拷贝到分配的内存中。

Call CopyMem(ByVal (lpGlobal + Len(df)), ByVal data, _

Len(data))

Call GlobalUnlock(hGlobal)

'将数据拷贝到剪贴版上

If SetClipboardData(CF_HDROP, hGlobal) Then

clipCopyFiles = True

End If

End If

Call CloseClipboard

End If

End Function

Public Function clipPasteFiles(Files() As String) 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 tfStr As SHFILEOPSTRUCT

Const MAX_PATH As Long = 260

'确定剪贴版的数据格式是文件,并打开剪贴版

If IsClipboardFormatAvailable(CF_HDROP) Then

If OpenClipboard(0&) Then

hDrop = GetClipboardData(CF_HDROP)

'获得文件数

nFiles = DragQueryFile(hDrop, -1&, "", 0)

ReDim Files(0 To nFiles - 1) As String

filename = Space(MAX_PATH)

'确定执行的操作类型为拷贝操作

tfStr.wFunc = FO_COPY

'目的路径设置为File1指定的路径

tfStr.pTo = Form1.File1.Path

For i = 0 To nFiles - 1

'根据获取的每一个文件执行文件拷贝操作

Call DragQueryFile(hDrop, i, filename, Len(filename))

Files(i) = TrimNull(filename)

tfStr.pFrom = Files(i)

SHFileOperation tfStr

Next i

Form1.File1.Refresh

Form1.Dir1.Refresh

Call CloseClipboard

End If

clipPasteFiles = nFiles

End If

End Function

Private Function TrimNull(ByVal StrIn As String) As String

Dim nul As Long

nul = InStr(StrIn, vbNullChar)

Select Case nul

Case Is > 1

TrimNull = Left(StrIn, nul - 1)

Case 1

TrimNull = ""

Case 0

TrimNull = Trim(StrIn)

End Select

End Function

2、在Form1中加入一个FileListBox,Name属性设置为File1。加入一个DirListBox,

Name属性设置为Dir1,在Dir1的Change事件中加入如下代码:

Private Sub Dir1_Change()

File1.Path = Dir1.Path

End Sub

加入一个DriveListBox,Name属性设置为Drive1,在Drive1的Change事件中加入如下

代码:

Private Sub Drive1_Change()

Dir1.Path = Drive1.Drive

End Sub

加入一个CommandButton,Name属性设置为cmdCopy,在cmdCopy的Click事件中加入如下

代码:

Private Sub cmdCopy_Click()

Dim Files() As String

Dim Path As String

Dim i As Long, n As Long

Path = Dir1.Path

If Right(Path, 1) <> "\" Then

Path = Path & "\"

End If

'根据在List1上的选择建立拷贝文件的列表

With File1

For i = 0 To .ListCount - 1

If .Selected(i) Then

ReDim Preserve Files(0 To n) As String

Files(n) = Path & .List(i)

n = n + 1

End If

Next i

End With

'拷贝文件到Clipboard

If clipCopyFiles(Files) Then

MsgBox "拷贝文件成功.", , "Success"

Else

MsgBox "无法拷贝文件...", , "Failure"

End If

End Sub

加入一个CommandButton,Name属性设置为cmdPaste,在cmdPaste的Click事件中加入如

下代码:

Private Sub cmdPaste_Click()

Dim Files() As String

Dim nRet As Long

Dim i As Long

Dim msg As String

nRet = clipPasteFiles(Files)

If nRet Then

For i = 0 To nRet - 1

msg = msg & Files(i) & vbCrLf

Next i

MsgBox msg, , "共粘贴" & nRet & "个文件"

Else

MsgBox "从剪贴版粘贴文件错误", , "Failure"

End If

End Sub

运行文件,在Windows 资源管理器中,选择文件,再在资源管理器菜单中选 编辑 | 复制

然后在Form1中点击cmdPaste,从资源管理器中复制的文件就拷贝到Dir1所在的目录中。从

File1中选择文件,按cmdCopy复制,再在资源管理器中选 编辑 | 粘贴 ,选择的文件就被

拷贝到Windows 资源管理器的当前目录下。

上面的程序在Windows98 VB6.0下运行通过。

www.applevb.com

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有