Thread: MFC Open directory dialog box

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    MFC Open directory dialog box

    Is there an MFC class for an open directory dialog box, similar to CFileDialog? I can't seem to find one anywhere.

    If there isn't, what is the best way to do such a box?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Here is an api version:
    Code:
    static int CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg, LPARAM lParam, LPARAM lpData)
    {
    	// If the BFFM_INITIALIZED message is received
    	// set the path to the start path.
    	switch (uMsg)
    	{
    		case BFFM_INITIALIZED:
    		{
    			if (NULL != lpData)
    			{
    				SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
    			}
    		}
    	}
    
    	return 0; // The function should always return 0.
    }
    
    // HWND is the parent window.
    // szCurrent is an optional start folder. Can be NULL.
    // szPath receives the selected path on success. Must be MAX_PATH characters in length.
    BOOL BrowseForFolder(HWND hwnd, LPCTSTR szCurrent, LPTSTR szPath)
    {
    	BROWSEINFO   bi = { 0 };
    	LPITEMIDLIST pidl;
    	TCHAR        szDisplay[MAX_PATH];
    	BOOL         retval;
    
    	CoInitialize();
    
    	bi.hwndOwner      = hwnd;
    	bi.pszDisplayName = szDisplay;
    	bi.lpszTitle      = TEXT("Please choose a folder.");
    	bi.ulFlags        = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
    	bi.lpfn           = BrowseCallbackProc;
    	bi.lParam         = (LPARAM) szCurrent;
    
    	pidl = SHBrowseForFolder(&bi);
    
    	if (NULL != pidl)
    	{
    		retval = SHGetPathFromIDList(pidl, szPath);
    		CoTaskMemFree(pidl);
    	}
    	else
    	{
    		retval = FALSE;
    	}
    
    	if (!retval)
    	{
    		szPath[0] = TEXT('\0');
    	}
    
    	CoUninitialize();
    	return retval;
    }
    Last edited by anonytmouse; 06-01-2004 at 05:13 AM.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Cool, thanks. I might chuck together a class and post it, if I can be bothered.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dialog Box from within a dll without MFC
    By tonyxxy in forum C++ Programming
    Replies: 9
    Last Post: 01-31-2009, 07:49 PM
  2. Open dialog box issue
    By patrick22 in forum Windows Programming
    Replies: 6
    Last Post: 06-11-2008, 03:12 AM
  3. Command to open the 'Open Dialog Box' found in most programs.
    By OmegaFirebolt in forum Windows Programming
    Replies: 5
    Last Post: 03-16-2003, 08:58 PM
  4. Dialog box
    By DeanaM in forum Windows Programming
    Replies: 3
    Last Post: 10-16-2002, 12:04 PM
  5. Simple Dialog Box (noob)
    By tegwin in forum Windows Programming
    Replies: 6
    Last Post: 06-30-2002, 06:04 PM