|
可以使用以下过程实现!
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
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
Public Sub getFileName()
Dim OpenFile As OPENFILENAME
Dim sFilter As String
Dim defLoc As String
defLoc = "C:\" 'where to start from
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = hWinLoc 'Me.hwnd - See note at the end of this web page
'OpenFile.hInstance = App.hInstance 'See the note at the end of this web page
sFilter = "Visual Basic Modules (*.bas)" & Chr(0) & "*.bas" & Chr(0) 'start the string
sFilter = sFilter & "C++ program and header files (*.cpp, *.h)" & Chr(0) & "*.cpp;*.h" & Chr(0) 'add another multiple file type entry
sFilter = sFilter & "erl/CGI files (*.cgi, *.pl, *.pm)" & Chr(0) & "*.cgi;*.pl;*.pm" & Chr(0)
sFilter = sFilter & "Text files (*.txt)" & Chr(0) & "*.txt" & Chr(0) 'start the string
sFilter = sFilter & "All files (*.*)" & Chr(0) & "*.*" & Chr(0) 'Add something that allows all files to be seen
sFilter = sFilter & Chr(0) 'terminate the string
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = defLoc
OpenFile.lpstrTitle = myTitle
OpenFile.flags = 0
lreturn = GetOpenFileName(OpenFile)
If lreturn = 0 Then
MsgBox "The User pressed the Cancel Button"
fnSel = ""
Else
MsgBox "The user Chose " & Trim(OpenFile.lpstrFile)
fnSel = Trim(OpenFile.lpstrFile)
End If
If InStr(fnSel, Chr(0)) > 0 Then
fnSel = Left(fnSel, InStr(fnSel, Chr(0)) - 1) 'ditch any terminating chr(0)
End If
Forms!增加新文件!正文 = fnSel
增加新文件.正文 = fnSel
End Sub
|
|