'打开文件对话框结构
Public 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
Private Const DLG_SHOWOPEN = 1
Private Const DLG_SHOWSAVE = 2
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public Function GetDlgRtnFileName(ByVal iAction As Integer, vOpenFile As OPENFILENAME, ByVal hWndOw As Long, ByVal sFilter As String, _
ByVal sTitle As String, ByVal DefExt As String, ByVal InitFileName As String) As String '用API函数获得打开对话框和保存对话框返回的文件名
Dim lReturn As Long
vOpenFile.lStructSize = Len(vOpenFile)
vOpenFile.hwndOwner = ThisDrawing.HWND32 ' hWndOw 'Form1.hwnd
' vOpenFile.hInstance = app.hInstance
vOpenFile.lpstrFilter = sFilter
vOpenFile.nFilterIndex = 1
If iAction = 2 Then
vOpenFile.lpstrFile = InitFileName & String((257 - Len(InitFileName)), 0)
Else
vOpenFile.lpstrFile = String(257, 0)
End If
vOpenFile.nMaxFile = Len(vOpenFile.lpstrFile) - 1
vOpenFile.lpstrFileTitle = vOpenFile.lpstrFile
vOpenFile.nMaxFileTitle = vOpenFile.nMaxFile
vOpenFile.lpstrInitialDir = ThisDrawing.Path '& "\"
vOpenFile.lpstrTitle = sTitle '"使用对话框API而不是控件" '"Use the Comdlg API not the OCX"
vOpenFile.Flags = 2
vOpenFile.lpstrDefExt = DefExt
Select Case iAction
Case DLG_SHOWOPEN
lReturn = GetOpenFileName(vOpenFile)
Case DLG_SHOWSAVE
lReturn = GetSaveFileName(vOpenFile)
Case Else 'unknown action
Exit Function
End Select
If lReturn = 0 Then
GetDlgRtnFileName = "Cancel" 'MsgBox "用户按下Cancel按钮" '"The User pressed the Cancel Button"
Else
GetDlgRtnFileName = Trim(Left(vOpenFile.lpstrFile, InStr(1, vOpenFile.lpstrFile, Chr$(0), vbBinaryCompare) - 1))
End If
End Function