Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hi All,

I am working with C++ using VS 2010 MFC(WindowXP). Now I want to opem folder dialog(the same in C#) when click on button "Browser". I found out CFolderPickerDialog but It can not display dialog so I can choose a folder which I need.
C++
CFolderPickerDialog  fFolder("C:\\", 0,NULL,0);
fFolder.DoModal()



Please help me.


thanks in advance.
Posted
Comments
RDBurmon 13-Jun-12 9:33am    
Thanks Everyone who replied to this thread , So ngthra, I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.

Have a look at SHBrowseForFolder[^]
 
Share this answer
 
May be this link will help you :)


[^]

Thanks
 
Share this answer
 
Comments
ngthtra 15-Jun-12 7:16am    
How to set default path when open folder dialog Using SHBrowseForFolder? because this code is set default path which it is desktop. and I want to it change.
my solution:
C++
inline 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.
}


/*--------------------------------------------------------------------*/
/*  BrowseForFolder                                                   */
/*--------------------------------------------------------------------*/
/**
 *  @brief   open a folder dialog to choose folder
 *
 *  @param   HWND                 [in] is the parent window.
 *           szCurrent            [in] is an optional start folder. Can be NULL.
 *           szPath               [in] receives the selected path on success. Must be MAX_PATH characters in length.
 *
 *  @return  TRUE: open and return folder path which is choosed
 *           FALSE: can't open
 */
inline BOOL BrowseForFolder(HWND hwnd, LPCTSTR szCurrent, LPTSTR szPath)
{
 
  BROWSEINFO   bi = { 0 };
  LPITEMIDLIST pidl;
  TCHAR        szDisplay[MAX_PATH];
  BOOL         retval;

  CoInitialize(NULL); 
  if(szPath == NULL){
    szPath = szDisplay;
  }  
  bi.hwndOwner      = hwnd;
  bi.pszDisplayName = szDisplay;
  bi.lpszTitle      = TEXT("フォルダを選択してください。");
  bi.ulFlags        = BIF_RETURNONLYFSDIRS;
  bi.lpfn           = BrowseCallbackProc;
  bi.lParam         = (LPARAM) szCurrent; 
  // open browser folder dialog
  pidl = SHBrowseForFolder(&bi);
  if (NULL != pidl)
  {
    // get folder path
    retval = SHGetPathFromIDList(pidl, szPath);
    CoTaskMemFree(pidl);
  }
  else
  {
    retval = FALSE;
  }
  if (!retval)
  {
    szPath[0] = TEXT('\0');
  }
  CoUninitialize();
  return retval;
}
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900