关于SHFileOperation的详细用法(100分)(100分)

  • 主题发起人 主题发起人 luyear
  • 开始时间 开始时间
L

luyear

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是Fyx的帖子!!!!!!!!!!!我还想知道的详细些!!!!!<br>来自:Fyx, 时间:00-11-18 9:11:00, ID:397396 &nbsp;<br>以下代码可以把C盘下的文件夹a复制成文件夹b:<br>uses ..., ShellAPI<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; OpStruc: TSHFileOpStruct;<br>&nbsp; frombuf, tobuf: Array [0..128] of Char;<br>Begin<br>&nbsp; FillChar( frombuf, Sizeof(frombuf), 0 );<br>&nbsp; FillChar( tobuf, Sizeof(tobuf), 0 );<br>&nbsp; StrPCopy( frombuf, 'c:/a' );<br>&nbsp; StrPCopy( tobuf, 'c:/b' );<br>&nbsp; With OpStruc DO Begin<br>&nbsp; &nbsp; Wnd:= Handle;<br>&nbsp; &nbsp; wFunc:= FO_COPY;<br>&nbsp; &nbsp; pFrom:= @frombuf;<br>&nbsp; &nbsp; pTo:=@tobuf;<br>&nbsp; &nbsp; fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;<br>&nbsp; &nbsp; fAnyOperationsAborted:= False;<br>&nbsp; &nbsp; hNameMappings:= Nil;<br>&nbsp; &nbsp; lpszProgressTitle:= Nil;<br>&nbsp; end;<br>&nbsp; ShFileOperation( OpStruc );<br>end;<br>&nbsp;<br>
 
当然是查msdn啦<br>SHFILEOPSTRUCT<br><br><br>typedef struct _SHFILEOPSTRUCT{ <br>&nbsp; &nbsp; HWND &nbsp; &nbsp; &nbsp; &nbsp; hwnd; <br>&nbsp; &nbsp; UINT &nbsp; &nbsp; &nbsp; &nbsp; wFunc; <br>&nbsp; &nbsp; LPCSTR &nbsp; &nbsp; &nbsp; pFrom; <br>&nbsp; &nbsp; LPCSTR &nbsp; &nbsp; &nbsp; pTo; <br>&nbsp; &nbsp; FILEOP_FLAGS fFlags; <br>&nbsp; &nbsp; BOOL &nbsp; &nbsp; &nbsp; &nbsp; fAnyOperationsAborted; <br>&nbsp; &nbsp; LPVOID &nbsp; &nbsp; &nbsp; hNameMappings; <br>&nbsp; &nbsp; LPCSTR &nbsp; &nbsp; &nbsp; lpszProgressTitle; <br>} SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT; <br>&nbsp;<br>Contains information that the SHFileOperation function uses to perform file operations. <br><br>hwnd <br>Window handle to the dialog box to display information about the status of the file operation. <br>wFunc <br>Operation to perform. This member can be one of the following values: FO_COPY &nbsp;Copies the files specified in the pFrom member to the location specified in the pTo member. <br>FO_DELET &nbsp;Deletes the files specified in pFrom. (pTo is ignored.) <br>FO_MOVE &nbsp;Moves the files specified in pFrom to the location specified in pTo. <br>FO_RENAME &nbsp;Renames the files specified in pFrom. &nbsp;<br><br>pFrom <br>Address of a buffer to specify one or more source file names. Multiple names must be null-separated. The list of names must be double null-terminated. <br>pTo <br>Address of a buffer to contain the name of the destination file or directory. The buffer can contain multiple destination file names if the fFlags member specifies FOF_MULTIDESTFILES. Multiple names must be null-separated. The list of names must be double null-terminated. <br>fFlags <br>Flags that control the file operation. This member can be a combination of the following flags: FOF_ALLOWUNDO &nbsp;Preserve Undo information, if possible. If pFrom does not contain fully qualified path and filenames, this flag is ignored. &nbsp;<br>FOF_CONFIRMMOUSE &nbsp;Not currently implemented. &nbsp;<br>FOF_FILESONLY &nbsp;Perform the operation on files only if a wildcard file name (*.*) is specified. &nbsp;<br>FOF_MULTIDESTFILES &nbsp;The pTo member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited. &nbsp;<br>FOF_NOCONFIRMATION &nbsp;Respond with Yes to All for any dialog box that is displayed. &nbsp;<br>FOF_NOCONFIRMMKDIR &nbsp;Does not confirm the creation of a new directory if the operation requires one to be created. &nbsp;<br>FOF_NOCOPYSECURITYATTRIBS &nbsp;Version 4.71. Microsoft&amp;reg; Windows NT&amp;reg; only. The security attributes of the file will not be copied. &nbsp;<br>FOF_NOERRORUI &nbsp;No user interface will be displayed if an error occurs. &nbsp;<br>FOF_RENAMEONCOLLISION &nbsp;Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists. &nbsp;<br>FOF_SILENT &nbsp;Does not display a progress dialog box. &nbsp;<br>FOF_SIMPLEPROGRESS &nbsp;Displays a progress dialog box but does not show the file names. &nbsp;<br>FOF_WANTMAPPINGHANDLE &nbsp;If FOF_RENAMEONCOLLISION is specified, the hNameMappings member will be filled in if any files were renamed. &nbsp;<br><br>fAnyOperationsAborted <br>Value that receives TRUE if the user aborted any file operations before they were completed, or FALSE otherwise. <br>hNameMappings <br>Handle to a file name mapping object that contains an array of SHNAMEMAPPING structures. Each structure contains the old and new path names for each file that was moved, copied, or renamed. This member is used only if the fFlags member includes the FOF_WANTMAPPINGHANDLE flag. The handle must be freed by using the SHFreeNameMappings function. <br>lpszProgressTitle <br>Address of a string to use as the title of a progress dialog box. This member is used only if fFlags includes the FOF_SIMPLEPROGRESS flag. <br>If the pFrom or pTo members are unqualified names, the current directories are taken from the global current drive and directory settings as managed by theGetCurrentDirectory andSetCurrentDirectory functions. <br><br>
 
MSDN说的是VC里面的用法,而且是英文,不好看<br>你的回答顶多可以得到20分
 
sigh,好吧,我试着把他译出来(只能说个大概,但相信够用)<br>ShFileOperation只有一个参数是LPSHFILEOPSTRUCT型的相当于delphi中的TSHFileOpStruct;<br>c语言定义为:<br>typedef struct _SHFILEOPSTRUCT{ <br>&nbsp; &nbsp; HWND &nbsp; &nbsp; &nbsp; &nbsp; hwnd; <br>&nbsp; &nbsp; UINT &nbsp; &nbsp; &nbsp; &nbsp; wFunc; <br>&nbsp; &nbsp; LPCSTR &nbsp; &nbsp; &nbsp; pFrom; <br>&nbsp; &nbsp; LPCSTR &nbsp; &nbsp; &nbsp; pTo; <br>&nbsp; &nbsp; FILEOP_FLAGS fFlags; <br>&nbsp; &nbsp; BOOL &nbsp; &nbsp; &nbsp; &nbsp; fAnyOperationsAborted; <br>&nbsp; &nbsp; LPVOID &nbsp; &nbsp; &nbsp; hNameMappings; <br>&nbsp; &nbsp; LPCSTR &nbsp; &nbsp; &nbsp; lpszProgressTitle; <br>} SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT; <br><br>相应的pascal就是:<br>type <br>&nbsp; &nbsp; _SHFILEOPSTRUCTA = packed record<br>&nbsp; &nbsp; Wnd: HWND;<br>&nbsp; &nbsp; wFunc: UINT;<br>&nbsp; &nbsp; pFrom: PAnsiChar;<br>&nbsp; &nbsp; pTo: PAnsiChar;<br>&nbsp; &nbsp; fFlags: FILEOP_FLAGS;<br>&nbsp; &nbsp; fAnyOperationsAborted: BOOL;<br>&nbsp; &nbsp; hNameMappings: Pointer;<br>&nbsp; &nbsp; lpszProgressTitle: PAnsiChar; { only used if FOF_SIMPLEPROGRESS }<br>&nbsp; end; <br>hwnd:用来显示操作状态的对话框句柄。 例中是form1的句柄<br>wFunc:执行的操作。可以是以下各值:(例中是FO_COPY)<br>&nbsp; &nbsp; &nbsp; &nbsp; FO_COPY:拷贝pfrom域中指定的(目录,例中是'c:/a')到pto中指定的位置(例中为'c:/b') <br>&nbsp; &nbsp; &nbsp; &nbsp; FO_DELET:删除pfrom中指定的文件. &nbsp; (pTo不用) <br>&nbsp; &nbsp; &nbsp; &nbsp; FO_MOVE:移动PFrom中指定的文件到pto中指定的位置。 &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; FO_RENAME:给PFrom中指定的文件改名。<br>pFrom:指定一个或多个源文件名的缓冲区地址。多个名字必须用NULL分隔。名字列表必须用两个NULL(nil,'/0')来结束。<br>pTo:目标文件或目录名缓冲区地址。 如果fFlags域指定FOF_MULTIDESTFILES,缓冲区可以包含多个目标文件名。多个名字必须用NULL分隔。名字列表必须用两个NULL(nil,'/0')<br>fFlags :控制操作的标志,可以是以下各值组合:<br>&nbsp; &nbsp; &nbsp; &nbsp; FOF_ALLOWUNDO:保留Undo信息, 如果pFrom没有包含全的绝对的路径或文件名此值忽略。<br>&nbsp; &nbsp; &nbsp; &nbsp; FOF_CONFIRMMOUSE:没有实现.<br>&nbsp; &nbsp; &nbsp; &nbsp; FOF_FILESONLY:只有文件名使用通配符时(*.*)才对文件操作。<br>&nbsp; &nbsp; &nbsp; &nbsp; FOF_MULTIDESTFILES: &nbsp;pTo域指一定了多个目标文件.(一个对就一个源文件) 而不是指定一个目录来存放所有源文件 &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; FOF_NOCONFIRMATION:所有显示的对话框全部选择yes to all<br>&nbsp; &nbsp; &nbsp; &nbsp; FOF_NOCONFIRMMKDIR: 如果需要创建一个新目录不确认。<br>&nbsp; &nbsp; &nbsp; &nbsp; FOF_NOCOPYSECURITYATTRIBS: &nbsp;4.71. Microsoft&amp;reg; Windows NT&amp;reg; only. 安全属性不复制.<br>&nbsp; &nbsp; &nbsp; &nbsp; FOF_NOERRORUI:发生错误时不提供用户接口。<br>&nbsp; &nbsp; &nbsp; &nbsp; FOF_RENAMEONCOLLISION: &nbsp;move,copy,rename操作时如目标文件存在,给操作的文件另起一个名字。<br>&nbsp; &nbsp; &nbsp; &nbsp; FOF_SILENT:不显示进度对话框<br>&nbsp; &nbsp; &nbsp; &nbsp; FOF_SIMPLEPROGRESS:显示进度对话框但不显示文件名。<br>&nbsp; &nbsp; &nbsp; &nbsp; FOF_WANTMAPPINGHANDLE:如果指定了FOF_RENAMEONCOLLISION 当任何文件改名时将填写hNameMappings 域<br>fAnyOperationsAborted:当用户在完成前取消任何文件操作时赋值TRUE,否则FALSE.<br>hNameMappings:一个包含SHNAMEMAPPING结构数组的文件名映射对象句柄. 每一个(SHNAMEMAPPING)结构包括一个旧的或新的目录名为了每一个移动的复制的改名的文件。这个域仅在fFlags域包括FOF_WANTMAPPINGHANDLES标志时使用。句柄必须使用SHFreeNameMappings来释放(用完后)<br>lpszProgressTitle :进程对话框的标题串地址。仅在fFlags中包括FOF_SIMPLEPROGRESS标志时使用。<br><br>如果pFrom和pTo不是一个绝对目录时,当前目录从全局当前盘符和当前目录中取得,同时目录设置由GetCurrentDirectory 和SetCurrentDirectory 函数维护.<br>
 
回答的好极了!!<br>如果没有更精彩的补充,你至少可以得70分
 
我看楼上写的太详细了,根本不需要补充了。<br>我只是想强调两句,顺便得一点分。 &nbsp;:-)<br>如果要对多个文件进行操作,在PFROM和PTO两项中的文件名列表中,文件名<br>要用#0隔开,最后要用#0#0(双#0)终止,切记。
 
多人接受答案了。
 
后退
顶部