是吗?试试这个例子
'Example Name:Utilizing Windows SHFileOperation API, Advanced '------------------------------------------------------------------------------
'
' BAS Moduel Code
'
'------------------------------------------------------------------------------
Option ExplicitPublic Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Public Const FO_MOVE = &H1
Public Const FO_COPY = &H2
Public Const FO_DELETE = &H3
Public Const FO_RENAME = &H4Public Const FOF_SILENT = &H4
Public Const FOF_RENAMEONCOLLISION = &H8
Public Const FOF_NOCONFIRMATION = &H10
Public Const FOF_SIMPLEPROGRESS = &H100
Public Const FOF_ALLOWUNDO = &H40Public Declare Function GetTempPath Lib "kernel32" _
Alias "GetTempPathA" _
(ByVal nSize As Long, _
ByVal lpBuffer As String) As LongPublic Declare Function SHFileOperation Lib "shell32" _
Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long
'--end block--'
'------------------------------------------------------------------------------
'
' Form Code
'
'------------------------------------------------------------------------------
Option Explicit'FO_FUNC is determined by the type of SHFileOperation action
'chosen (move/delete/rename/copy)
Dim FO_FUNC As Long
'FOF_FLAGS is determined by the both the SHFileOperation Actions/Method
'frame and the SHFileOperation Options frame choices
'(delete/recycle/simple progress/no confirm etc.)
Dim FOF_FLAGS As Long
'for ease of reading, substitute constants for numbers in code
'SHFileOperationAction option button constants
Const FileMove As Integer = 1
Const FileCopy As Integer = 2
Const FileDelete As Integer = 3
'The delete methods
Const Delete2Recycle As Integer = 0
Const Delete4Good As Integer = 1
'Checkbox constants
Const OptSilent As Integer = 0
Const OptNoFilenames As Integer = 1
Const OptNoConfirmDialog As Integer = 2
Const OptRenameIfExists As Integer = 3Private Sub Form_Load() Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
Option2(FileDelete).Value = True
Option1(Delete2Recycle).Value = True
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Command1_Click()
'set some working variables
Dim i As Integer
Dim c As Integer
Dim fNames() As String
Dim fPath As String
Dim r As Long
Dim target As String
'get the current path from the Dir1 control
fPath = Dir1.Path
'load an array with the file name(s) selected
For i = 0 To File1.ListCount - 1
If File1.Selected(i) Then
c = c + 1
ReDim Preserve fNames(1 To c)
fNames(c) = fPath & "\" & File1.List(i)
End If
Next
'if nothing is yet selected, don't go any farther
If c = 0 Then Exit Sub
'If copying or moving to the temp
'folder, get its location
If Option2(1).Value Or _
Option2(2).Value Then target = GetTempDir()
'call ShellDelete
ShellDelete fNames(), target
'refresh the file list
File1.Refresh
End Sub
Private Sub Dir1_Change() File1.Path = Dir1.PathEnd Sub
Private Sub Option2_Click(Index As Integer)
'this was missing from previous posts. Thanks
'to Sylvain Hamel for finding this one!
FO_FUNC = CLng(Index)
'disable the Method frame if the action <> delete
frDeleteMethod.Enabled = Option2(FileDelete).Value = TrueEnd Sub
Private Function BuildFlags() As Long
'Iterate through the options, and build the flag variable
'according to the user selection.
Dim flag As Long
'can only have one of these, so ..
If Option1(Delete2Recycle).Value Then flag = FOF_ALLOWUNDO
'these can be multiple
If Check1(OptSilent).Value Then flag = flag Or FOF_SILENT
If Check1(OptNoFilenames).Value Then flag = flag Or FOF_SIMPLEPROGRESS
If Check1(OptNoConfirmDialog).Value Then flag = flag Or FOF_NOCONFIRMATION
BuildFlags = flagEnd Function
Public Function GetTempDir() As String Dim nSize As Long
Dim tmp As String
tmp = Space$(256)
nSize = Len(tmp)
Call GetTempPath(nSize, tmp)
GetTempDir = TrimNull(tmp)
End Function
Private Function TrimNull(item As String) Dim pos As Integer
'double check that there is a chr$(0) in the string
pos = InStr(item, Chr$(0))
If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If
End Function
Public Sub ShellDelete(sFileArray() As String, sDestination As String)
'Note: sDestination (the pTo member of
'the SHFILEOPSTRUCT) is ignored for deletion.
'
'In addition, a DWORD-alignment problem exists
'in the SHFileOp Type. This means you can not
'use the SHFileOp hNameMaps or sProgress
'members without significant code changes to
'assure DWORD alignment is corrected. See the
'MS KB for information. If you attempt to use
'these members without following the KB and GPF,
'this alignment issue is probably the cause.
'set some working variables
Dim i As Integer
Dim sFiles As String
Dim SHFileOp As SHFILEOPSTRUCT
'create a single string of files from the passed file array,
'each separated by Chr$(0)
For i = LBound(sFileArray) To UBound(sFileArray)
sFiles = sFiles & sFileArray(i) & Chr$(0)
Next 'add a final null to double-null terminate the string
sFiles = sFiles & Chr$(0) 'debug data - print the resulting string
Print sFiles
'determine the user's options selected
FOF_FLAGS = BuildFlags()
'set up the options
With SHFileOp
.wFunc = FO_FUNC
.pFrom = sFiles
.pTo = sDestination
.fFlags = FOF_FLAGS
End With
'and perform the chosen operation
Call SHFileOperation(SHFileOp)End Sub
'Example Name:Utilizing Windows SHFileOperation API, Advanced '------------------------------------------------------------------------------
'
' BAS Moduel Code
'
'------------------------------------------------------------------------------
Option ExplicitPublic Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Public Const FO_MOVE = &H1
Public Const FO_COPY = &H2
Public Const FO_DELETE = &H3
Public Const FO_RENAME = &H4Public Const FOF_SILENT = &H4
Public Const FOF_RENAMEONCOLLISION = &H8
Public Const FOF_NOCONFIRMATION = &H10
Public Const FOF_SIMPLEPROGRESS = &H100
Public Const FOF_ALLOWUNDO = &H40Public Declare Function GetTempPath Lib "kernel32" _
Alias "GetTempPathA" _
(ByVal nSize As Long, _
ByVal lpBuffer As String) As LongPublic Declare Function SHFileOperation Lib "shell32" _
Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long
'--end block--'
'------------------------------------------------------------------------------
'
' Form Code
'
'------------------------------------------------------------------------------
Option Explicit'FO_FUNC is determined by the type of SHFileOperation action
'chosen (move/delete/rename/copy)
Dim FO_FUNC As Long
'FOF_FLAGS is determined by the both the SHFileOperation Actions/Method
'frame and the SHFileOperation Options frame choices
'(delete/recycle/simple progress/no confirm etc.)
Dim FOF_FLAGS As Long
'for ease of reading, substitute constants for numbers in code
'SHFileOperationAction option button constants
Const FileMove As Integer = 1
Const FileCopy As Integer = 2
Const FileDelete As Integer = 3
'The delete methods
Const Delete2Recycle As Integer = 0
Const Delete4Good As Integer = 1
'Checkbox constants
Const OptSilent As Integer = 0
Const OptNoFilenames As Integer = 1
Const OptNoConfirmDialog As Integer = 2
Const OptRenameIfExists As Integer = 3Private Sub Form_Load() Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
Option2(FileDelete).Value = True
Option1(Delete2Recycle).Value = True
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Command1_Click()
'set some working variables
Dim i As Integer
Dim c As Integer
Dim fNames() As String
Dim fPath As String
Dim r As Long
Dim target As String
'get the current path from the Dir1 control
fPath = Dir1.Path
'load an array with the file name(s) selected
For i = 0 To File1.ListCount - 1
If File1.Selected(i) Then
c = c + 1
ReDim Preserve fNames(1 To c)
fNames(c) = fPath & "\" & File1.List(i)
End If
Next
'if nothing is yet selected, don't go any farther
If c = 0 Then Exit Sub
'If copying or moving to the temp
'folder, get its location
If Option2(1).Value Or _
Option2(2).Value Then target = GetTempDir()
'call ShellDelete
ShellDelete fNames(), target
'refresh the file list
File1.Refresh
End Sub
Private Sub Dir1_Change() File1.Path = Dir1.PathEnd Sub
Private Sub Option2_Click(Index As Integer)
'this was missing from previous posts. Thanks
'to Sylvain Hamel for finding this one!
FO_FUNC = CLng(Index)
'disable the Method frame if the action <> delete
frDeleteMethod.Enabled = Option2(FileDelete).Value = TrueEnd Sub
Private Function BuildFlags() As Long
'Iterate through the options, and build the flag variable
'according to the user selection.
Dim flag As Long
'can only have one of these, so ..
If Option1(Delete2Recycle).Value Then flag = FOF_ALLOWUNDO
'these can be multiple
If Check1(OptSilent).Value Then flag = flag Or FOF_SILENT
If Check1(OptNoFilenames).Value Then flag = flag Or FOF_SIMPLEPROGRESS
If Check1(OptNoConfirmDialog).Value Then flag = flag Or FOF_NOCONFIRMATION
BuildFlags = flagEnd Function
Public Function GetTempDir() As String Dim nSize As Long
Dim tmp As String
tmp = Space$(256)
nSize = Len(tmp)
Call GetTempPath(nSize, tmp)
GetTempDir = TrimNull(tmp)
End Function
Private Function TrimNull(item As String) Dim pos As Integer
'double check that there is a chr$(0) in the string
pos = InStr(item, Chr$(0))
If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If
End Function
Public Sub ShellDelete(sFileArray() As String, sDestination As String)
'Note: sDestination (the pTo member of
'the SHFILEOPSTRUCT) is ignored for deletion.
'
'In addition, a DWORD-alignment problem exists
'in the SHFileOp Type. This means you can not
'use the SHFileOp hNameMaps or sProgress
'members without significant code changes to
'assure DWORD alignment is corrected. See the
'MS KB for information. If you attempt to use
'these members without following the KB and GPF,
'this alignment issue is probably the cause.
'set some working variables
Dim i As Integer
Dim sFiles As String
Dim SHFileOp As SHFILEOPSTRUCT
'create a single string of files from the passed file array,
'each separated by Chr$(0)
For i = LBound(sFileArray) To UBound(sFileArray)
sFiles = sFiles & sFileArray(i) & Chr$(0)
Next 'add a final null to double-null terminate the string
sFiles = sFiles & Chr$(0) 'debug data - print the resulting string
Print sFiles
'determine the user's options selected
FOF_FLAGS = BuildFlags()
'set up the options
With SHFileOp
.wFunc = FO_FUNC
.pFrom = sFiles
.pTo = sDestination
.fFlags = FOF_FLAGS
End With
'and perform the chosen operation
Call SHFileOperation(SHFileOp)End Sub
是为什么??