DirDialog.cpp
资源名称:11111111.rar [点击查看]
上传用户:fujunqh
上传日期:2021-05-10
资源大小:7090k
文件大小:4k
源码类别:
多国语言处理
开发平台:
Visual C++
- ///////////////////////////////////////////////////////////////////////////
- // DirDialog.cpp: implementation of the CDirDialog class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "DirDialog.h"
- #include "shlobj.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- CString csStatusText;
- 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, selection) )
- selection[0] = _T(' ');
- BOOL bOk = pDirDialogObj->SelChanged(selection, csStatusText);
- if( pDirDialogObj->m_bStatus )
- ::SendMessage(hwnd, BFFM_SETSTATUSTEXT , 0, (LPARAM)(LPCSTR)selection);
- ::SendMessage(hwnd, BFFM_ENABLEOK, 0, bOk);
- }
- 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);
- 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)
- {
- CString strPath(lpcsSelection);
- if(!strPath.IsEmpty())
- {
- if(m_bStatus)
- csStatusText = strPath ;
- return TRUE;
- }
- else
- return FALSE;
- };