#include "StdAfx.h"
#include "DirDialog.h"
int __stdcall CDirDialog::BrowseCtrlCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
CDirDialog* pDirDialogObj = (CDirDialog*)lpData;
if (uMsg == BFFM_INITIALIZED )
{
if( ! pDirDialogObj->m_strInitDir.IsEmpty() )
::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)(LPCTSTR)(pDirDialogObj->m_strInitDir));
if( ! pDirDialogObj->m_strDlgTitle.IsEmpty() )
::SetWindowText(hwnd, (LPCTSTR) pDirDialogObj->m_strDlgTitle);
}
else if( uMsg == BFFM_SELCHANGED )
{
LPITEMIDLIST pidl = (LPITEMIDLIST) lParam;
char selection[MAX_PATH];
if( ! ::SHGetPathFromIDList(pidl, (LPWSTR)selection) )
selection[0] = _T('\0');
CString csStatusText;
bool bOK = pDirDialogObj->SelChanged(selection, csStatusText);
if( pDirDialogObj->m_bStatus )
::SendMessage(hwnd, BFFM_SETSTATUSTEXT , 0, (LPARAM)(LPCTSTR)csStatusText);
BOOL bCanSelect = TRUE ;
if(!bOK)
bCanSelect = FALSE ;
::SendMessage(hwnd, BFFM_ENABLEOK, 0, bCanSelect);
}
return 0;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDirDialog::CDirDialog()
{
m_bStatus = false ;
}
CDirDialog::~CDirDialog()
{
}
//取得的路径名有路径标识符
bool CDirDialog::DoBrowse(CWnd *pParent)
{
m_strInitDir.TrimRight();
if( ! m_strInitDir.IsEmpty() )
{
if( m_strInitDir.Right(1) == _T("\\") || m_strInitDir.Right(1) == _T("//") )
m_strInitDir = m_strInitDir.Left(m_strInitDir.GetLength() - 1);
//correct selection "X:"
if( m_strInitDir.Right(1) == _T(":"))
m_strInitDir+=_T("\\");
}
bool bOK = false ;
LPMALLOC pMalloc;
if (SHGetMalloc (&pMalloc)!= NOERROR)
{
return bOK;
}
BROWSEINFO bInfo;
LPITEMIDLIST pidl;
ZeroMemory ( (PVOID) &bInfo,sizeof (BROWSEINFO));
if (!m_strRootDir.IsEmpty ())
{
OLECHAR olePath[MAX_PATH];
ULONG chEaten;
ULONG dwAttributes;
HRESULT hr;
LPSHELLFOLDER pDesktopFolder;
//
// Get a pointer to the Desktop's IShellFolder interface.
//
if (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
{
//
// IShellFolder::ParseDisplayName requires the file name be in Unicode.
//
#ifndef _UNICODE
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_strRootDir.GetBuffer(MAX_PATH), -1,
olePath, MAX_PATH);
#else
memcpy(olePath,m_strRootDir,m_strRootDir.GetLength() * sizeof(TCHAR));
#endif
m_strRootDir.ReleaseBuffer (-1);
//
// Convert the path to an ITEMIDLIST.
//
hr = pDesktopFolder->ParseDisplayName(NULL,
NULL,
olePath,
&chEaten,
&pidl,
&dwAttributes);
if (FAILED(hr))
{
pMalloc ->Free (pidl);
pMalloc ->Release ();
return bOK;
}
bInfo.pidlRoot = pidl;
}
}
if(m_strTitle.IsEmpty())
m_strTitle = _T("请选择目录");
bInfo.hwndOwner = pParent->GetSafeHwnd() ;
bInfo.pszDisplayName = m_strSelDir.GetBuffer (MAX_PATH);
bInfo.lpszTitle = (LPCTSTR)m_strTitle;
bInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS| (m_bStatus ? BIF_STATUSTEXT : 0) | BIF_NEWDIALOGSTYLE;
bInfo.lpfn = BrowseCtrlCallback; // address of callback function
bInfo.lParam = (LPARAM)this; // pass address of object to callback function
if ((pidl = ::SHBrowseForFolder(&bInfo)) == NULL)
{
m_strSelDir.ReleaseBuffer();
return bOK;
}
m_strSelDir.ReleaseBuffer();
m_iImageIndex = bInfo.iImage;
bOK = (::SHGetPathFromIDList(pidl,m_strSelDir.GetBuffer(MAX_PATH)) == TRUE );
m_strSelDir.ReleaseBuffer();
pMalloc ->Free(pidl);
pMalloc ->Release();
if(bOK)
{
if(m_strSelDir.IsEmpty())
m_strSelDir = _T('\\');
else if(m_strSelDir.Right(1) != _T('\\') )
m_strSelDir += _T('\\');
}
return bOK;
}
bool CDirDialog::SelChanged(LPCSTR lpcsSelection, CString& csStatusText)
{
bool bOK = false ;
csStatusText.Empty();
CString strPath(lpcsSelection);
if(!strPath.IsEmpty())
{
if(m_bStatus)
csStatusText = strPath ;
bOK = true;
}
return bOK;
}