标准文件对话框

'例: CommonFileOpenSave(, , "所有文件(*.*)" & Chr(0) & "*.*" & Chr(0))
Option Compare Database
Option Explicit
Type tagOPENFILENAME
    lStructSize    As Long
    hwndOwner  As Long
    hInstance  As Long
    strFilter  As String
    strCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    strFile    As String
    nMaxFile   As Long
    strFileTitle As String
    nMaxFileTitle As Long
    strInitialDir As String
    strTitle   As String
    flags      As Long
    nFileOffset As Integer
    nFileExtension As Integer
    strDefExt  As String
    lCustData  As Long
    lpfnHook   As Long
    lpTemplateName As String
End Type
Private Declare Function apiGetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (ofn As tagOPENFILENAME) As Boolean
Private Declare Function apiGetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (ofn As tagOPENFILENAME) As Boolean
Public Function CommonFileOpenSave( _
                                   Optional ByRef flags As Variant, _
                                   Optional ByVal InitialDir As Variant, _
                                   Optional ByVal Filter As Variant, _
                                   Optional ByVal FilterIndex As Variant, _
                                   Optional ByVal DefaultExt As Variant, _
                                   Optional ByVal Filename As Variant, _
                                   Optional ByVal DialogTitle As Variant, _
                                   Optional ByVal hwnd As Variant, _
                                   Optional ByVal OpenFile As Variant) As Variant

Dim ofn        As tagOPENFILENAME
Dim strFilename As String
Dim strFileTitle As String
Dim fResult    As Boolean
Dim strErrNotes As String
    On Error GoTo CommonFileOpenSave_Error

    If IsMissing(InitialDir) Then InitialDir = CurDir
    If IsMissing(Filter) Then Filter = ""
    If IsMissing(FilterIndex) Then FilterIndex = 1
    If IsMissing(flags) Then flags = 0&
    If IsMissing(DefaultExt) Then DefaultExt = ""
    If IsMissing(Filename) Then Filename = ""
    If IsMissing(DialogTitle) Then DialogTitle = ""
    If IsMissing(hwnd) Then hwnd = Application.hWndAccessApp
    If IsMissing(OpenFile) Then OpenFile = True

    strFilename = Left(Filename & String$(255, 0), 255)
    strFileTitle = String$(255, 0)

    With ofn
        .lStructSize = Len(ofn)
        .hwndOwner = hwnd
        .strFilter = Filter
        .nFilterIndex = FilterIndex
        .strFile = strFilename
        .nMaxFile = Len(strFilename)
        .strFileTitle = strFileTitle
        .nMaxFileTitle = Len(strFileTitle)
        .strTitle = DialogTitle
        .flags = flags
        .strDefExt = DefaultExt
        .strInitialDir = InitialDir
        .hInstance = 0
        .strCustomFilter = String(255, 0)
        .nMaxCustFilter = 255
        .lpfnHook = 0
    End With


    If OpenFile Then
        fResult = apiGetOpenFileName(ofn)
    Else
        fResult = apiGetSaveFileName(ofn)
    End If


    If fResult Then
        If Not IsMissing(flags) Then flags = ofn.flags
        CommonFileOpenSave = TrimNull(ofn.strFile)
    Else
        CommonFileOpenSave = Null
    End If

CommonFileOpenSave_WrapUp:
    Exit Function

CommonFileOpenSave_Error:
    CommonFileOpenSave = Null
    GoTo CommonFileOpenSave_WrapUp

End Function

Public Function AddFilterItem(strFilter As String, _
                              strDescription As String, Optional varItem As Variant) As String

Dim strErrNotes As String

    If IsMissing(varItem) Then varItem = "*.*"
    AddFilterItem = strFilter & _
                    strDescription & vbNullChar & _
                    varItem & vbNullChar

End Function

Private Function TrimNull(ByVal strItem As String) As String
Dim intPos     As Integer
Dim strErrNotes As String

    intPos = InStr(strItem, vbNullChar)
    If intPos > 0 Then
        TrimNull = Left(strItem, intPos - 1)
    Else
        TrimNull = strItem
    End If

End Function

选择目录的代码
Option Explicit

Private Const BIF_RETURNONLYFSDIRS = 1
Private Const BIF_DONTGOBELOWDOMAIN = 2
Private Const MAX_PATH = 260

Private Type BrowseInfo
   hwndOwner      As Long
   pIDLRoot       As Long
   pszDisplayName As Long
   lpszTitle      As Long
   ulFlags        As Long
   lpfnCallback   As Long
   lParam         As Long
   iImage         As Long
End Type

Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long

Public Function OpenDirectoryTV(Optional odtvTitle As String) As String

   Dim lpIDList As Long
   Dim sBuffer As String
   Dim szTitle As String
   Dim tBrowseInfo As BrowseInfo
   szTitle = odtvTitle
   lpIDList = SHBrowseForFolder(tBrowseInfo)
   If (lpIDList) Then
      sBuffer = Space(MAX_PATH)
      SHGetPathFromIDList lpIDList, sBuffer
      sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
      OpenDirectoryTV = sBuffer
   End If
End Function

Private Sub Command0_Click()
Me.Text1 = OpenDirectoryTV("请选择一个目录")
End Sub

0 回复,0 引用: 标准对话框代码(打开,另存为,选择目录)

添加回复

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。