vb打开文件夹对话框,并可事先指定默认路径,打开指定目录的对话框

点击收藏

 

可以用SHBrowseForFolder来实现

'Objects:   Form1、Command1、Module1  
  'Form1:  
  Option   Explicit  
  Private   Const   BIF_RETURNONLYFSDIRS   =   1  
  Private   Const   BIF_DONTGOBELOWDOMAIN   =   2  
  Private   Const   MAX_PATH   =   260  
  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  
  Private   Declare   Function   LocalAlloc   Lib   "kernel32"   (ByVal   uFlags   As   Long,   ByVal   uBytes   As   Long)   As   Long  
  Private   Declare   Sub   CopyMemory   Lib   "kernel32"   Alias   "RtlMoveMemory"   (pDest   As   Any,   pSource   As   Any,   ByVal   dwLength   As   Long)  
  Private   Const   LPTR   =   (&H0   Or   &H40)  
  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   Function   MyAddressOf(AddressOfX   As   Long)   As   Long  
  MyAddressOf   =   AddressOfX  
  End   Function  
   
  Private   Sub   Command1_Click()  
  Dim   lpIDList   As   Long  
  Dim   sBuffer   As   String  
  Dim   szTitle   As   String  
  Dim   tBrowseInfo   As   BrowseInfo  
  Dim   Ret   As   Long  
  szTitle   =   "This   is   the   title"  
  Dim   sPath   As   String  
  sPath   =   VBA.InputBox("初始路径:",   ,   "C:/program   files")  
  With   tBrowseInfo  
          .hWndOwner   =   Me.hWnd  
          .lpszTitle   =   lstrcat(szTitle,   "")  
          .ulFlags   =   BIF_RETURNONLYFSDIRS   +   BIF_DONTGOBELOWDOMAIN  
          .lpfnCallback   =   MyAddressOf(AddressOf   BrowseForFolders_CallbackProc)  
          Ret   =   LocalAlloc(LPTR,   VBA.Len(sPath)   +   1)  
          CopyMemory   ByVal   Ret,   ByVal   sPath,   VBA.Len(sPath)   +   1  
          .lParam   =   Ret  
  End   With  
  lpIDList   =   SHBrowseForFolder(tBrowseInfo)  
  If   (lpIDList)   Then  
      sBuffer   =   VBA.Space(MAX_PATH)  
      SHGetPathFromIDList   lpIDList,   sBuffer  
      sBuffer   =   VBA.Left(sBuffer,   VBA.InStr(sBuffer,   vbNullChar)   -   1)  
      MsgBox   sBuffer  
      End   If  
  End   Sub  
   
  'Module1:  
  Option   Explicit  
  Private   Declare   Function   SendMessage   Lib   "user32"   Alias   "SendMessageA"   (ByVal   hWnd   As   Long,   ByVal   wMsg   As   Long,   ByVal   wParam   As   Long,   lParam   As   Any)   As   Long  
  Private   Const   WM_USER   =   &H400  
  Private   Const   BFFM_SETSELECTIONA   As   Long   =   (WM_USER   +   102)  
  Private   Const   BFFM_SETSELECTIONW   As   Long   =   (WM_USER   +   103)  
  Private   Const   BFFM_INITIALIZED   As   Long   =   1  
  Public   Function   BrowseForFolders_CallbackProc(ByVal   hWnd   As   Long,   ByVal   uMsg   As   Long,   ByVal   lParam   As   Long,   ByVal   lpData   As   Long)   As   Long  
  If   uMsg   =   BFFM_INITIALIZED   Then  
      SendMessage   hWnd,   BFFM_SETSELECTIONA,   True,   ByVal   lpData  
  End   If  
  End   Function

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值