vb選擇文件夾
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
'新建一個(gè)模塊Module,復(fù)制如下代碼到里面
Option Explicit 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 Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" ( _ ByVal pidl As Long, _ ByVal pszPath As String) As Long Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" ( _ lpBrowseInfo As BROWSEINFO) As Long Type BROWSEINFO hOwner As Long pidlRoot As Long pszDisplayName As String lpszTitle As String ulFlags As Long lpfnCallback As Long lParam As Long iImage As Long End Type Dim xStartPath As String Function SelectDir(Optional StartPath As String,Optional Titel As String) As String Dim iBROWSEINFO As BROWSEINFO With iBROWSEINFO .lpszTitle = IIf(Len(Titel), Titel, "【請(qǐng)選擇文件夾】") .ulFlags = 7 If Len(StartPath) Then xStartPath = StartPath & vbNullChar .lpfnCallback = GetAddressOf(AddressOf CallBack) End If End With Dim xPath As String, NoErr As Long: xPath = Space$(512) NoErr = SHGetPathFromIDList(SHBrowseForFolder(iBROWSEINFO), xPath) SelectDir = IIf(NoErr, Left$(xPath, InStr(xPath, Chr(0)) - 1), "") End Function Function GetAddressOf(Address As Long) As Long GetAddressOf = Address End Function Function CallBack(ByVal hWnd As Long, _ ByVal Msg As Long, _ ByVal pidl As Long, _ ByVal pData As Long) As Long Select Case Msg Case 1 Call SendMessage(hWnd, 1126, 1, xStartPath) Case 2 Dim sDir As String * 64, tmp As Long tmp = SHGetPathFromIDList(pidl, sDir) If tmp = 1 Then SendMessage hWnd, 1124, 0, sDir End Select End Function '在窗體中: dim strDir as String strDir = SelectDir("C:\", "呵呵,請(qǐng)選擇所需的文件夾")'假設(shè)初始路徑為"C:\" 'strDir中就保存了所選的文件夾 該文章在 2012/7/20 14:13:15 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |