FolderDlg.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /////////////////////////////////////////////////////////////////////////////
  2. /*
  3. DESCRIPTION:
  4. CFolderDialog - Folder Selection Dialog Class
  5. Copyright(C) Armen Hakobyan, 2002 - 2005
  6. http://www.codeproject.com/dialog/cfolderdialog.asp
  7. VERSION HISTORY:
  8. 24 Mar 2002 - First release
  9. 30 Mar 2003 - Some minor changes
  10. - Added missing in old Platform SDK new flag definitions
  11. - Added support for both MFC 6.0 and 7.0
  12. - Added OnIUnknown handler for Windows XP folder filtration
  13. - Added SetExpanded and SetOKText and GetSelectedFolder functions
  14. 24 May 2003 - Added OnSelChanged implementation
  15. 14 Jul 2003 - Added custom filtration for Windows XP, thanks to Arik Poznanski
  16. 29 Nov 2003 - Added SetRootFolder, thanks to Eckhard Schwabe ( and Jose Insa )
  17. 02 Jan 2004 - Added GetRootFolder, uncomment if needed
  18. 15 Feb 2005 - Small bug fix in DoModal, thanks to WindSeven
  19. */
  20. /////////////////////////////////////////////////////////////////////////////
  21. #ifndef __FOLDERDLG_H__
  22. #define __FOLDERDLG_H__
  23. #if defined( _MSC_VER ) && ( _MSC_VER >= 1020 )
  24. #pragma once
  25. #endif
  26. /////////////////////////////////////////////////////////////////////////////
  27. #ifndef __AFXDLGS_H__
  28. #include < AfxDlgs.h >
  29. #endif
  30. #ifndef __ATLCONV_H__
  31. #include < AtlConv.h > // MBCS/Unicode Conversion Macros
  32. #endif
  33. // Uncomment if using GetRootFolder
  34. //#ifndef _INC_SHLWAPI
  35. // #include < shlwapi.h >
  36. //#endif
  37. //#pragma comment( lib, "shlwapi.lib" )
  38. /////////////////////////////////////////////////////////////////////////////
  39. #ifndef SAFE_DELETE2
  40. #define SAFE_DELETE2( p ) \
  41. if( p ){ delete[] p; p = NULL; }
  42. #endif
  43. #ifndef SAFE_ZEROMEMORY
  44. #define SAFE_ZEROMEMORY( p, size ) \
  45. if( p ){ ZeroMemory( p, size ); }
  46. #endif
  47. #ifndef SAFE_RELEASE
  48. #if defined( __cplusplus )
  49. #define SAFE_RELEASE( p ) \
  50. if( p ){ p->Release(); p = NULL; }
  51. #else
  52. #define SAFE_RELEASE( p ) \
  53. if( p ){ p->lpVtbl->Release( p ); p = NULL; }
  54. #endif
  55. #endif
  56. #ifndef SAFE_COTASKMEMFREE
  57. #define SAFE_COTASKMEMFREE( p ) \
  58. if( p ){ CoTaskMemFree( (LPVOID)p ); p = NULL; }
  59. #endif
  60. #ifndef _countof
  61. #define _countof( x ) \
  62. ( sizeof( x ) / sizeof( x[ 0 ] ) )
  63. #endif
  64. /////////////////////////////////////////////////////////////////////////////
  65. #ifndef BFFM_SETOKTEXT // Version 5.0 or later
  66. #define BFFM_SETOKTEXT ( WM_USER + 105 ) // Unicode only, req. BIF_USENEWUI
  67. #define BFFM_SETEXPANDED ( WM_USER + 106 ) // Unicode only, req. BIF_USENEWUI
  68. #endif
  69. #ifndef BIF_NEWDIALOGSTYLE // Version 5.0 or later
  70. #define BIF_NEWDIALOGSTYLE 0x0040
  71. #define BIF_BROWSEINCLUDEURLS 0x0080
  72. #define BIF_UAHINT 0x0100 // Req. BIF_NEWDIALOGSTYLE
  73. #define BIF_NONEWFOLDERBUTTON 0x0200
  74. #define BIF_NOTRANSLATETARGETS 0x0400
  75. #define BIF_SHAREABLE 0x8000 // Req. BIF_USENEWUI
  76. #define BIF_USENEWUI ( BIF_NEWDIALOGSTYLE | BIF_EDITBOX )
  77. #endif
  78. /////////////////////////////////////////////////////////////////////////////
  79. class EXPORT_CLASS CFolderDialog : public CCommonDialog
  80. {
  81. // DECLARE_DYNAMIC( CFolderDialog )
  82. public:
  83. CFolderDialog( IN LPCTSTR pszTitle = NULL,
  84. IN LPCTSTR pszSelPath = NULL,
  85. IN CWnd* pWndParent = NULL,
  86. IN UINT uFlags = BIF_RETURNONLYFSDIRS );
  87. virtual ~CFolderDialog( VOID );
  88. public:
  89. #if ( _MFC_VER >= 0x0700 ) // VC++ 2002 (7.0)
  90. virtual INT_PTR DoModal( VOID );
  91. #else
  92. virtual INT DoModal( VOID );
  93. #endif
  94. BOOL SetRootFolder( IN LPCTSTR pszPath );
  95. BOOL GetRootFolder( IN OUT LPTSTR pszPath );
  96. BOOL SetSelectedFolder( IN LPCTSTR pszPath );
  97. public:
  98. AFX_INLINE LPCTSTR GetFolderPath( VOID ) const;
  99. AFX_INLINE LPCTSTR GetFolderName( VOID ) const;
  100. AFX_INLINE INT GetFolderImage( VOID ) const;
  101. AFX_INLINE LPCTSTR GetSelectedFolder( VOID ) const;
  102. AFX_INLINE BROWSEINFO& GetBI( VOID );
  103. AFX_INLINE const BROWSEINFO& GetBI( VOID ) const;
  104. protected:
  105. BROWSEINFO m_bi;
  106. TCHAR m_szSelPath[ MAX_PATH ];
  107. TCHAR m_szFolPath[ MAX_PATH ];
  108. protected:
  109. DECLARE_MESSAGE_MAP()
  110. protected: // Overridables
  111. virtual VOID OnInitialized( VOID );
  112. virtual VOID OnSelChanged( IN LPITEMIDLIST pItemIDList );
  113. virtual INT OnValidateFailed( IN LPCTSTR /*pszPath*/ );
  114. protected: // Windows XP or later
  115. virtual VOID OnIUnknown( IN IUnknown* /*pIUnknown*/ );
  116. protected: // Valid to call only from the above handlers
  117. VOID EnableOK( IN BOOL bEnable = TRUE );
  118. VOID SetSelection( IN LPITEMIDLIST pItemIDList );
  119. VOID SetSelection( IN LPCTSTR pszPath );
  120. VOID SetStatusText( IN LPCTSTR pszText );
  121. protected: // Shell version 5.0 or later:
  122. VOID SetExpanded( IN LPCTSTR pszPath );
  123. VOID SetExpanded( IN LPITEMIDLIST pItemIDList );
  124. VOID SetOKText( IN LPCTSTR pszText );
  125. private:
  126. HWND m_hWnd; // used only in the callback function
  127. private:
  128. static INT CALLBACK BrowseCallbackProc(
  129. IN HWND hWnd, IN UINT uMsg, IN LPARAM lParam, IN LPARAM lpData
  130. );
  131. };
  132. /////////////////////////////////////////////////////////////////////////////
  133. AFX_INLINE LPCTSTR CFolderDialog::GetSelectedFolder( VOID ) const
  134. { return m_szSelPath; }
  135. AFX_INLINE BROWSEINFO& CFolderDialog::GetBI( VOID )
  136. { return m_bi; }
  137. AFX_INLINE const BROWSEINFO& CFolderDialog::GetBI( VOID ) const
  138. { return m_bi; }
  139. /////////////////////////////////////////////////////////////////////////////
  140. // Filled after a call to DoModal
  141. AFX_INLINE LPCTSTR CFolderDialog::GetFolderPath( VOID ) const
  142. { return m_szFolPath; }
  143. AFX_INLINE LPCTSTR CFolderDialog::GetFolderName( VOID ) const
  144. { return m_bi.pszDisplayName; }
  145. AFX_INLINE INT CFolderDialog::GetFolderImage( VOID ) const
  146. { return m_bi.iImage; }
  147. /////////////////////////////////////////////////////////////////////////////
  148. #endif // __FOLDERDLG_H__
  149. /////////////////////////////////////////////////////////////////////////////