In the demo project, CSBDestination shows a really good example of what you can do.
-- BrowseForfolder.h --
#ifndef __SHELLBROWSER_H__
#define __SHELLBROWSER_H__
#if _MSC_VER >= 1000
#pragma once
#endif
#include
#include
class CBrowseForFolder
{
public:
CBrowseForFolder(const HWND hParent = NULL,
const LPITEMIDLIST pidl = NULL,
const int nTitleID = 0);
CBrowseForFolder(const HWND hParent,
const LPITEMIDLIST pidl,
const CString& strTitle);
virtual ~CBrowseForFolder() = 0;
void SetOwner(const HWND hwndOwner);
void SetRoot(const LPITEMIDLIST pidl);
CString GetTitle() const;
void SetTitle(const CString& strTitle);
bool SetTitle(const int nTitle);
UINT GetFlags() const;
void SetFlags(const UINT ulFlags);
CString GetSelectedFolder() const;
int GetImage() const;
bool SelectFolder();
protected:
virtual void OnInit() const;
virtual void OnSelChanged(const LPITEMIDLIST pidl) const;
void EnableOK(const bool bEnable) const;
void SetSelection(const LPITEMIDLIST pidl) const;
void SetSelection(const CString& strPath) const;
void SetStatusText(const CString& strText) const;
private:
static int __stdcall BrowseCallbackProc(HWND hwnd,
UINT uMsg,
LPARAM lParam,
LPARAM lpData);
typedef std::auto_ptr<char> AUTO_STR;
AUTO_STR m_pchTitle;
BROWSEINFO m_bi;
char m_szSelected[MAX_PATH];
CString m_strPath;
HWND m_hwnd;
};
inline UINT CBrowseForFolder::GetFlags() const
{
return m_bi.ulFlags;
}
inline int CBrowseForFolder::GetImage() const
{
return m_bi.iImage;
}
#endif
-- BrowseForFolder.cpp --
#include "stdafx.h"
#include "BrowseForFolder.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
CBrowseForFolder::CBrowseForFolder(const HWND hParent , const LPITEMIDLIST pidl , const int nTitleID )
{
m_hwnd = NULL;
SetOwner(hParent);
SetRoot(pidl);
SetTitle(nTitleID);
m_bi.lpfn = BrowseCallbackProc;
m_bi.lParam = reinterpret_cast<long>(this);
m_bi.pszDisplayName = m_szSelected;
}
CBrowseForFolder::CBrowseForFolder(const HWND hParent, const LPITEMIDLIST pidl, const CString& strTitle)
{
m_hwnd = NULL;
SetOwner(hParent);
SetRoot(pidl);
SetTitle(strTitle);
m_bi.lpfn = BrowseCallbackProc;
m_bi.lParam = reinterpret_cast<long>(this);
m_bi.pszDisplayName = m_szSelected;
}
CBrowseForFolder::~CBrowseForFolder()
{
}
void CBrowseForFolder::SetOwner(const HWND hwndOwner)
{
if (m_hwnd != NULL)
return;
m_bi.hwndOwner = hwndOwner;
}
void CBrowseForFolder::SetRoot(const LPITEMIDLIST pidl)
{
if (m_hwnd != NULL)
return;
m_bi.pidlRoot = pidl;
}
CString CBrowseForFolder::GetTitle() const
{
return m_bi.lpszTitle;
}
void CBrowseForFolder::SetTitle(const CString& strTitle)
{
if (m_hwnd != NULL)
return;
m_pchTitle = std::auto_ptr<char>(new char [static_cast(strTitle.GetLength()) + 1]);
strcpy(m_pchTitle.get(), strTitle);
m_bi.lpszTitle = m_pchTitle.get();
}
bool CBrowseForFolder::SetTitle(const int nTitle)
{
if (nTitle <= 0)
return false;
CString strTitle;
if(!strTitle.LoadString(static_cast(nTitle)))
{
return false;
}
SetTitle(strTitle);
return true;
}
void CBrowseForFolder::SetFlags(const UINT ulFlags)
{
if (m_hwnd != NULL)
return;
m_bi.ulFlags = ulFlags;
}
CString CBrowseForFolder::GetSelectedFolder() const
{
return m_szSelected;
}
bool CBrowseForFolder::SelectFolder()
{
bool bRet = false;
LPITEMIDLIST pidl;
if ((pidl = ::SHBrowseForFolder(&m;_bi)) != NULL)
{
m_strPath.Empty();
if (SUCCEEDED(::SHGetPathFromIDList(pidl, m_szSelected)))
{
bRet = true;
m_strPath = m_szSelected;
}
LPMALLOC pMalloc;
if (SUCCEEDED(SHGetMalloc(&pMalloc;)))
{
pMalloc->Free(pidl);
(void)pMalloc->Release();
}
}
m_hwnd = NULL;
return bRet;
}
void CBrowseForFolder::OnInit() const
{
}
void CBrowseForFolder::OnSelChanged(const LPITEMIDLIST pidl) const
{
(void)pidl;
}
void CBrowseForFolder::EnableOK(const bool bEnable) const
{
if (m_hwnd == NULL)
return;
(void)SendMessage(m_hwnd, BFFM_ENABLEOK, static_cast(bEnable), NULL);
}
void CBrowseForFolder::SetSelection(const LPITEMIDLIST pidl) const
{
if (m_hwnd == NULL)
return;
(void)SendMessage(m_hwnd, BFFM_SETSELECTION, FALSE, reinterpret_cast<long>(pidl));
}
void CBrowseForFolder::SetSelection(const CString& strPath) const
{
if (m_hwnd == NULL)
return;
(void)SendMessage(m_hwnd, BFFM_SETSELECTION, TRUE, reinterpret_cast<long>(LPCTSTR(strPath)));
}
void CBrowseForFolder::SetStatusText(const CString& strText) const
{
if (m_hwnd == NULL)
return;
(void)SendMessage(m_hwnd, BFFM_SETSTATUSTEXT, NULL, reinterpret_cast<long>(LPCTSTR(strText)));
}
int __stdcall CBrowseForFolder::BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
CBrowseForFolder* pbff = reinterpret_cast(lpData);
pbff->m_hwnd = hwnd;
if (uMsg == BFFM_INITIALIZED)
pbff->OnInit();
else if (uMsg == BFFM_SELCHANGED)
pbff->OnSelChanged(reinterpret_cast(lParam));
return 0;
}