Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Directory Picker in VC++ 6.0 with MFC

Posted on 2000-04-29
2
724 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 50 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

861 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