PropertyGridDirectoryPicker.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2001, nabocorp
  4. // All Rights Reserved
  5. //
  6. ////////////////////////////////////////////////////////////////////////////////
  7. #include "stdafx.h"
  8. #include "shlobj.h"
  9. #include "PropertyGridDirectoryPicker.h"
  10. #define BIF_NEWDIALOGSTYLE 0x0040
  11. string CPropertyGridDirectoryPicker::m_strTitle = "Choose a directory";
  12. CPropertyGridDirectoryPicker::CPropertyGridDirectoryPicker()
  13. {
  14. }
  15. CPropertyGridDirectoryPicker::~CPropertyGridDirectoryPicker()
  16. {
  17. }
  18. int CALLBACK CPropertyGridDirectoryPicker::BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData)
  19. {
  20. switch(uMsg)
  21. {
  22. // If the dialog is being initialised
  23. case BFFM_INITIALIZED:
  24. {
  25. // Send a SetSelection message on the passed directory
  26. SendMessage(hwnd,BFFM_SETSELECTION,TRUE,pData);
  27. break;
  28. }
  29. }
  30. return 0;
  31. }
  32. bool CPropertyGridDirectoryPicker::PickDirectory(string &directory, HWND hwnd)
  33. {
  34. char pszBuffer[MAX_PATH];
  35. pszBuffer[0] = '\0';
  36. // Gets the Shell's default allocator
  37. LPMALLOC pMalloc;
  38. if (::SHGetMalloc(&pMalloc) == NOERROR)
  39. {
  40. BROWSEINFO bi;
  41. LPITEMIDLIST pidl;
  42. // Get help on BROWSEINFO struct
  43. bi.hwndOwner = hwnd;
  44. bi.pidlRoot = NULL;
  45. bi.pszDisplayName = pszBuffer;
  46. bi.lpszTitle = m_strTitle.c_str();
  47. bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE ;
  48. // The callback function initialises the dialog with the passed value
  49. bi.lpfn = BrowseCallbackProc;
  50. bi.lParam = LPARAM(directory.c_str());
  51. // This next call issues the dialog box.
  52. if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
  53. {
  54. // Get the full path into pszBuffer
  55. ::SHGetPathFromIDList(pidl, pszBuffer);
  56. // Free the PIDL allocated by SHBrowseForFolder.
  57. pMalloc->Free(pidl);
  58. }
  59. // Release the shell's allocator.
  60. pMalloc->Release();
  61. }
  62. // get the result
  63. if (strlen(pszBuffer) != 0)
  64. {
  65. directory = pszBuffer;
  66. return TRUE;
  67. }
  68. return FALSE;
  69. }