What api calls can I use to display a browse for folder dialog???
Printable View
What api calls can I use to display a browse for folder dialog???
Rather than coding all of the drive and folder select dialogue into Visual Basic, it is easier to call a standard common dialogue from Windows. The call used here allows a folder to be selected, without needing to select a file within that folder.
VB Code:
Public Declare Function SHBrowseForFolder Lib "shell32.dll" _ (lpBrowseInfo As BROWSEINFO) As Long Public Declare Function SHGetPathFromIDList Lib "shell32.dll" _ (ByVal pidl As Long, ByVal pszPath As String) As Long Public Type BROWSEINFO howner As Long pidlRoot As Long pszDisplayName As String lpszTitle As String ulFlags As Long lpfn As Long lParam As Long lImage As Long End Type Public bi As BROWSEINFO Public pidl As Long gMyFolder = String$(255, " ") With bi If IsNumeric(hWnd) Then .howner = hWnd .pidlRoot = 0 .lpszTitle = "Select a Registered File folder:" & Chr$(0) End With ‘ Display the dialogue box from Windows pidl = SHBrowseForFolder(bi) ‘ Get the actual folder selected – without any null chars on the end If SHGetPathFromIDList(ByVal pidl, ByVal gMyFolder) Then gMyFolder = Left(Trim(gMyFolder), Len(Trim(gMyFolder)) - 1) End If
Rather than starting the search for a folder from the top level (the desktop), it is often desirable to search for a folder starting from a known point (like the last folder that was selected).
With the SHBrowseForFolder API function used above, this is not easy, as it involves using a Call Back Procedure – as shown below. This procedure sends a message to the browsing window, telling it to start the display where the parameter specifies.
In order to implement this, certain API and function definitions are required in a code module:
As well as the definitions above, the actual code to do the calling of the Browse For Folder window is required. This code sets the initial folder required, although it is the call back routine that actually makes the window select the specified directory.VB Code:
Public Declare Function SHBrowseForFolder Lib "shell32.dll" _ (lpBrowseInfo As BROWSEINFO) As Long Public Declare Function SHGetPathFromIDList Lib "shell32.dll" _ (ByVal pidl As Long, ByVal pszPath As String) As Long Public Type BROWSEINFO howner As Long pidlRoot As Long pszDisplayName As String lpszTitle As String ulFlags As Long lpfn As Long lParam As Long lImage As Long End Type Public bi As BROWSEINFO Public pidl As Long 'release the memory used by the browse for folder Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long) Public Const LMEM_FIXED = &H0 Public Const LMEM_ZEROINIT = &H40 Public Const LPTR = (LMEM_FIXED Or LMEM_ZEROINIT) 'send a message to the browse for folder window 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 Public Const BFFM_INITIALIZED = 1 Public Const BFFM_SELECTIONCHANGED = 2 'allocate and free space for the folder parameter ‘ that is to be passed to browse for folder Public Declare Function LocalAlloc Lib "kernel32" _ (ByVal uFlags As Long, _ ByVal uBytes As Long) As Long Public Declare Function LocalFree Lib "kernel32" _ (ByVal hMem As Long) As Long Public Declare Sub CopyMemory Lib "kernel32" _ Alias "RtlMoveMemory" _ (pDest As Any, _ pSource As Any, _ ByVal dwLength As Long) Public Const MAX_PATH = 260 Public Const WM_USER = &H400 Public Const BFFM_SETSELECTIONA As Long = (WM_USER + 102) Public Const BFFM_SETSELECTIONW As Long = (WM_USER + 103) Public Function BrowseCallbackProcStr(ByVal hWnd As Long, _ ByVal uMsg As Long, _ ByVal lParam As Long, _ ByVal lpData As Long) As Long 'Called from the browse for folder window 'Sets the initial path to whatever has already been set Select Case uMsg Case BFFM_INITIALIZED Call SendMessage(hWnd, BFFM_SETSELECTIONA, _ True, ByVal lpData) Case Else: End Select End Function Public Function FARPROC(ByVal pfn As Long) As Long 'A dummy procedure that receives and ' returns the return value of the AddressOf operator. 'Used to get a pointer (AddressOf) to the call back routine. FARPROC = pfn End Function
VB Code:
Dim lpSelPath As Long Dim sPath As String * MAX_PATH Dim pidl as long ' Get the folder required. ' Allocate it in some memory, with a pointer to it sPath = “C:\Program Files” lpSelPath = LocalAlloc(LPTR, Len(sPath) + 1) CopyMemory ByVal lpSelPath, ByVal sPath, Len(sPath) + 1 With bi If IsNumeric(hWnd) Then .howner = hWnd .pidlRoot = 0 .lpfn = FARPROC(AddressOf BrowseCallbackProcStr) .lParam = lpSelPath .lpszTitle = "Select a Registered File folder:" & Chr$(0) End With pidl = SHBrowseForFolder(bi) If pidl Then If SHGetPathFromIDList(ByVal pidl, ByVal gMyFolder) Then gMyFolder = Left(Trim(gMyFolder), _ Len(Trim(gMyFolder)) - 1) End If Call CoTaskMemFree(pidl) End If Call LocalFree(lpSelPath) ' gMyFolder now holds the path and folder actually selected
Thanks buddy.. just what I was looking for..
I was looking for a way to start in a certain directory, but this seems pretty complicated. What does the pidlRoot value do? Some sample code I've seen describes it as:
Is there a way in VB to use pidlRoot to start in a certain place instead of the callback method?Quote:
Pointer to the item identifier list specifying the location of the "root" folder to browse from. If NULL, the desktop folder is used.
-JoeyCode
Do you know of anyway to center the folder select dialog?