skyfree 发表于 2008-7-29 12:32:31

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

<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>' 返回: 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>' 返回: 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 编辑 ]

sanhen 发表于 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

skyfree 发表于 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 编辑 ]

jinghai 发表于 2008-7-29 16:17:29

比较好收藏一下.....

[ 本帖最后由 jinghai 于 2008-7-29 16:27 编辑 ]

superflq 发表于 2009-7-16 14:33:58

留个脚印 学习下

liufenglg 发表于 2010-10-10 15:39:34

太深奥.做个记号

xrbenbeba 发表于 2010-10-15 12:08:42

标记一下吧 还是很实用的说

pizigao 发表于 2010-10-15 13:04:08

这个确实很实用的

qq82015930 发表于 2010-12-29 09:34:42

回复 3# skyfree


    怎么实现自动复制替换已存在的文件??

yarsye 发表于 2011-1-10 17:38:02

回复 2# sanhen

看您用shell用的好帅 , 您是怎么学的?
能够在copy的时候加上进度条吗?

zjw201314 发表于 2011-8-8 14:54:04

学习一下!

leibin0121 发表于 2011-8-16 21:30:02

过来学习 学习

872777825 发表于 2011-9-9 04:24:23

回复sanhen

看您用shell用的好帅 , 您是怎么学的?
能够在copy的时候加上进度条吗?
yarsye 发表于 2011-1-10 17:38 http://www.autoitx.com/images/common/back.gif


    复制的时候本来就显示了进度条你没测试过么

airen007 发表于 2012-2-14 09:41:56

老大的帖子怎么也得顶一下

gujifly 发表于 2012-6-28 19:28:18

{:face (125):}这个确实好用,谢谢拉。
页: [1] 2
查看完整版本: AU3复制或移动文件显示进度条的方法