📄 fileinfo.cpp
字号:
// FileInfo.cpp: implementation of the CFileInfo class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LeoBlock2004.h"
#include "FileInfo.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFileInfo::CFileInfo()
{
}
CFileInfo::CFileInfo(LPCTSTR lpszFileName)
{
m_strFileName=lpszFileName;
//把一个路径名称分解成组成成分
_splitpath(m_strFileName,m_szDrive,m_szDir,m_szFname,m_szExt);
}
CFileInfo::~CFileInfo()
{
}
BOOL CFileInfo::BrowseForFolder(CWnd *pParentWnd)
{
//申请一个指向IMalloc对象的指针
LPMALLOC pMalloc;
//得到一个指向外壳的IMalloc指针
if(::SHGetMalloc(&pMalloc)!=NOERROR)
return FALSE;
//申请BROWSEINFO结构
BROWSEINFO bInfo;
//申请LPITEMIDLIST结构
LPITEMIDLIST pidl;
//申请空间为BROWSEINFO结构大小的内存
ZeroMemory((PVOID)&bInfo,sizeof(BROWSEINFO));
if(!m_strInitDir.IsEmpty())
{
OLECHAR olePath[MAX_PATH];
ULONG chEaten;
ULONG dwAttributes;
HRESULT hr;
LPSHELLFOLDER pDesktopFolder;
//得到桌面的IShellFolder蚧面,它是外壳名字域的根目录
if(SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
{
//IShellFolder::ParseDisplayName()函数需要使用UNICODE编码
//将字符串编码变为UNICODE编码方式
MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,m_strInitDir.GetBuffer(MAX_PATH),-1,olePath,MAX_PATH);
m_strInitDir.ReleaseBuffer(-1);
//将路径转换为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()) ? "Open" : m_strTitle;
bInfo.ulFlags=BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;
//设定回调函数入囗
bInfo.lpfn=BrowseCallbackProc;
bInfo.lParam=(LPARAM)this;
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;
}
CString CFileInfo::GetFileName()
{
CString str;
str.Format("%s%s",m_szFname,m_szExt);
return str;
}
CString CFileInfo::GetRoot()
{
CString str;
str.Format("%s%s",m_szDrive,m_szDir);
return str;
}
CString CFileInfo::GetFileTitle()
{
return m_szFname;
}
CString CFileInfo::GetDescription()
{
SHFILEINFO sfi;
::SHGetFileInfo(m_strFileName,0,&sfi,sizeof(SHFILEINFO),SHGFI_TYPENAME);
return sfi.szTypeName;
}
BOOL CFileInfo::Exist()
{
//该函数用于判断一个文件或路径是否存在
WIN32_FIND_DATA fd;
CString szFindPath=m_strFileName;
int nSlash=szFindPath.ReverseFind('\\');
int nLength=szFindPath.GetLength();
if(nSlash==nLength-1)
{
if(nLength==3)
if(szFindPath.GetAt(1)==':')
return true;
else
szFindPath=szFindPath.Left(nSlash);
}
HANDLE hFind=FindFirstFile(szFindPath,&fd);
if(hFind!=INVALID_HANDLE_VALUE)
FindClose(hFind);
return hFind!=INVALID_HANDLE_VALUE;
}
int CFileInfo::GetIconIndex()
{
SHFILEINFO sfi;
::SHGetFileInfo(m_strFileName,0,&sfi,sizeof(SHFILEINFO),SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
return sfi.iIcon;
}
CString CFileInfo::GetDisplayName()
{
SHFILEINFO sfi;
::SHGetFileInfo(m_strFileName,0,&sfi,sizeof(SHFILEINFO),SHGFI_DISPLAYNAME);
return sfi.szDisplayName;
}
CString CFileInfo::GetLastWriteTime()
{
CString strTime;
CTime time;
m_pFoundFile->GetLastWriteTime(time);
//格式化时间字符串
strTime.Format("%.2d/%.2d/%d %.2d:%.2d",time.GetMonth(),time.GetDay(),time.GetYear(),time.GetHour(),time.GetSecond());
return strTime;
}
CString CFileInfo::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+=",";
nCount=1;
}
nSize--;
}
strFileSize.MakeReverse();
return strFileSize;
}
void CFileInfo::GetSystemImageList(CImageList *pSmallList,CImageList *pLargeList)
{
//图像列表设置
SHFILEINFO ssfi,lsfi;
//得到系统小图标列表的句柄
HIMAGELIST hSystemSmallImageList=(HIMAGELIST)::SHGetFileInfo((LPCTSTR)_T("C:\\"),0,&ssfi,sizeof(SHFILEINFO),SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
//使用系统小图标列表
pSmallList->Attach(hSystemSmallImageList);
//得到系统大图标列表的句柄
HIMAGELIST hSystemLargeImageList=(HIMAGELIST)::SHGetFileInfo((LPCTSTR)_T("C:\\"),0,&lsfi,sizeof(SHFILEINFO),SHGFI_SYSICONINDEX|SHGFI_ICON);
//使用系统大图标列表
pLargeList->Attach(hSystemLargeImageList);
}
int CALLBACK CFileInfo::BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lParam,LPARAM lpData)
{
CFileInfo *pShellFileInfo=(CFileInfo*)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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -