📄 shfileinfo.cpp
字号:
#include "stdafx.h"
#include "shfileinfo.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
////////////////////////////////////////////////////////////////////////////////
// CSHFileInfo
CSHFileInfo::CSHFileInfo (LPCTSTR lpszFileName)
{
m_strFileName = lpszFileName;
_tsplitpath (m_strFileName, m_szDrive, m_szDir, m_szFname, m_szExt);
}
CSHFileInfo::CSHFileInfo (CFileFind * pFoundFile)
{
m_pFoundFile = pFoundFile;
m_strFileName = m_pFoundFile->GetFilePath ();
_tsplitpath (m_strFileName, m_szDrive, m_szDir, m_szFname, m_szExt);
}
CSHFileInfo:: ~ CSHFileInfo ()
{
// TODO: add destruction code here.
}
///////////////////////////////////////////////////////////////////
// Function: GetFileName()
// Purpose: Retrieves current filename minus the path
// Remarks: if the filename is "c:\incoming\hello.txt", this
// function returns "hello.txt".
///////////////////////////////////////////////////////////////////
CString CSHFileInfo::
GetFileName ()
{
CString str;
str.Format (_T ("%s%s"), m_szFname, m_szExt);
return str;
}
///////////////////////////////////////////////////////////////////
// Function: GetRoot()
// Purpose: Retrieves the path only of the current filename.
// Remarks: if the filename is "c:\incoming\hello.txt", this
// function returns "c:\incoming\".
///////////////////////////////////////////////////////////////////
CString CSHFileInfo::
GetRoot ()
{
CString str;
str.Format (_T ("%s%s"), m_szDrive, m_szDir);
return str;
}
///////////////////////////////////////////////////////////////////
// Function: GetFileTitle()
// Purpose: Retrieves the title of the filename excluding
// the path and extension.
// Remarks: if the filename is "c:\incoming\hello.txt", this
// function returns "hello".
///////////////////////////////////////////////////////////////////
CString CSHFileInfo::
GetFileTitle ()
{
return m_szFname;
}
///////////////////////////////////////////////////////////////////
// Function: GetFileExt()
// Purpose: Retrieves the file extension.
// Remarks: if the filename is "c:\incoming\hello.txt", this
// function returns "txt".
///////////////////////////////////////////////////////////////////
CString CSHFileInfo::
GetFileExt ()
{
return m_szExt;
}
///////////////////////////////////////////////////////////////////
// Function: GetDescription()
// Purpose: Returns the description of the file
///////////////////////////////////////////////////////////////////
CString CSHFileInfo::
GetDescription ()
{
SHFILEINFO sfi;
::SHGetFileInfo (m_strFileName, 0, &sfi,
sizeof (SHFILEINFO), SHGFI_TYPENAME);
return sfi.szTypeName;
}
///////////////////////////////////////////////////////////////////
// Function: Exists()
// Purpose: Determines whether a file or directory exists.
///////////////////////////////////////////////////////////////////
bool CSHFileInfo::
Exist ()
{
WIN32_FIND_DATA fd;
CString szFindPath = m_strFileName;
int nSlash = szFindPath.ReverseFind (_T ('\\'));
int nLength = szFindPath.GetLength ();
if (nSlash == nLength - 1)
{
if (nLength == 3)
if (szFindPath.GetAt (1) == _T (':'))
return true;
else
szFindPath = szFindPath.Left (nSlash);
}
HANDLE hFind = FindFirstFile (szFindPath, &fd);
if (hFind != INVALID_HANDLE_VALUE)
FindClose (hFind);
return hFind != INVALID_HANDLE_VALUE;
}
///////////////////////////////////////////////////////////////////
// Function: GetIconIndex()
// Purpose: Returns the icon index of the file.
///////////////////////////////////////////////////////////////////
int CSHFileInfo::
GetIconIndex ()
{
SHFILEINFO sfi;
::SHGetFileInfo (m_strFileName, 0, &sfi, sizeof (SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
return sfi.iIcon;
}
///////////////////////////////////////////////////////////////////
// Function: GetDisplayName()
// Purpose: Returns the display name for the file.
///////////////////////////////////////////////////////////////////
CString CSHFileInfo::
GetDisplayName ()
{
SHFILEINFO sfi;
::SHGetFileInfo (m_strFileName, 0, &sfi,
sizeof (SHFILEINFO), SHGFI_DISPLAYNAME);
return sfi.szDisplayName;
}
///////////////////////////////////////////////////////////////////
// Function: GetLastWriteTime()
// Purpose: Returns the last time file was written to.
///////////////////////////////////////////////////////////////////
CString CSHFileInfo::
GetLastWriteTime ()
{
CString strTime;
CTime time;
m_pFoundFile->GetLastWriteTime (time);
// Format the date time string.
strTime.Format (_T ("%.2d/%.2d/%d %.2d:%.2d"), time.GetMonth (), time.GetDay (),
time.GetYear (), time.GetHour (), time.GetSecond ());
return strTime;
}
///////////////////////////////////////////////////////////////////
// Function: GetFileSize()
// Purpose: Returns the comma seperated size of the file.
///////////////////////////////////////////////////////////////////
CString CSHFileInfo::
GetFileSize ()
{
CString strFileSize;
CString strTemp;
strTemp.Format (_T ("%d"), m_pFoundFile->GetLength ());
int nSize = strTemp.GetLength () - 1;
int nCount = 1;
while (nSize >= 0)
{
strFileSize += strTemp.GetAt (nSize);
if (nSize == 0)
break;
if (nCount != 3)
++nCount;
else
{
strFileSize += _T (",");
nCount = 1;
}
nSize--;
}
strFileSize.MakeReverse ();
return strFileSize;
}
///////////////////////////////////////////////////////////////////
// Function: GetSystemImageList()
// Purpose: Returns the system image list for small and large icons.
///////////////////////////////////////////////////////////////////
void CSHFileInfo::
GetSystemImageList (CImageList * pSmallList, CImageList * pLargeList)
{
//image list setup
SHFILEINFO ssfi, lsfi;
// Get a handle to the system small icon list
HIMAGELIST hSystemSmallImageList = (HIMAGELIST)
::SHGetFileInfo ((LPCTSTR) _T ("C:\\"), 0, &ssfi,
sizeof (SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
// Attach it to the small image list
// DON'T FORGET TO PUT pSmallList->Detach(); in your destructor
pSmallList->Attach (hSystemSmallImageList);
// Get a handle to the system large icon list
HIMAGELIST hSystemLargeImageList = (HIMAGELIST)
::SHGetFileInfo ((LPCTSTR) _T ("C:\\"), 0, &lsfi,
sizeof (SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_ICON);
// Attach it to the large image list
// DON'T FORGET TO PUT pLargeList->Detach(); in your destructor
pLargeList->Attach (hSystemLargeImageList);
}
//////////////////////////////////////////////////////////////////////
// SHBrowseForFolder callback...
//////////////////////////////////////////////////////////////////////
#pragma warning ( disable : 4100 )
static int __stdcall
BrowseCtrlCallback (HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
CSHFileInfo *pShellFileInfo = (CSHFileInfo *) lpData;
if (uMsg == BFFM_INITIALIZED && !pShellFileInfo->m_strSelDir.IsEmpty ())
{
::SendMessage (hwnd, BFFM_SETSELECTION, TRUE, (LPARAM) (LPCTSTR) (pShellFileInfo->m_strSelDir));
}
else // uMsg == BFFM_SELCHANGED
{
}
return FALSE;
}
#pragma warning ( default : 4100 )
///////////////////////////////////////////////////////////////////
// Function: BrowseForFolder()
// Purpose: Displays browse for folder dialog.
///////////////////////////////////////////////////////////////////
BOOL CSHFileInfo::
BrowseForFolder (CWnd * pParentWnd)
{
LPMALLOC pMalloc;
if (::SHGetMalloc (&pMalloc) != NOERROR)
return FALSE;
BROWSEINFO bInfo;
LPITEMIDLIST pidl;
ZeroMemory ((PVOID) & bInfo, sizeof (BROWSEINFO));
if (!m_strInitDir.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.
//
#ifdef _UNICODE
_tcscpy (olePath, m_strInitDir);
#else // _UNICODE
MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, m_strInitDir.GetBuffer (MAX_PATH), -1,
olePath, MAX_PATH);
#endif // _UNICODE
m_strInitDir.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 FALSE;
}
bInfo.pidlRoot = pidl;
}
}
bInfo.hwndOwner = pParentWnd->GetSafeHwnd ();
bInfo.pszDisplayName = m_strPath.GetBuffer (MAX_PATH);
bInfo.lpszTitle = (m_strTitle.IsEmpty ())? _T ("Open") : m_strTitle;
bInfo.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
bInfo.lpfn = BrowseCtrlCallback; // address of callback function
bInfo.lParam = (LPARAM) this; // pass address of object to callback function
if ((pidl =::SHBrowseForFolder (&bInfo)) == NULL)
{
return FALSE;
}
m_strPath.ReleaseBuffer ();
m_iImageIndex = bInfo.iImage;
if (::SHGetPathFromIDList (pidl, m_strPath.GetBuffer (MAX_PATH)) == FALSE)
{
pMalloc->Free (pidl);
pMalloc->Release ();
return FALSE;
}
m_strPath.ReleaseBuffer ();
pMalloc->Free (pidl);
pMalloc->Release ();
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -