A 'browse for folder' dialog?

This is a discussion on A 'browse for folder' dialog? within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, Are there any system supplied dialogs that will allow you to browse for a folder/directory? (like in winamp ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    50

    A 'browse for folder' dialog?

    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.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    50
    Thanks for that!

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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 08:32 AM. Reason: Added CoUninitialize(). Thanks for pointing that out.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    50
    Code:
    CoTaskMemFree(pidl);
    cool, didn't know about that one.

    should the program be CoUninitialzise()'ing to?

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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).
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 02-23-2010, 12:49 AM
  2. 'browse for file' dialog box?
    By esaptonor in forum Windows Programming
    Replies: 2
    Last Post: 06-23-2007, 03:34 PM
  3. browse dialog popup
    By Kilo in forum C++ Programming
    Replies: 5
    Last Post: 06-02-2005, 07:19 AM
  4. make Child Dialog not Popup?
    By Zeusbwr in forum Windows Programming
    Replies: 5
    Last Post: 04-08-2005, 02:42 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21