Function Reference


Displays a dialog box that enables the user to select a Shell folder

#include <WinAPIDlg.au3>
_WinAPI_BrowseForFolderDlg ( [$sRoot = '' [, $sText = '' [, $iFlags = 0 [, $pBrowseProc = 0 [, $lParam = 0 [, $hParent = 0]]]]]] )


$sRoot [optional] The root folder from which to start browsing.
Only the specified folder and its subfolders in the namespace hierarchy appear in the dialog box.
If this parameter is 0, the namespace root (the Desktop folder) is used.
$sText [optional] The string that is displayed above the tree view control in the dialog box.
$iFlags [optional] Flags that specify the options for the dialog box. This parameter can a combination of the following values:

Windows 7 or later
$pBrowseProc [optional] Pointer to a callback function that the dialog box calls when an event occurs.
This function will receive one of the following event messages:
(See MSDN for more information)
$lParam [optional] The value that the dialog box passes to the callback function.
$hParent [optional] Handle to the parent window for the dialog box.

Return Value

Success: The full path for chosen folder, or an empty string if user cancels/closes the dialog.
Failure: Empty string.

See Also

Search SHBrowseForFolder in MSDN Library.


#include <APIDlgConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIDlg.au3>
#include <WinAPISys.au3>

Local Const $sInitDir = @ProgramFilesDir

Local $hBrowseProc = DllCallbackRegister('_BrowseProc', 'int', 'hwnd;uint;lparam;ptr')
Local $pBrowseProc = DllCallbackGetPtr($hBrowseProc)

Local $pText = _WinAPI_CreateString($sInitDir)
Local $sPath = _WinAPI_BrowseForFolderDlg(_WinAPI_PathStripToRoot($sInitDir), 'Select a folder from the list below.', BitOR($BIF_RETURNONLYFSDIRS, $BIF_EDITBOX, $BIF_VALIDATE), $pBrowseProc, $pText)

If $sPath Then
    ConsoleWrite('--------------------------------------------------' & @CRLF)
    ConsoleWrite($sPath & @CRLF)


Func _BrowseProc($hWnd, $iMsg, $wParam, $lParam)
    Local $sPath

    Switch $iMsg
            _WinAPI_SetWindowText($hWnd, 'MyTitle')
            _SendMessage($hWnd, $BFFM_SETSELECTIONW, 1, $lParam)
            $sPath = _WinAPI_ShellGetPathFromIDList($wParam)
            If Not @error Then
                ConsoleWrite($sPath & @CRLF)
            MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Error', _WinAPI_GetString($wParam) & ' is invalid.', 0, $hWnd)
            Return 1
    Return 0
EndFunc   ;==>_BrowseProc