找回密码
 加入
搜索
查看: 20457|回复: 25

AU3复制或移动文件显示进度条的方法

 火... [复制链接]
发表于 2008-7-29 12:32:31 | 显示全部楼层 |阅读模式
<P>Option Explicit </P>
<P>Private Type SHFILEOPSTRUCT <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hWnd As Long <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wFunc As Long <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pFrom As String <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pTo As String <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fFlags As Integer <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fAnyOperationsAborted As Long <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hNameMappings As Long <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lpszProgressTitle As String <BR>End Type </P>
<P>Private Declare Function SHFileOperation Lib "shell32.dll" _ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long </P>
<P>Private Const FOF_ALLOWUNDO = &H40 <BR>Private Const FOF_NOCONFIRMATION = &H10 <BR>Private Const FOF_SIMPLEPROGRESS = &H100 </P>
<P>Private Const FO_COPY = &H2 <BR>Private Const FO_MOVE = &H1 </P>
<P>'-------------------------------------------------------------------------------- <BR>' 过程: ShellFileCopy <BR>' 描述: 复制文件,并显示“正在复制...”进度条对话框 <BR>' 返回: [Boolean] True为复制成功,False为复制失败 <BR>' <BR>' 参数: <BR>'&nbsp;&nbsp;&nbsp;&nbsp; Src (String)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 要复制的源文件 <BR>'&nbsp;&nbsp;&nbsp;&nbsp; Dest (String)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 要复制到的位置 <BR>'&nbsp;&nbsp;&nbsp;&nbsp; hWnd (Long)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 父窗体的句柄(可选) <BR>'&nbsp;&nbsp;&nbsp;&nbsp; NoShowText (Boolean = False)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 是否不显示复制的文件名 <BR>'&nbsp;&nbsp;&nbsp;&nbsp; NoConfirm (Boolean = False)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 是否不显示确认对话框 <BR>' <BR>'-------------------------------------------------------------------------------- <BR>Public Function ShellFileCopy(Src As String, Dest As String, _ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Optional hWnd As Long, _ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Optional NoShowText As Boolean = False, _ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Optional NoConfirm As Boolean = False) As Boolean </P>
<P>&nbsp;&nbsp;&nbsp; Dim SFO As SHFILEOPSTRUCT <BR>&nbsp;&nbsp;&nbsp; Dim lRet As Long <BR>&nbsp;&nbsp;&nbsp; Dim lflags As Long </P>
<P>&nbsp;&nbsp;&nbsp; lflags = FOF_ALLOWUNDO </P>
<P>&nbsp;&nbsp;&nbsp; If NoShowText Then lflags = lflags Or FOF_SIMPLEPROGRESS </P>
<P>&nbsp;&nbsp;&nbsp; If NoConfirm Then lflags = lflags Or FOF_NOCONFIRMATION </P>
<P>&nbsp;&nbsp;&nbsp; With SFO </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .wFunc = FO_COPY <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .pFrom = Src <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .pTo = Dest <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .fFlags = lflags <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; End With </P>
<P>&nbsp;&nbsp;&nbsp; lRet = SHFileOperation(SFO) <BR>&nbsp;&nbsp;&nbsp; ShellFileCopy = (lRet = 0) </P>
<P>End Function </P>
<P>'-------------------------------------------------------------------------------- <BR>' 过程: ShellFileMove <BR>' 描述: 移动文件,并显示“正在移动...”进度条对话框 <BR>' 返回: [Boolean] True为移动成功,False为移动失败 <BR>' <BR>' 参数: <BR>'&nbsp;&nbsp;&nbsp;&nbsp; Src (String)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 要移动的源文件 <BR>'&nbsp;&nbsp;&nbsp;&nbsp; Dest (String)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 要移动到的位置 <BR>'&nbsp;&nbsp;&nbsp;&nbsp; hWnd (Long)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 父窗体的句柄(可选) <BR>'&nbsp;&nbsp;&nbsp;&nbsp; NoShowText (Boolean = False)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 是否不显示移动的文件名 <BR>'&nbsp;&nbsp;&nbsp;&nbsp; NoConfirm (Boolean = False)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 是否不显示确认对话框 <BR>' <BR>'-------------------------------------------------------------------------------- <BR>Public Function ShellFileMove(Src As String, Dest As String, _ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Optional hWnd As Long, _ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Optional NoShowText As Boolean = False, _ <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Optional NoConfirm As Boolean = False) As Boolean </P>
<P>&nbsp;&nbsp;&nbsp; Dim SFO As SHFILEOPSTRUCT <BR>&nbsp;&nbsp;&nbsp; Dim lRet As Long <BR>&nbsp;&nbsp;&nbsp; Dim lflags As Long </P>
<P>&nbsp;&nbsp;&nbsp; lflags = FOF_ALLOWUNDO </P>
<P>&nbsp;&nbsp;&nbsp; If NoShowText Then lflags = lflags Or FOF_SIMPLEPROGRESS </P>
<P>&nbsp;&nbsp;&nbsp; If NoConfirm Then lflags = lflags Or FOF_NOCONFIRMATION </P>
<P>&nbsp;&nbsp;&nbsp; With SFO </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .wFunc = FO_MOVE <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .pFrom = Src <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .pTo = Dest <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .fFlags = lflags <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; End With </P>
<P>&nbsp;&nbsp;&nbsp; lRet = SHFileOperation(SFO) <BR>&nbsp;&nbsp;&nbsp; ShellFileMove = (lRet = 0) </P>
<P>End Function <BR></P>
<P>&nbsp;</P>


经朋友介绍,日前获得了一段VB的源代码,用于复制或移动文件显示进度条,哪位高人可以把这个改成AU3的不,感激不尽!!

[ 本帖最后由 skyfree 于 2008-7-29 15:14 编辑 ]
发表于 2008-7-29 14:01:59 | 显示全部楼层
API的话。工程实太大了。不过API有返回值判断,就是非常麻烦。将就一下,用SHELL对象来实现,简单化^_^




;_DirCopy('d:\sf','d:\aaa')
;_DirMove("d:\abc","e:\ooq")

Func _DirCopy($SourceDir,$Destdir) 
;说明:利用Shell对象来实现复制文件对话框
;作者:Sanhen
    Local $Shell
    Local $FOF_SIMPLEPROGRESS = 16
    If Not FileExists($Destdir) Then DirCreate($Destdir)
    $Shell = ObjCreate("shell.application")
    $Shell.namespace($Destdir).CopyHere($SourceDir,$FOF_SIMPLEPROGRESS)
EndFunc


Func _DirMove($SourceDir,$Destdir)
;说明:利用Shell对象来实现移动文件对话框
;作者:Sanhen
     Local $Shell
     Local $FOF_CREATEPROGRESSDLG = 16
     If Not FileExists($Destdir) Then DirCreate($Destdir)
     $Shell = ObjCreate("Shell.Application")
 $Shell.NameSpace($Destdir).MoveHere($SourceDir, $FOF_CREATEPROGRESSDLG)
EndFunc

 楼主| 发表于 2008-7-29 15:02:08 | 显示全部楼层
还是sanhen厉害!方法不错,简单实用。

我这里有我论坛7猫给我的一段用于复制文件显示进度条的AU3代码,共同交流吧

Global Const $FO_COPY = 0x0002
Global Const $FOF_ALLOWUNDO = 0x0040

_ExplorerCopy("f:\XPBAK.gho", "D:\")

Func _ExplorerCopy($source, $dest)

    Local $SHFILEOPSTRUCT, $source_struct, $dest_struct

    $SHFILEOPSTRUCT = DllStructCreate("hwnd hWnd;uint wFunc;ptr pFrom;ptr pTo;int fFlags;" & _
                                      "int fAnyOperationsAborted;ptr hNameMappings;ptr lpszProgressTitle")

    $source_struct = DllStructCreate("char[" & StringLen($source) + 2 & "]")
    DllStructSetData($source_struct, 1, $source)
    DllStructSetData($source_struct, 1, 0, StringLen($source) + 2)

    $dest_struct = DllStructCreate("char[" & StringLen($dest) + 2 & "]")
    DllStructSetData($dest_struct, 1, $dest)
    DllStructSetData($dest_struct, 1, 0, StringLen($dest) + 2)

    DllStructSetData($SHFILEOPSTRUCT, "hWnd", 0)
    DllStructSetData($SHFILEOPSTRUCT, "wFunc", $FO_COPY)
    DllStructSetData($SHFILEOPSTRUCT, "pFrom", DllStructGetPtr($source_struct))
    DllStructSetData($SHFILEOPSTRUCT, "pTo", DllStructGetPtr($dest_struct))
    DllStructSetData($SHFILEOPSTRUCT, "fFlags", $FOF_ALLOWUNDO)

    DllCall("shell32.dll", "int", "SHFileOperation", "ptr", DllStructGetPtr($SHFILEOPSTRUCT))
EndFunc   ;==>_ExplorerCopy


[ 本帖最后由 skyfree 于 2008-7-29 15:03 编辑 ]
发表于 2008-7-29 16:17:29 | 显示全部楼层
比较好收藏一下.....

[ 本帖最后由 jinghai 于 2008-7-29 16:27 编辑 ]
发表于 2009-7-16 14:33:58 | 显示全部楼层
留个脚印 学习下
发表于 2010-10-10 15:39:34 | 显示全部楼层
太深奥.做个记号
发表于 2010-10-15 12:08:42 | 显示全部楼层
标记一下吧 还是很实用的说
发表于 2010-10-15 13:04:08 | 显示全部楼层
这个确实很实用的
发表于 2010-12-29 09:34:42 | 显示全部楼层
回复 3# skyfree


    怎么实现自动复制替换已存在的文件??
发表于 2011-1-10 17:38:02 | 显示全部楼层
回复 2# sanhen

看您用shell用的好帅 , 您是怎么学的?
能够在copy的时候加上进度条吗?
发表于 2011-8-8 14:54:04 | 显示全部楼层
学习一下!
发表于 2011-8-16 21:30:02 | 显示全部楼层
过来学习 学习
发表于 2011-9-9 04:24:23 | 显示全部楼层
回复  sanhen

看您用shell用的好帅 , 您是怎么学的?
能够在copy的时候加上进度条吗?
yarsye 发表于 2011-1-10 17:38



    复制的时候本来就显示了进度条  你没测试过么
发表于 2012-2-14 09:41:56 | 显示全部楼层
老大的帖子怎么也得顶一下
发表于 2012-6-28 19:28:18 | 显示全部楼层
这个确实好用,谢谢拉。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-6 05:04 , Processed in 0.077984 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表