我想需要一个类似CommonDialog的File Open的功能,能够选择本机和局域网内的目录,只要目录,但是CommonDialog的File Opend只能打开文件。
请问,各位有没有好办法?

解决方案 »

  1.   

    Option ExplicitPrivate Const BIF_STATUSTEXT = &H4&
    Private Const BIF_RETURNONLYFSDIRS = 1
    Private Const BIF_DONTGOBELOWDOMAIN = 2
    Private Const MAX_PATH = 260Private Const WM_USER = &H400
    Private Const BFFM_INITIALIZED = 1
    Private Const BFFM_SELCHANGED = 2
    Private Const BFFM_SETSTATUSTEXT = (WM_USER + 100)
    Private Const BFFM_SETSELECTION = (WM_USER + 102)Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
                                         ByVal wParam As Long, ByVal lParam As String) As Long
    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 LongPrivate Type BrowseInfo
      hWndOwner      As Long
      pIDLRoot       As Long
      pszDisplayName As Long
      lpszTitle      As String
      ulFlags        As Long
      lpfnCallback   As Long
      lParam         As Long
      iImage         As Long
    End TypePrivate m_CurrentDirectory As String
    Public Function BrowseForFolder(Owner As Form, Optional Title As String, Optional StartDir As String) As String  Dim lpIDList As Long
      Dim szTitle As String
      Dim sBuffer As String
      Dim tBrowseInfo As BrowseInfo
      m_CurrentDirectory = StartDir & vbNullChar  szTitle = Title
      With tBrowseInfo
        .hWndOwner = Owner.hWnd
        .lpszTitle = IIf(Title <> "", Title, "路径选择:")
        .ulFlags = BIF_RETURNONLYFSDIRS + BIF_DONTGOBELOWDOMAIN + BIF_STATUSTEXT
        If StartDir <> "" Then
        .lpfnCallback = GetAddressofFunction(AddressOf BrowseCallbackProc)
        End If
      End With  lpIDList = SHBrowseForFolder(tBrowseInfo)
      If (lpIDList) Then
        sBuffer = Space(MAX_PATH)
        SHGetPathFromIDList lpIDList, sBuffer
        sBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)
        BrowseForFolder = sBuffer
      Else
        BrowseForFolder = ""
      End If
      
    End Function
     
    Private Function BrowseCallbackProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lp As Long, ByVal pData As Long) As Long
      
      Dim lpIDList As Long
      Dim ret As Long
      Dim sBuffer As String
      
      On Error Resume Next
      
      Select Case uMsg
      
        Case BFFM_INITIALIZED
          Call SendMessage(hWnd, BFFM_SETSELECTION, 1, m_CurrentDirectory)
          
        Case BFFM_SELCHANGED
          sBuffer = Space(MAX_PATH)
          
          ret = SHGetPathFromIDList(lp, sBuffer)
          If ret = 1 Then
            Call SendMessage(hWnd, BFFM_SETSTATUSTEXT, 0, sBuffer)
          End If
          
      End Select
      
      BrowseCallbackProc = 0
      
    End FunctionPrivate Function GetAddressofFunction(add As Long) As Long
      GetAddressofFunction = add
    End Function将上面的代码放入模块中。
    调用:
    Dim strPath As StringstrPath = BrowseForFolder(窗体名, 对话框标题,想要打开的目录)
      

  2.   

    选择目录用Shell函数中的SHBrowseForFolder