Hi all,
Are there any system supplied dialogs that will allow you to browse for a folder/directory? (like in winamp when you add a directory you get a dialog that just shows the folders on a tree).
thanks.
Hi all,
Are there any system supplied dialogs that will allow you to browse for a folder/directory? (like in winamp when you add a directory you get a dialog that just shows the folders on a tree).
thanks.
SHBrowseForFolder is the function you're looking for. Requires shlobj.h, shell32.lib and an awful lot of stupid shell programming. You'll need at least SHGetPathFromIDList to get a proper path, or SHGetDesktopFolder in case you're interested in browsing the shell namespace's representation of whatever is selected.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Thanks for that!
Code:#include <windows.h> #include <shlobj.h> #include <objbase.h> #pragma comment(lib, "shell32.lib") #pragma comment(lib, "ole32.lib") /* * Displays a directory selection dialog. CoInitialize must be called before calling this function. * szBuf must be MAX_PATH characters in length. hWnd may be NULL. */ BOOL GetFolderSelection(HWND hWnd, LPTSTR szBuf, LPCTSTR szTitle) { LPITEMIDLIST pidl = NULL; BROWSEINFO bi = { 0 }; BOOL bResult = FALSE; bi.hwndOwner = hWnd; bi.pszDisplayName = szBuf; bi.pidlRoot = NULL; bi.lpszTitle = szTitle; bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI; if ((pidl = SHBrowseForFolder(&bi)) != NULL) { bResult = SHGetPathFromIDList(pidl, szBuf); CoTaskMemFree(pidl); } return bResult; } #if 1 #include <stdio.h> int main(void) { TCHAR szFolder[MAX_PATH]; CoInitialize(NULL); if (GetFolderSelection(NULL, szFolder, TEXT("Please select a folder."))) { printf("The folder selected was %s\n", szFolder); } else { printf("Failed to select a folder.\n"); } CoUninitialize(); getchar(); return 0; } #endif
Last edited by anonytmouse; 12-03-2004 at 09:32 AM. Reason: Added CoUninitialize(). Thanks for pointing that out.
cool, didn't know about that one.Code:CoTaskMemFree(pidl);
should the program be CoUninitialzise()'ing to?
Looks like your problem's solved, but I'm attaching a class called CFolderDialog that you might find useful. Instead of a tree control (something I'm absolutely not a fan of), it looks like a standard CFileDialog (it is derived from it) but allows you to browse for files, folders, or both. It's not very well thrown together, but it works like a charm. It is originally from CodeGuru, but I threw some fixes in myself (as ugly as they may be).