
需求:一個Filelistbox、Dirlistbox、drivelistbox元件各一個與兩個Command按鍵
主表單程式碼
Option Explicit '強制變數宣告
'將資料搬到資源回收桶(刪除檔案)
Private Sub Command1_Click()
Dim SHFileOp As SHFILEOPSTRUCT '宣告資料結構參數
Dim Path As String, pFrom As String, i As Integer
Path = File1.Path 'File1為Filelistbox元件
'判斷是否位於根目錄
If Right(Path, 1) <> "\" Then Path = Path & "\"
'選取全部已點選的檔案
For i = 0 To File1.ListCount - 1
If File1.Selected(i) Then
pFrom = pFrom & Path & File1.List(i) & Chr(0)
End If
Next
SHFileOp.wFunc = FO_DELETE
SHFileOp.pFrom = pFrom
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION
SHFileOperation SHFileOp
File1.Refresh
End Sub
'-------------------------------------------------------------------------------------------------
'複製檔案至指定的路徑
Private Sub Command2_Click()
Dim SHFileOp As SHFILEOPSTRUCT '宣告資料結構參數
Dim Path As String, pFrom As String, i As Integer
Path = File1.Path
If Right(Path, 1) <> "\" Then Path = Path & "\"
For i = 0 To File1.ListCount - 1
If File1.Selected(i) Then
pFrom = pFrom & Path & File1.List(i) & Chr(0)
End If
Next
SHFileOp.wFunc = FO_COPY
SHFileOp.pFrom = pFrom
SHFileOp.pTo = "c:\test\"
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR
SHFileOperation SHFileOp
File1.Refresh
End Sub
'-------------------------------------------------------------------------------------------------
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
'-------------------------------------------------------------------------------------------------
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
模組程式碼
Option Explicit
'以下為檔案系統Api宣告
Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
'以下為Api [SHFILEOPSTRUCT]變數之資料結構參數宣告
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
'-------------------------------------------------------------------------------------------------
'以下為Api [SHFILEOPSTRUCT]變數之 [wFunc] 參數變數宣告
Public Const FO_MOVE = &H1 '搬移
Public Const FO_COPY = &H2 '複製
Public Const FO_DELETE = &H3 '刪除
Public Const FO_RENAME = &H4 '更名
'-------------------------------------------------------------------------------------------------
'以下為Api [SHFILEOPSTRUCT]變數之 [wFlags] 參數變數宣告
Public Const FOF_NOCONFIRMATION = &H10 '遇全部刪除不需徵求同意
Public Const FOF_NOCONFIRMMKDIR = &H200 '建立資料夾時不需徵求同意
Public Const FOF_FILESONLT = &H80 '只可操作檔案不可操作目錄
Public Const FOF_ALLOWUNDO = &H40 '將刪除的檔丟到資源回收桶
|