----------------------FolderDialog.h----------------------
#pragma once
class CFolderDialog
{
private:
BROWSEINFO m_bi;
TCHAR m_cOpendFolder[260];
public:
CString m_sDialogTitle;
CString m_sInitialFolder;
CString m_sOpendFolder;
CFolderDialog(DWORD dwFlags = NULL, CWnd* pParentWnd = NULL);
~CFolderDialog(void);
void CallbackFunction(HWND hWnd, UINT uMsg, LPARAM lParam);
int DoModal();
};
----------------------FolderDialog.cpp----------------------
#include "StdAfx.h"
#include "FolderDialog.h"
#include "Shlwapi.h"
static int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
CFolderDialog* pFd = (CFolderDialog*)lpData;
pFd->CallbackFunction(hWnd, uMsg, lParam);
return 0;
}
void CFolderDialog::CallbackFunction(HWND hWnd, UINT uMsg, LPARAM lParam)
{
switch(uMsg)
{
case BFFM_INITIALIZED:
if( PathFileExists(m_sInitialFolder) )
{
lstrcpy(m_cOpendFolder, (LPTSTR)(LPCTSTR)m_sInitialFolder);
SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)m_cOpendFolder);
if( (m_sInitialFolder[m_sInitialFolder.GetLength()-1]) != _TEXT('\\') )
{
m_sInitialFolder += _TEXT("\\");
}
CString sTempStr;
if (m_sInitialFolder.GetLength() <= 45)
{
sTempStr = m_sInitialFolder;
}
else
{
sTempStr = m_sInitialFolder.Left(25) + _TEXT("...") + m_sInitialFolder.Right(15);
}
SendMessage(hWnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)(LPCTSTR)sTempStr);
}
break;
case BFFM_SELCHANGED:
if( SHGetPathFromIDList((LPITEMIDLIST)lParam, m_cOpendFolder) )
{
m_sOpendFolder = m_cOpendFolder;
if( (m_sOpendFolder[m_sOpendFolder.GetLength()-1]) != _TEXT('\\') )
{
m_sOpendFolder += _TEXT("\\");
}
CString sTempStr;
if (m_sOpendFolder.GetLength() <= 45)
{
sTempStr = m_sOpendFolder;
}
else
{
sTempStr = m_sOpendFolder.Left(25) + _TEXT("...") + m_sOpendFolder.Right(15);
}
SendMessage(hWnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)(LPCTSTR)sTempStr);
}
break;
}
}
CFolderDialog::CFolderDialog(DWORD dwFlags, CWnd* pParentWnd)
{
memset(&m_bi, 0, sizeof(BROWSEINFO));
if (pParentWnd == NULL)
{
m_bi.hwndOwner = 0;
}
else
{
m_bi.hwndOwner = pParentWnd->GetSafeHwnd();
}
m_bi.pidlRoot = NULL;
m_bi.pszDisplayName = m_cOpendFolder;
m_bi.ulFlags = dwFlags | BIF_STATUSTEXT;
m_bi.lpfn = BrowseCallbackProc;
m_bi.lParam = (LPARAM)this;
}
CFolderDialog::~CFolderDialog(void)
{
}
int CFolderDialog::DoModal()
{
int nReturn = IDOK;
LPITEMIDLIST pidl;
m_bi.lpszTitle = m_sDialogTitle;
pidl = SHBrowseForFolder(&m_bi);
if ( pidl&&SHGetPathFromIDList(pidl, m_cOpendFolder) )
{
m_sOpendFolder = m_cOpendFolder;
if( (m_sOpendFolder[m_sOpendFolder.GetLength()-1]) != _TEXT('\\') )
{
m_sOpendFolder += _TEXT("\\");
}
nReturn = IDOK;
}
else
{
nReturn = IDCANCEL;
}
if(pidl)
{
free(pidl);
}
return nReturn;
}