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

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

嗨,
我需要一些帮助。我有两个单选按钮,当我按下分配列表中的其中一个时,它会显示当前文件夹中的所有dwg。我给它放了一个浏览按钮,这样我就可以打开一个窗口并更改当前文件夹,但我不知道如何更新单选按钮的路径和打开的对话框窗口在同一个文件夹中。有人能帮我吗?
谢谢。


**** Hidden Message *****

Matt__W 发表于 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

montzagcg 发表于 2008-12-11 02:01:30


1.我有全局变量,但我不知道要传递给打开的对话框窗口。要打开对话框窗口,我使用了以下命令:ThisDrawing.SendCommand(“open”&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
这个代码可以,但是我不想打开资源管理器窗口,有什么想法吗?

Rogue 发表于 2008-12-11 13:20:37


好吧,肮脏的事实是,无论您使用Shell还是调用文件打开对话框,它都是资源管理器
或者,如果你想做对…
将以下代码放入它自己的(.bas)模块中
它包含“文件打开”和“保存文件”对话框。它实际上并不为您打开或保存文件,但它为用户提供了一个界面,并返回所选文件(如果用户取消,则返回空字符串)。
我还添加了“SetCurrentDirectory”函数,该函数为对话框的开始窗口设置种子。此API版本比VB的“ChDir”函数工作得更好,因为它也适用于网络驱动器等。
一些示例调用:
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

'和附加的代码模块:

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]
查看完整版本: 在当前文件夹中打开对话框窗口