|
发表于 2008-12-11 13:20:37
|
显示全部楼层
糟糕的事实是,无论你使用Shell还是调用文件打开对话框,它都是浏览器
但是,无论如何,最快捷、最肮脏的方式是“使用”;ChDir;。我不知道您是如何生成对话框的,但有时在这些情况下设置当前的windows目录是可行的
或者,如果你想把它做好,把下面的代码放到它自己的(.bas)模块中
它包含;“文件打开”;和“;“保存文件”;对话框。它实际上并没有为您打开或保存文件,但它为用户提供了一个界面,并返回所选文件(如果用户取消,则返回一个空字符串)
我还添加了;SetCurrentDirectory;函数,该函数为对话框的启动窗口设定种子。此API版本比;ChDir“;VB的函数,因为它也适用于网络驱动器等。一些示例调用:
- Sub main()
- Dim retFile As String
-
- ' set directory you want dialog to start in as the \windows directory
- SetCurrentDirectory "c:\windows"
-
-
- ' call File Open Dialog
- FileOpen Application.hWnd, MyFile$, "*.dwg", "Acad Drawings", "Select a Drawing"
- MsgBox "The File selected to open was " & MyFile$
-
- ' set directory you want dialog to start in as the root dir
- SetCurrentDirectory "c:"
-
- ' call file save dialog
- SaveFile Application.hWnd, MyFile$, "*.txt", "Text Files", "Save a text file"
- MsgBox "The File selected to save was " & MyFile$
- End Sub
&039;以及附带的代码模块:
-
- Option Explicit
- Public Const OFN_ALLOWMULTISELECT = &H200
- Public Const OFN_CREATEPROMPT = &H2000
- Public Const OFN_ENABLEHOOK = &H20
- Public Const OFN_ENABLETEMPLATE = &H40
- Public Const OFN_ENABLETEMPLATEHANDLE = &H80
- Public Const OFN_EXPLORER = &H80000
- Public Const OFN_EXTENSIONDIFFERENT = &H400
- Public Const OFN_FILEMUSTEXIST = &H1000
- Public Const OFN_HIDEREADONLY = &H4
- Public Const OFN_LONGNAMES = &H200000
- Public Const OFN_NOCHANGEDIR = &H8
- Public Const OFN_NODEREFERENCELINKS = &H100000
- Public Const OFN_NOLONGNAMES = &H40000
- Public Const OFN_NONETWORKBUTTON = &H20000
- Public Const OFN_NOREADONLYRETURN = &H8000
- Public Const OFN_NOTESTFILECREATE = &H10000
- Public Const OFN_NOVALIDATE = &H100
- Public Const OFN_OVERWRITEPROMPT = &H2
- Public Const OFN_PATHMUSTEXIST = &H800
- Public Const OFN_READONLY = &H1
- Public Const OFN_SHAREAWARE = &H4000
- Public Const OFN_SHAREFALLTHROUGH = 2
- Public Const OFN_SHAREWARN = 0
- Public Const OFN_SHARENOWARN = 1
- Public Const OFN_SHOWHELP = &H10
- Public Const OFS_MAXPATHNAME = 128
-
- Public Const OFS_FILE_OPEN_FLAGS = OFN_EXPLORER Or OFN_LONGNAMES Or OFN_CREATEPROMPT Or OFN_NODEREFERENCELINKS
- Public Const OFS_FILE_SAVE_FLAGS = OFN_EXPLORER Or OFN_LONGNAMES Or OFN_OVERWRITEPROMPT Or OFN_HIDEREADONLY
- Public Const OFS_MULTIFILE_OPEN_FLAGS = OFN_ALLOWMULTISELECT Or OFN_EXPLORER Or OFN_LONGNAMES Or OFN_CREATEPROMPT Or OFN_NODEREFERENCELINKS
- Public Type OPENFILENAME
- nStructSize As Long
- hwndOwner As Long
- hInstance As Long
- sFilter As String
- sCustomFilter As String
- nCustFilterSize As Long
- nFilterIndex As Long
- sFile As String
- nFileSize As Long
- sFileTitle As String
- nTitleSize As Long
- sInitDir As String
- sDlgTitle As String
- flags As Long
- nFileOffset As Integer
- nFileExt As Integer
- sDefFileExt As String
- nCustDataSize As Long
- fnHook As Long
- sTemplateName As String
- End Type
-
- Public Llama As OPENFILENAME
- Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
- Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
- Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
- Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
- Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long
- ' +--------------------------------------------------------------------+
- ' | -= Main sub to call File SAVE Dialog =- |
- ' | |
- ' | Parameters: FileName$ is a variable that the name of the SAVED |
- ' | file name is returned in. You do NOT have to pass |
- ' | a filename to this routine, one is returned. Note |
- ' | that the Win API checks for, and prompts, if the |
- ' | filename already exists. |
- ' | |
- ' | FileExt$ is the file extension name you wish the |
- ' | Dialog box to use, for default extension, file |
- ' | listings, and availablity innthe drop-down "file |
- ' | type" box. |
- ' | |
- ' | FileDesc$ is a descriptive name for the File Name |
- ' | Extension, used to describe the filetype in the drop |
- ' | down type box. |
- ' | |
- ' | DlgTitle$ is the name of the caption on the Dialog |
- ' | |
- ' | |
- ' +--------------------------------------------------------------------+
- Public Sub SaveFile(hWnd As Long, FileName$, FileExt$, FileDesc$, DlgTitle$)
- Dim lngGo As Long
- Dim lngHwnd As Long
- Dim strCurName As String
- Dim strNewName As String
- On Error GoTo Err_Control
- strCurName = FileName$
- lngHwnd = hWnd
- FileName$ = vbdShowSave(lngHwnd, strCurName, FileExt$, FileDesc$, DlgTitle$)
- Exit Sub
- Err_Control:
- 'Just get out, to many things to account for
- MsgBox Err.Description, vbCritical, "Too many errors, aborting"
- End Sub
- ' +--------------------------------------------------------------------+
- ' | -= Main sub to call File OPEN Dialog =- |
- ' | |
- ' | Parameters: FileName$ is a variable that the name of the SAVED |
- ' | file name is returned in. You do NOT have to pass |
- ' | a filename to this routine, one is returned. |
- ' | |
- ' | FileExt$ is the file extension name you wish the |
- ' | Dialog box to use, for default extension, file |
- ' | listings, and availablity innthe drop-down "file |
- ' | type" box. |
- ' | |
- ' | FileDesc$ is a descriptive name for the File Name |
- ' | Extension, used to describe the filetype in the drop |
- ' | down type box. |
- ' | |
- ' | |
- ' | DlgTitle$ is the name of the caption on the Dialog |
- ' | |
|
|