这是我在VBA应用程序中使用的一个类模块。我不使用CommonDialog控件
我也不知道这是否适用于Windows 7或AutoCAD 2012
- Option Explicit
- '//The Win32 API Functions///
- Private Declare Function GetSaveFileName Lib _
- "comdlg32.dll" Alias "GetSaveFileNameA" _
- (pOpenfilename As OPENFILENAME) As Long
- Private Declare Function GetOpenFileName Lib _
- "comdlg32.dll" Alias "GetOpenFileNameA" _
- (pOpenfilename As OPENFILENAME) As Long
- Private Declare Function FindWindow Lib "user32" _
- Alias "FindWindowA" (ByVal lpClassName As String, _
- ByVal lpWindowName As String) As Long
- '//A few of the available Flags///
- Private Const OFN_FILEMUSTEXIST = &H1000
- Private Const OFN_HIDEREADONLY = &H4
- Private Const OFN_ALLOWMULTISELECT = &H200
- 'This one keeps your dialog from turning into
- 'A browse by folder dialog if multiselect is true!
- 'Not sure what I mean? Remove it from the flags
- 'In the "Show Open" & "Show Save" methods.
- Private Const OFN_EXPLORER As Long = &H80000
- '//The Structure
- Private 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 lngHwnd As Long
- Private strFilter As String
- Private strTitle As String
- Private strDir As String
- Private blnHideReadOnly As Boolean
- Private blnAllowMulti As Boolean
- Private blnMustExist As Boolean
- Public Property Let OwnerHwnd(WindowHandle As Long)
- '//FOR YOU TODO//
- 'Use the API to validate this handle
- lngHwnd = WindowHandle
- 'This value is set at startup to the handle of the
- 'AutoCAD Application window, if you want the owner
- 'to be a user form you will need to obtain its
- 'Handle by using the "FindUserForm" function in
- 'This class.
- End Property
- Public Property Get OwnerHwnd() As Long
- OwnerHwnd = lngHwnd
- End Property
- Public Property Let Title(Caption As String)
- 'don't allow null strings
- If Not Caption = vbNullString Then
- strTitle = Caption
- End If
- End Property
- Public Property Get Title() As String
- Title = strTitle
- End Property
- Public Property Let Filter(ByVal FilterString As String)
- 'Filters change the type of files that are
- 'displayed in the dialog. I have designed this
- 'validation to use the same filter format the
- 'Common dialog OCX uses:
- '"All Files (*.*)|*.*"
- Dim intPos As Integer
- Do While InStr(FilterString, "|") > 0
- intPos = InStr(FilterString, "|")
- If intPos > 0 Then
- FilterString = Left$(FilterString, intPos - 1) _
- & Chr$(0) & Right$(FilterString, _
- Len(FilterString) - intPos)
- End If
- Loop
- If Right$(FilterString, 2) Chr$(0) & Chr$(0) Then
- FilterString = FilterString & Chr$(0)
- End If
- strFilter = FilterString
- End Property
- Public Property Get Filter() As String
- 'Here we reverse the process and return
- 'the Filter in the same format the it was
- 'entered
- Dim intPos As Integer
- Dim strTemp As String
- strTemp = strFilter
- Do While InStr(strTemp, Chr$(0)) > 0
- intPos = InStr(strTemp, Chr$(0))
- If intPos > 0 Then
- strTemp = Left$(strTemp, intPos - 1) _
- & "|" & Right$(strTemp, _
- Len(strTemp) - intPos)
- End If
- Loop
- If Right$(strTemp, 1) = "|" Then
- strTemp = Left$(strTemp, Len(strTemp) - 1)
- End If
- Filter = strTemp
- End Property
- Public Property Let InitialDir(strFolder As String)
- 'Sets the directory the dialog displays when called
- If Len(Dir(strFolder)) > 0 Then
- strDir = strFolder
- Else
|