/**
* Copyright(c) 2008 NucTech Corporation, All Rights Reserved.
*
* Filename: FolderDialog.cpp
*
*
* @Author Yu Junheng
* @Version 1.00 Date: 2008-11-19
*/
#include "stdafx.h"
#include "FolderDialog.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
const LPCTSTR lpszDefaultTile = _T("请选择文件夹");
const TCHAR tcSeparator = _T('\\');
const TCHAR tcBias = _T('/');
const LPCTSTR lpszEmptyString = _T("");
CString CFolderDialog::m_csSelDir = lpszEmptyString;
/**
* Constructor
* @return : none
* @since 1.00
*/
CFolderDialog::CFolderDialog()
: m_csCurrentPath(lpszEmptyString),
m_csWindowTitle(lpszEmptyString)
{
}
/**
* Destructor
* @return : none
* @since 1.00
*/
CFolderDialog::~CFolderDialog()
{
}
/**
* Callback function called by SHBrowseForFolder's browse control
* after initialization and when selection changes
* @Param hwnd:
* @Param uMsg:
* @Param lParam:
* @Param lpData:
* @return int __stdcall:
* @since 1.00
*/
int __stdcall CFolderDialog::BrowseCtrlCallback(
HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData )
{
CFolderDialog* pFolderDlg = reinterpret_cast<CFolderDialog*>( lpData );
if ( BFFM_INITIALIZED == uMsg )
{
if ( !pFolderDlg->m_csSelDir.IsEmpty())
{
::SendMessage( hwnd, BFFM_SETSELECTION,
TRUE, (LPARAM)(LPCTSTR)( pFolderDlg->m_csSelDir ));
}
}
::SendMessage( hwnd, BFFM_ENABLEOK, 0, TRUE );
return 0;
}
/**
* Browse a folder
* @Param pParent: [in] The parent pointer
* @Param lpszTitle: [in] The title text
* @return BOOL: TRUE if selected one
* @since 1.00
*/
BOOL CFolderDialog::DoBrowse( CWnd* pParent, LPCTSTR lpszTitle /*= NULL*/)
{
try
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if ( NULL == lpszTitle )
{
m_csWindowTitle = lpszDefaultTile;
}
else
{
m_csWindowTitle = lpszTitle;
}
if ( ! m_csSelDir.IsEmpty() )
{
m_csSelDir.TrimRight();
m_csSelDir.TrimRight( tcBias );
m_csSelDir.TrimRight( tcSeparator );
}
LPMALLOC pMalloc;
if ( NOERROR != ::SHGetMalloc( &pMalloc ))
{
return FALSE;
}
BROWSEINFO bInfo;
LPITEMIDLIST pidl;
::ZeroMemory ((PVOID)&bInfo, sizeof(BROWSEINFO));
bInfo.hwndOwner = (( NULL == pParent ) ? NULL : pParent->GetSafeHwnd());
bInfo.pszDisplayName = m_csCurrentPath.GetBuffer( MAX_PATH );
bInfo.lpszTitle = m_csWindowTitle;
bInfo.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
bInfo.lpfn = BrowseCtrlCallback;
bInfo.lParam = (LPARAM)this;
if ( NULL == ( pidl = ::SHBrowseForFolder( &bInfo )))
{
pMalloc->Free( pidl );
pMalloc->Release();
return FALSE;
}
m_csCurrentPath.ReleaseBuffer();
if ( !::SHGetPathFromIDList( pidl, m_csCurrentPath.GetBuffer( MAX_PATH )))
{
m_csCurrentPath.ReleaseBuffer();
pMalloc->Free( pidl );
pMalloc->Release();
return FALSE;
}
m_csCurrentPath.ReleaseBuffer();
m_csSelDir = m_csCurrentPath;
pMalloc->Free( pidl );
pMalloc->Release();
}
catch (...)
{
return FALSE;
}
return TRUE;
}
- 1
- 2
前往页