Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
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
Const BIF_RETURNONLYFSDIRS = 1
Const MAX_PATH = 260
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 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 Long
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'KPDTeam@Allapi.net
Dim iNull As Integer, lpIDList As Long, lResult As Long
Dim sPath As String, udtBI As BrowseInfo
With udtBI
'Set the owner window
.hWndOwner = Me.hWnd
'lstrcat appends the two strings and returns the memory address
.lpszTitle = lstrcat("C:\", "")
'Return only if the user selected a directory
.ulFlags = BIF_RETURNONLYFSDIRS
End With
'Show the 'Browse for folder' dialog
lpIDList = SHBrowseForFolder(udtBI)
If lpIDList Then
sPath = String$(MAX_PATH, 0)
'Get the path from the IDList
SHGetPathFromIDList lpIDList, sPath
'free the block of memory
CoTaskMemFree lpIDList
iNull = InStr(sPath, vbNullChar)
If iNull Then
sPath = Left$(sPath, iNull - 1)
End If
End If
MsgBox sPath
End Sub
sFilePath = "C:\Windows\System\msvbvm60.dll"
sFolderPath = Left$(sFilePath, InstrRev(sFilePath, "\"))
Option Explicit
Public 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 Enum BrowseFlagsEnum
bifBrowseForComputer = &H1000&
bifBrowseForPrinter = &H2000&
bifBrowseIncludeFiles = &H4000&
bifBrowseIncludeURLs = &H80&
bifShareable = &H8000&
bifDontGoBelowDomain = &H2&
bifEditBox = &H10&
bifReturnFSAncestors = &H8&
bifReturnOnlyFSDirs = &H1&
bifStatusText = &H4&
bifUseNewUI = &H40&
bifValidate = &H20&
End Enum
Private Enum BrowseMessages
'Messages from browser
bffmInitialized = 1
bffmSelChanged = 2
bffmValidateFailedA = 3 'lParam:szPath ret:1(cont),0(EndDialog)
bffmValidateFailedW = 4 'lParam:szPath ret:1(cont),0(EndDialog)
'Messages to browser
bffmSetStatusTextA = (WM_USER + 100)
bffmEnableOk = (WM_USER + 101)
bffmSetSelectionA = (WM_USER + 102)
bffmSetSelectionW = (WM_USER + 103)
bffmSetStatusTextW = (WM_USER + 104)
#If Win32 Then
bffmSetStatusText = bffmSetStatusTextW
bffmSetSelection = bffmSetSelectionW
bffmValidateFailed = bffmValidateFailedW
#Else
bffmSetStatusText = bffmSetStatusTextA
bffmSetSelection = bffmSetSelectionA
bffmValidateFailed = bffmValidateFailedA
#End If
End Enum
Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As BrowseFlagsEnum
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Private BrowsePath As String
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 Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Function FnPtr(ByVal lp As Long) As Long
FnPtr = lp
End Function
Private Sub RemoveNullChar(ByRef Text As String)
Dim I As Long
I = InStr(Text, vbNullChar)
If I > 0 Then Text = Left$(Text, I - 1)
End Sub
Public Function BrowseCallbackProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As Long) As Long
Select Case uMsg
Case BrowseMessages.bffmInitialized
SendMessage hWnd, BrowseMessages.bffmSetSelectionA, True, ByVal BrowsePath
End Select
BrowseCallbackProc = 0&
End Function
Public Function dlgBrowseFolder(OwnerForm As Form, ByRef Path As String, Optional ByVal Title As String, Optional ByVal AllowExtStyle As Boolean = False, Optional ByVal AllowManualEnter As Boolean = False) As Boolean
Dim BI As BrowseInfo, lpIDList As Long
Const MaxPath As Long = 1024&
BrowsePath = Path
Title = StrConv(Title, vbFromUnicode)
With BI
If OwnerForm Is Nothing Then
.hWndOwner = 0&
Else
.hWndOwner = OwnerForm.hWnd
End If
.pIDLRoot = 0&
.pszDisplayName = 0&
.lpszTitle = StrPtr(Title)
.ulFlags = bifReturnOnlyFSDirs Or IIf(AllowExtStyle, bifUseNewUI, 0&) Or IIf(AllowManualEnter, bifEditBox, 0&)
.lpfnCallback = FnPtr(AddressOf BrowseCallbackProc)
.lParam = 0&
.iImage = 0&
End With
lpIDList = SHBrowseForFolder(BI)
If lpIDList <> 0 Then
Path = String$(MaxPath, 0)
SHGetPathFromIDList lpIDList, Path
CoTaskMemFree lpIDList
RemoveNullChar Path
dlgBrowseFolder = True
End If
End Function
Dim S As String
S = "C:\MyFolder"
If dlgBrowseFolder(Me, S, "Select folder") Then
MsgBox S
End If
Сейчас этот форум просматривают: Bing-бот и гости: 17