7,765
社区成员
发帖
与我相关
我的任务
分享
' 模块 增加API声明:
Private Declare Function GetSaveFileName Lib "Comdlg32" Alias "GetSaveFileNameW" _
(pOpenfilename As OPENFILENAME) As Long
' 增加接口函数:
Public Function ShowSave(hWndOwner As Long, FileOpen As String, _
Optional Title As String = "保存文件:", _
Optional Filter As String = "所有文件(*.*)|*.*", _
Optional FilterIndex As Long = 0&, _
Optional StartDir As String = vbNullChar, _
Optional Flags As Long = &H806&) As Long
Dim OpenFN As OPENFILENAME
Dim strFilt As String
Dim strName As String
Dim strTemp As String
' 之前的“打开文件”如果要设置默认文件名,这句也这样改一下
strName = FileOpen & vbNullChar & String$(264&, 0)
strTemp = strName
strFilt = Replace(Replace(Filter & vbNullChar, "|", vbNullChar), ",", vbNullChar)
OpenFN.lStructSize = 76&
OpenFN.hWndOwner = hWndOwner
OpenFN.hInstance = App.hInstance
OpenFN.lpstrTitle = StrPtr(Title)
OpenFN.lpstrFilter = StrPtr(strFilt)
OpenFN.nFilterIndex = FilterIndex
OpenFN.lpstrDefExt = 5& + StrPtr(strFilt) + InStrB(1&, Filter, "|")
OpenFN.lpstrInitialDir = StrPtr(StartDir)
OpenFN.lpstrFile = StrPtr(strName)
OpenFN.nMaxFile = 256&
OpenFN.lpstrFileTitle = StrPtr(strTemp)
OpenFN.nMaxFileTitle = 256&
OpenFN.Flags = Flags
If (GetSaveFileName(OpenFN)) Then
FileOpen = Left$(strName, InStr(1&, strName, vbNullChar) - 1&)
ShowSave = 0&
Else
ShowSave = -1&
End If
End Function
调用示例:
Private Sub Command2_Click()
Dim strFilt As String
Dim strFile As String
strFile = "ABCD123"
' ↑ 如果在在“对话框”中设置默认文件名,先把这个变量赋值
strFilt = "文本文件(*.txt)|*.txt,所有文件(*.*)|*.*" & vbNullChar
If (ShowSave(Me.hWnd, strFile, , strFilt)) Then
MsgBox "用户选择了“取消”。", 64
Else
MsgBox "要保存的文件名是:" & strFile, 64
End If
End Sub
' 标准模块中的代码:
Option Explicit
Private Declare Function GetOpenFileName Lib "Comdlg32" Alias "GetOpenFileNameW" _
(pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hWndOwner As Long
hInstance As Long
lpstrFilter As Long
lpstrCustFilter As Long
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As Long
nMaxFile As Long
lpstrFileTitle As Long
nMaxFileTitle As Long
lpstrInitialDir As Long
lpstrTitle As Long
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As Long
End Type
Private Const OFN_HIDEREADONLY As Long = 4& ' 隐藏“只读”选项
Public Function ShowOpen(hWndOwner As Long, FileOpen As String, _
Optional Title As String = "打开文件:", _
Optional Filter As String = "所有文件(*.*)|*.*", _
Optional FilterIndex As Long = 0&, _
Optional StartDir As String = vbNullChar, _
Optional Flags As Long = OFN_HIDEREADONLY) As Long
Dim OpenFN As OPENFILENAME
Dim strFilt As String
Dim strName As String
Dim strTemp As String
strName = String$(264&, 0)
strTemp = strName
strFilt = Replace(Replace(Filter & vbNullChar, "|", vbNullChar), ",", vbNullChar)
OpenFN.lStructSize = 76&
OpenFN.hWndOwner = hWndOwner
OpenFN.hInstance = App.hInstance
OpenFN.lpstrTitle = StrPtr(Title)
OpenFN.lpstrFilter = StrPtr(strFilt)
OpenFN.nFilterIndex = FilterIndex
OpenFN.lpstrDefExt = 5& + StrPtr(strFilt) + InStrB(1&, Filter, "|")
OpenFN.lpstrInitialDir = StrPtr(StartDir)
OpenFN.lpstrFile = StrPtr(strName)
OpenFN.nMaxFile = 256&
OpenFN.lpstrFileTitle = StrPtr(strTemp)
OpenFN.nMaxFileTitle = 256&
OpenFN.Flags = Flags
If (GetOpenFileName(OpenFN)) Then
FileOpen = Left$(strName, InStr(1&, strName, vbNullChar) - 1&)
ShowOpen = 0&
Else
ShowOpen = -1&
End If
End Function
' 窗体代码(调用示例)
Option Explicit
Private Sub Command1_Click()
Dim strFilt As String
Dim strFile As String
strFilt = "文本文件(*.txt)|*.txt,所有文件(*.*)|*.*" & vbNullChar
If (ShowOpen(Me.hWnd, strFile, , strFilt)) Then
MsgBox "用户选择了“取消”。", 64
Else
MsgBox "选择的文件是:" & strFile, 64
End If
End Sub