说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

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

HTML文档下载 WORD文档下载 PDF文档下载
VB与Windows资源管理器互拷文件
作者:长沙 陈锐


   通过VB编程来拷贝或移动文件的原理可能大家都十分清楚,可以利用Windows APISHFileOperation来进行操作,也可以利用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下运行通过。


高性能、高流量Java Web站点打造的22条建议 云巅:第三代虚拟技术+搜索引擎+乐高理念打造分布式桌面云 Intel:从“芯”构建下一代数据中心 WhatsApp:50名员工,月4亿活跃用户,日160亿条消息和5亿张图片 三星Galaxy Gear智能手表失利 需改进的8个方面 IE11全新的F12开发人员工具详解 研发周报:为什么开发者担心将代码公布 Hadoop YARN的发展史与详细解析 一周消息树:Firefox OS会不会成为下一个Android PhoneGap 3.3.0发布,支持Android 4.4 KitKat 移动周报:两岸三地在线编程学习网站大搜罗 有道周枫和他的“放养”团队 看IE11如何助力AAF推动公益事业 SUSE总裁Nils:将SUSE Cloud 2.0打造成“零影响的云安装程序” Windows 8之父Steven Sinofsky:2014年科技界的十大趋势 电视盒子还没智能化?看Fan TV们怎么玩 畅游启动“必赢计划” 十亿巨资代理优质产品 数据控使用Hadoop的三种最常用方式 值得创始人学习的五大企业文化管理方式 圣诞节快乐:来自程序员们的问候 宏碁任命新CEO Jason Chen:曾任职英特尔14年、台积电8年 史上第一次越狱发布会与Evad3rs太极助手罗生门 新东方在线CTO曾明:已用Cocos2d-html5开发出150余款产品 Facebook 2013年的9个开源项目 开源力量公开课2013年度庆典:我们的开源项目 看雪学院“走进企业看安全”活动走进安全管家 视频+PPT:苹果iOS7 Tech Talks大会演讲内容 iPhone 6和Galaxy S5暂不会采用弯曲屏技术 当程序不工作时,开发者常用的借口 携程技术副总裁叶亚明:三次重大技术改进的故事及背后原因 Google抛弃C语言,采用Go语言重写Go编译器 在MDI框架中,如何禁止View相互切换? 代码转换 谁能告诉我delphi6.0中的installshield怎样使 为什么我用Zend Encoder加密了一个php文件,速度比以前没加密慢了好多哦, 怎样让在运行时加载的控件总在最前面?? 代码转换 linux + mysql+ apache + php + jdk + jsdk 怎样让在运行时加载的控件总在最前面?? 代码转换 一个关于全局变量的访问问题。 263游戏大厅里的用户信息列表是怎么实现的?急问,多谢!! 也帮帮我吧!i'll be dead ! 263游戏大厅里的用户信息列表是怎么实现的?急问,多谢!! 关于窗体 如何去掉我的“计划任务”共享? linux + mysql + apache + php + jdk + jsdk 新手问各位大哥一个问题:我在机器上已经安装了98,2000,现在有装了红帽7.2,已经安装成功,但是在LOGIN之后,有开始GNOME之后,什么都 菜鸟问题,请多帮忙 window 2000 server服务器,不能使用mail函数,怎么解决!?急!! 我靠!我今天算碰到一个希奇事!你们听听这个网址! 关于鼠标右键菜单的制作问题 SOS!100分相送。很简单的题目,明天考试急用 请问oracle中的帮助导航器、帮助主题窗口是在哪里啊 重要问题!!!请尽快回复。 证券之星最新版本有没有破解版啊,我在证券之星下载的多是只能用三天 各位老大,有谁知道怎样边接收网络传送的音频流边播放 System.in的问题 为什么listbox.selectIndex返回的值总是-1??? masm611装好后不能DEBUG,怎么回事啊???????? 挥泪放出两百分。为了实行打击报负不得不放点血了。来者有分。大家要涌跃发言。快快回答,正确就结贴。 dll的问题和语言混编 今天买了双新皮鞋,感觉不错。散分。 《微型计算机技术》问题 放分!!!:使用C++多年的老手,一定要看过来,看看我们对C有多了解 怎么让listbox有响应onDbClick事件呀! 如何恢复IE的“自动完成”功能。即让IE弹出那个“自动完成”的提示框询问用户是否保存输入表单中的内容 。 怎样扩大、缩小不规则区域界面? 高手,怎样将数据库表中为BIT的字段,读出来后,显示为yes或者no,而不是默认的true or false 我得分的帖子里显示得分还是0,为什么?能不能纠正!! 在Servlet引用了JDK中没有的包应该怎么设置! 问问一个很简单的问题。大家看看能否帮我澄清这个概念 求职 我想做个类似qq的东西,但是用户登陆后的消息怎么做?? 关于数据库 请教一个简单的阶乘递归的问题 请small_wei前辈领分.[1] 紧急求救!!!! 我还没毕业,却要做一家软件公司的经理,真有点不知所措,望大家踊跃发言,各点建议 简单问题,分数不低:如何得到splitter的rect? up也给分 请教一个win2k的系统复制问题!很头疼!! 寻JavaScript电子版好书,不好的就不要UP了, When I have time,I'll come to the school to see how you ___.A.study B.have studied C.studied D.are studying (最好讲解一下) preferred +什么? she said,"I didn't tell anyone about this."改成间接引语 i 've been waiting for you for an hour .为什么这个 have been 后面 带的是ing 布兰妮的歌if you seek Amy 中文是什么呢? would you please ( )me something about this community?这里加to tell还是tell为什么 这里to用在哪 125.---I’ve been waiting for you for an hour!Where have you been?---__________ It’s rush hour now,you know,the traffic is too bad!A.It doesn’t matter.B.Sorry about that.C.Be careful!D.What a pity! 布兰妮的If u Seek Amy怎么回事?好像要进行处理才能在电台播放,为什么?这首歌有什么问题 I bought my shoes this year ;my sister bought hers last year.(改为同义句)My shoes are ____________my sister.We arrived early so that we had time to waarm up .(改为同义句)We arrived early _____ _____ _____ have time to warm up We have been waiting since 8.00(改为否定句) she has been reading for an hour(对for an hour提问)He has been running since i arrived(对since I arrived提问)谢谢了这些句型转换 Where does cathyi is parents went to 把“the students of class 5 are in the classroom" 中的in the classroom"就提问 -Why did Miss Wang look so worried when we saw her?-Because she wondered_.A.where did the other students go B.when would the policeman come C.what her students did during the tripD.if her students leave the classroom we didn'r expect them to ______-for us .we didn'r expect them to ______-for when we arrive there so late.A.to be waiting B.to have been waiting C.to have waied D.to wait.为什么不选B.我知道A对.那B 错在哪儿? 英语中像with这样可以用在开头的介词还有哪些? she has been wait for an hour she has waited for an hour 二者的具体区别二者都属于现在完成时吗? We will invite them ____the party A.in B.for C.at D.toWhat do you ____at the meeting?A.tell B.say C.talk D.speak When is the best time to go to Sanya? 求助:改写句子 Sally will also be busy sending her mother postcards because she has...原句:Sally will also be busy sending her mother postcards because she has promised to write to her every week.改写: Sally will _______ _______ her mot When did Mary lose her handbag? — It was at Christmas ___she lived inlondonwith her mother.Awhere B when C that D which答案是B 为什么不选C lie to me翻译成中文是啥意思? where does Mrs Zhao take me to? 英语翻译Once upon a time,there lived a washer man in a village.He possessed a thin donkey by the name of Udhata.The donkey had to carry loads of clothes to the river bank by the day and he was not satisfied with the food provided by the washer ma lie to me 怎么翻译最合适 they are worried about the welfare of the cloned child if there is one意思是什么? does mrs cooper know where it is?的中文意思急 用四个然而造句,在一个句子里,会的就说, He is worried about him .about him 做什么成分 16.If you couldn’t find the time to attend the meeting,you should have ( ) the invitation early.16.If you couldn’t find the time to attend the meeting,you should have ( ) the invitation early.(本题分数:2 分.) A、 Decreased B、 Reduced C click here to meet them He is worried about if she is OK 改错,错那里了?有四处可以改:A is B worried C about if D OK On mary's ( ) birthday ,she was given a new computer by her parentsHOW ( ) she was A nine lucky B nineteenth lucky 选什么?说理由 how many time 与how soon 区别?__will you need to work out the problem?该填什么?为什么?请详细说明可选择项是how long ,how much time ,how soon ,和how fast,改选那个???? go through with get through with Lily________her parents in Shaihai when she was young.RT快点哦 Which word has thousands of letters in them?a.book b.picture c.dictionary d.TV get married with 英语翻译“to be"中的”be"可以去掉吗?去掉后有什么区别. what word has thousands of letter in them乐灵的每日一练求答案 I wanna knock you down and kick your ass我不知道怎么读.用拼音或者标准的汉字读音,最好来个英语读音的. 用下面四个句子造句 求切尔诺贝利的英文翻译 got sb.with sb. 麻烦大家来帮我造一个有文采的句子呗!按照题造句,仿照,“愿你的生命中有够多的云翳,来造成一个美丽的黄昏”,用相同的句式和修辞手法另写一个句子, 收集一下英语中过度的短语,比如on the other hand,however之类的要英文和中文解释.为写作文而积累 knock your punk ass down啥意思?这是will smith 说的粗口 圣诞节又来了 CHRISTMAS IS HERE AGAIN怎么样 福岛会成为第二个切尔诺贝利吗? About Ross' 'hunt him down and kick his ass'?What does that mean anyway?after Ross told Chandler,he laughs out rolling on the floor.After that,in Monica and Chandler's rehearsal dinner,Rachel and Phoebe laugh their asses off too.What's so funny about They are waiting for the car(the car划线)划线提问 tell me something about this slang "give a pulse"as above 在这篇文章中“What will Judy be in the play this time?”翻在这篇文章中“What will Judy be in the play this time?”翻译, Tom ang John are waiting for me at the library对划线部分提问 别着急,我们还剩下许多时间.(汉译英) ____ _____ ____,we have much time ____. when is a good time to have the party,这是什么语法,应该怎么解释 more,less,fewer,much,后面分别跟什么? Now I'd like to tell you something about my part timejob this summer vocation改错 what will happen in the world in 500 years' time?这句话怎么翻译啊? preferred to do you know how much time and effdo you know how much time and effort they devoted —— such a complicate work?A to complete B to completing 选哪个
备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘