⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shfileinfo1.cpp

📁 凌阳单片机61板的 usb模组的PC源码1
💻 CPP
字号:
/////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "SHFileInfo1.h"
#include <shlobj.h>

/////////////////////////////////////////////////////////////////////////////
// CSHFileInfo

CSHFileInfo::CSHFileInfo(LPCTSTR lpszFileName)
{
	m_strFileName = lpszFileName;
	_splitpath(m_strFileName, m_szDrive, m_szDir, m_szFname, m_szExt);
}

CSHFileInfo::CSHFileInfo(CFileFind *pFoundFile)
{
	m_pFoundFile = pFoundFile;
	m_strFileName = m_pFoundFile->GetFilePath();
	_splitpath(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("%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("%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 extension of the filename excluding
//				the path and name.
//	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('\\');
	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;
}

///////////////////////////////////////////////////////////////////
//	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.
	int i=time.GetMonth();
	strTime.Format( "%.2d/%.2d/%d %.2d:%.2d", time.GetMonth(), time.GetDay(),
		time.GetYear(), time.GetHour(), time.GetMinute());

	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 += ",";
			nCount = 1;
		}
		nSize--;
	}

	strFileSize.MakeReverse();
	strFileSize += "B";
	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...
//////////////////////////////////////////////////////////////////////
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;
}

///////////////////////////////////////////////////////////////////
//	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.
			//
			MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_strInitDir.GetBuffer(MAX_PATH), -1,
				olePath, MAX_PATH);
			
			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()) ? "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 + -