Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
'=======================================
'打开一个文件
'=======================================
Function GetFile(ByVal Hwnd As Long, Optional ByVal dlgFilter As String = "所有文件(*.*)|*.*", _
Optional ByVal dlgTitle As String = "打开文件", Optional ByVal DefaultDir) As String
Dim ofn As OPENFILENAME
Dim Rtn As String
Dim fn(0) As String
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = Hwnd
ofn.hInstance = App.hInstance
ofn.lpstrFile = String(254, Chr(0))
ofn.nMaxFile = 255
ofn.lpstrFileTitle = String(254, Chr(0))
ofn.nMaxFileTitle = 255
ofn.lpstrFilter = ChangeStr(dlgFilter) & Chr(0)
ofn.lpstrTitle = dlgTitle
If IsMissing(DefaultDir) Then
ofn.lpstrInitialDir = App.Path
Else
ofn.lpstrInitialDir = DefaultDir
End If
ofn.flags = &H80000 'Or &H20
Rtn = GetOpenFileName(ofn)
If Rtn >= 1 Then
GetFile = Left(ofn.lpstrFile, InStr(1, ofn.lpstrFile, Chr(0)) - 1)
Else
GetFile = ""
End If
End Function
Function GetFiles(ByVal Hwnd As Long, Optional ByVal dlgFilter As String = "所有文件(*.*)|*.*", _
Optional ByVal dlgTitle As String = "打开文件", Optional ByVal DefaultDir) As String()
Dim ofn As OPENFILENAME
Dim Rtn As Long
Dim fn(0) As String
ofn.lStructSize = Len(ofn)
ofn.hwndOwner = Hwnd
ofn.hInstance = App.hInstance
ofn.lpstrFile = String(2000, Chr(0))
ofn.nMaxFile = 2000
ofn.lpstrFileTitle = String(254, Chr(0))
ofn.nMaxFileTitle = 255
ofn.lpstrFilter = ChangeStr(dlgFilter) & Chr(0)
ofn.lpstrTitle = dlgTitle
If IsMissing(DefaultDir) Then
ofn.lpstrInitialDir = App.Path
Else
ofn.lpstrInitialDir = DefaultDir
End If
ofn.flags = &H80000 Or &H200 'Or &H20
Rtn = GetOpenFileName(ofn)
If Rtn >= 1 Then
GetFiles = GetFileNames(ofn.lpstrFile)
Else
GetFiles = fn
End If
End Function
'=======================================
'保存文件
'=======================================
Function SaveFile(ByVal Hwnd As Long, ByVal dlgFilterDescribe As String, ByVal dlgFilter As String, _
Optional ByVal dlgTitle As String = "保存文件", Optional ByVal dlgIniDir) As String
Dim OFName As OPENFILENAME
With OFName
'设置结构的大小
.lStructSize = Len(OFName)
'设置父窗口
.hwndOwner = Hwnd
'设置程序的实例
.hInstance = App.hInstance
'设置过滤属性
.lpstrFilter = dlgFilterDescribe & "(*" & dlgFilter & ")" & Chr(0) & "*" & dlgFilter & Chr(0)
'设置默认扩展名
.lpstrFile = String(254, Chr(0))
'设置返回的文件(全路径)的最大长度
.nMaxFile = 255
'为文件名称创建缓冲区
.lpstrFileTitle = String(254, Chr(0))
'设置返回的文件名称的最大长度
.nMaxFileTitle = 255
'设置初始目录
If IsMissing(sIniDir) Then
.lpstrInitialDir = App.Path
Else
.lpstrInitialDir = sIniDir
End If
'设置对话框标题
.lpstrTitle = sTitle
.flags = &H80000 'Or &H20
End With
If GetSaveFileName(OFName) Then
SaveFile = Left(OFName.lpstrFile, InStr(1, OFName.lpstrFile, Chr(0)) - 1) & Trim(dlgFilter)
Else
SaveFile = ""
End If
End Function
Private Function ChangeStr(ByVal strIn As String) As String
ChangeStr = Replace(strIn, "|", Chr(0))
End Function
Private Function GetFileNames(ByVal strIn As String) As String()
Dim ln As Long, num As Integer, strPath As String, strName As String
Dim pos() As Long, fn() As String
ReDim pos(0)
pos(0) = 1
strIn = Trim(strIn)
ln = Len(strIn)
For i = 1 To ln
If Asc(Mid(strIn, i)) = 0 Then
num = num + 1
ReDim Preserve pos(num)
pos(num) = i
If Asc(Mid(strIn, i + 1)) = 0 Then Exit For
End If
Next
If num = 1 Then
ReDim fn(0)
fn(0) = Left(strIn, pos(1))
Else
num = num - 2
ReDim fn(num)
strPath = Left(strIn, pos(1) - 1)
For i = 0 To num
fn(i) = strPath & "\" & Mid(strIn, pos(i + 1) + 1, pos(i + 2) - pos(i + 1))
Next
End If
GetFileNames = fn
End Function