Matt__W 发表于 2008-12-10 03:05:22

在当前文件夹中打开对话框窗口

嗨,我需要帮助。我有两个单选按钮,当我在指定的列表中按下其中一个按钮时,它会显示当前文件夹中的所有dwg。本人'我把它放在一个浏览按钮上,这样我可以打开一个窗口并更改当前文件夹,但我没有'我不知道如何更新单选按钮和打开对话框窗口的路径,使其位于同一文件夹中。有人能帮我吗
谢谢你


Keith™ 发表于 2008-12-10 08:22:50

听起来,文件夹路径需要一个全局变量 当您从一个按钮更改路径时,它将自动为另一个按钮更新。

Keith™ 发表于 2008-12-10 08:35:11

这取决于你如何打开浏览窗口。一些关于如何处理的代码将非常有用。要更新单选按钮,你所需要做的就是更改按钮的标题属性
你需要浏览按钮吗?如果没有,您可以简单地使用dir函数返回位于特定路径中的所有DWG文件,例如:
PlotFile = Dir(Folder & "\*.dwg")
While PlotFile""
ListBox.AddItem PlotFile
PlotFile = Dir
Wend

Keith™ 发表于 2008-12-11 02:01:30


1。我有全局变量,但我没有'我不知道如何通过打开对话框窗口。为了打开对话框窗口,我使用了这个命令:ThisDrawing。SendCommand(“打开”vbCr)。这个命令是打开一个窗口,就像你想从菜单中打开的一样:文件-打开…
2。刷新单选按钮的部分完成了,也完成了它的列表'好的。我需要浏览按钮,这样我可以打开一个对话框窗口来更改文件夹,并添加或选择新的图形。现在对我来说,唯一的问题是打开当前文件夹中的对话框窗口。本人'我也试过这样做:
Sub open_project_window()
Dim RetVal
Dim dwg_path As String
Dim PathTokens As Variant
Dim ProjectPath As String
dwg_path = ThisDrawing.Path
PathTokens = Split(dwg_path, "\")
ProjectPath = PathTokens(0) & "\" & PathTokens(1) & "\" & PathTokens(2) & "\" & PathTokens(3)
RetVal = Shell("C:\Windows\Explorer.exe " & ProjectPath, 1) - I don't want to open an explorer window
End Sub 此代码为'没关系,但我不知道;我不想打开浏览器窗口,有什么想法吗?

Keith™ 发表于 2008-12-11 13:20:37


糟糕的事实是,无论你使用Shell还是调用文件打开对话框,它都是浏览器
但是,无论如何,最快捷、最肮脏的方式是“使用”;ChDir;。我不知道您是如何生成对话框的,但有时在这些情况下设置当前的windows目录是可行的
或者,如果你想把它做好,把下面的代码放到它自己的(.bas)模块中
它包含;“文件打开”;和“;“保存文件”;对话框。它实际上并没有为您打开或保存文件,但它为用户提供了一个界面,并返回所选文件(如果用户取消,则返回一个空字符串)
我还添加了;SetCurrentDirectory;函数,该函数为对话框的启动窗口设定种子。此API版本比;ChDir“;VB的函数,因为它也适用于网络驱动器等。一些示例调用:
Sub main()
    Dim retFile As String
   
    ' set directory you wantdialog to start in as the \windows directory
    SetCurrentDirectory "c:\windows"
   
   
    ' call File Open Dialog
    FileOpen Application.hWnd, MyFile$, "*.dwg", "Acad Drawings", "Select aDrawing"
    MsgBox "The File selected to open was " & MyFile$
   
    ' set directory you wantdialog 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      |
'|                                                                  |
'|                                                                  |
'+--------------------------------------------------------------------+
Public Sub FileOpen(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
    strNewName = vbdShowOpen(lngHwnd, strCurName, FileExt$, FileDesc$, DlgTitle$)
    FileName$ = strNewName
Exit Sub
Err_Control:
    'Just get out, to many things to account for
    MsgBox Err.Description, vbCritical, "Too many errors, aborting"
End Sub
'   +---------------------------------------------------------------+
'   |   Interface from the "OpenFile" routine to the Windows API   |
'   +---------------------------------------------------------------+
Public Function vbdShowOpen(lngHwnd As Long, strDwgName As String, FileExt$, FileDesc$, DlgTitle$) As Variant
Dim lngReturn As Long, ShortSize As Long
Dim LongName As String, shortName As String, strFill As String
Dim strDblSpace As String, strFilter As String
    strFill = Chr(0): strDblSpace = strFill & strFill
    Llama.nStructSize = Len(Llama)
    Llama.hwndOwner = lngHwnd
    'This section is for the filter drop down list
      strFilter = FileDesc$ & strFill & FileExt$ & strFill
      strFilter = strFilter & "All Files" & strFill & "*.*" & strDblSpace
      Llama.sFilter = strFilter
    'This is the default information for the dialog
      Llama.sFile = strDwgName & Space$(1024) & strFill
      Llama.nFileSize = Len(Llama.sFile)
      Llama.sDefFileExt = FileExt$
   
      Llama.sFileTitle = Space(512)
      Llama.nTitleSize = Len(Llama.sFileTitle)
      Llama.sInitDir = CurDir
      Llama.sDlgTitle = DlgTitle$
   
    ' use below to call open dialog
      Llama.flags = OFS_FILE_OPEN_FLAGS
      lngReturn = GetOpenFileName(Llama)
   
      If lngReturn Then
         vbdShowOpen = Llama.sFile
      End If
End Function
'   +---------------------------------------------------------------+
'   |   Interface from the "SaveFile" routine to the Windows API   |
'   +---------------------------------------------------------------+
Public Function vbdShowSave(lngHwnd As Long, strDwgName As String, FileExt$, FileDesc$, Caption$) As String
Dim lngReturn As Long, ShortSize As Long
Dim LongName As String, shortName As String
Dim strFill As String, strDblSpace As String, strFilter As String
strFill = Chr(0): strDblSpace = strFill & strFill
Llama.nStructSize = Len(Llama)
Llama.hwndOwner = lngHwnd
   
'This section is for the filter drop down list
    strFilter = FileDesc$ & strFill & FileExt$ & strFill
    strFilter = strFilter & "All Files" & strFill & "*.*" & strDblSpace
    Llama.sFilter = strFilter
'This is the default information for the dialog
    Llama.sFile = strDwgName & Space$(1024) & strFill
    Llama.nFileSize = Len(Llama.sFile)
    Llama.sDefFileExt = FileExt$
   
    Llama.sFileTitle = Space(512)
    Llama.nTitleSize = Len(Llama.sFileTitle)
    Llama.sInitDir = CurDir
    Llama.sDlgTitle = Caption$
   
' use below to call save dialog
    Llama.flags = OFS_FILE_SAVE_FLAGS
    lngReturn = GetSaveFileName(Llama)
   
    If lngReturn Then
      vbdShowSave = Llama.sFile
    End If
End Function
页: [1]
查看完整版本: 在当前文件夹中打开对话框窗口