Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Directory Picker in VC++ 6.0 with MFC

Posted on 2000-04-29
2
Medium Priority
?
729 Views
Last Modified: 2008-02-01
I want to get a directory through a dialog. There is a standard dialog that allows you to select a file, but not only a directory. How can I manage that?
0
Comment
Question by:ToniVR
2 Comments
 
LVL 1

Expert Comment

by:cbeattie
ID: 2762638
I'm not in front of MSVC++ right now to walk you through it exactly, but it is very easy.  One of the menu items is something along the lines of Insert ActiveX compoment (Insert | ActiveX mabye).  There is an ActiveX component named something along the lines of FolderPicker or FolderDialog.  Just insert that ActiveX component into your project and then instantiate an object of that type.  I won't submit this as an answer becuase I haven't given you the precise details , but if it works for you, I would appreciate it if you used the "Accept comment as answer" feature.  Thanks.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 200 total points
ID: 2763334
Here's a working example.  Very useful for OnClick handler of [Browse] button.


//====================================================
// Pick Dir
//
static char szPickDir[MAX_PATH];
//---------------------------------------------------------------
int CALLBACK
BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData)
{
      char szTmpDir[MAX_PATH];
      switch( uMsg ) {
            case BFFM_INITIALIZED:
           SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)szPickDir); // WParam is TRUE since you are passing a path. It would be FALSE if you were passing a pidl.
        break;
        case BFFM_SELCHANGED:  // Set the status window to the currently selected path.
           if ( SHGetPathFromIDList((LPITEMIDLIST) lp ,szTmpDir) ) {
              SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szTmpDir);
           }
        break;
        default:
            break;
    }
      return 0;
}

int PickDir( CString& sDir )
{
      int iRet= IDCANCEL;
      BROWSEINFO bi;
      char szDir[MAX_PATH];
      LPITEMIDLIST pidl;
      LPMALLOC pMalloc;

      if ( sDir > "" ) {
            lstrcpyn( szPickDir, (LPCSTR)sDir, sizeof(szPickDir) ) ;
      } else {
            GetCurrentDirectory( sizeof(szPickDir), szPickDir );
      }
      sDir="";

      if ( SUCCEEDED(SHGetMalloc(&pMalloc)) ) {
            ZeroMemory(&bi,sizeof(bi));
            bi.hwndOwner = NULL;
            bi.pszDisplayName = 0;
            bi.pidlRoot = 0;
            bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
            bi.lpfn = BrowseCallbackProc;

            pidl = SHBrowseForFolder(&bi);
            if (pidl) {
                  if (SHGetPathFromIDList(pidl,szDir)) {
                        sDir= szDir;
                        iRet= IDOK;
                  }
                  pMalloc->Free(pidl); pMalloc->Release();
            }
      }
      return ( iRet );
}
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question