头文件
/// @class CSHGetFileInfo
/// SHGetFileInfo的包裹类
class CSHGetFileInfo
{
public:
CSHGetFileInfo();
virtual ~CSHGetFileInfo();
/// 获取系统图像列表
/// @param[in] bSmallIcon ture则为小图标,false则为大图标
HIMAGELIST GetSystemImageListHandle( BOOL bSmallIcon );
/// 获取系统图片列表的文件图片索引
/// @param[in] strFileName 文件名
/// @param[in] bSmallIcon ture则为小图标,false则为大图标
int GetFileIconIndex( CString strFileName, BOOL bSmallIcon);
/// 获取系统图片列表的文件夹图片索引
/// @param[in] bSmallIcon
int GetDirIconIndex(BOOL bSmallIcon);
/// 获取文件的icon的句柄
/// @param[in] strFileName 文件名
/// @param[in] bSmallIcon ture则为小图标,false则为大图标
HICON GetFileIconHandle(CString strFileName, BOOL bSmallIcon);
/// 获取文件夹的icon的句柄
/// @param[in] bSmallIcon ture则为小图标,false则为大图标
HICON GetFolderIconHandle(BOOL bSmallIcon );
/// 获取文件类型
/// @param[in] strFileName 文件名
CString GetFileType(CString strFileName);
};
cpp文件
CSHGetFileInfo::CSHGetFileInfo()
{
}
CSHGetFileInfo::~CSHGetFileInfo()
{
}
HIMAGELIST CSHGetFileInfo::GetSystemImageListHandle( BOOL bSmallIcon )
{
HIMAGELIST hSystemImageList;
SHFILEINFO ssfi;
if (bSmallIcon)
{
hSystemImageList =
(HIMAGELIST)SHGetFileInfo(
(LPCTSTR)_T("c:\\"),
0,
&ssfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
}
else
{
hSystemImageList =
(HIMAGELIST)SHGetFileInfo(
(LPCTSTR)_T("c:\\"),
0,
&ssfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_LARGEICON);
}
return hSystemImageList;
}
int CSHGetFileInfo::GetFileIconIndex( CString strFileName, BOOL bSmallIcon )
{
SHFILEINFO sfi;
if (bSmallIcon)
{
SHGetFileInfo(
(LPCTSTR)strFileName,
FILE_ATTRIBUTE_NORMAL,
&sfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);
}
else
{
SHGetFileInfo(
(LPCTSTR)strFileName,
FILE_ATTRIBUTE_NORMAL,
&sfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES);
}
return sfi.iIcon;
}
int CSHGetFileInfo::GetDirIconIndex(BOOL bSmallIcon )
{
SHFILEINFO sfi;
if (bSmallIcon)
{
SHGetFileInfo(
(LPCTSTR)"Doesn't matter",
FILE_ATTRIBUTE_DIRECTORY,
&sfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);
}
else
{
SHGetFileInfo(
(LPCTSTR)"Doesn't matter",
FILE_ATTRIBUTE_DIRECTORY,
&sfi,
sizeof(SHFILEINFO),
SHGFI_SYSICONINDEX | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES);
}
return sfi.iIcon;
}
HICON CSHGetFileInfo::GetFileIconHandle(CString strFileName, BOOL bSmallIcon)
{
SHFILEINFO sfi;
if (bSmallIcon)
{
SHGetFileInfo(
(LPCTSTR)strFileName,
FILE_ATTRIBUTE_NORMAL,
&sfi,
sizeof(SHFILEINFO),
SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);
}
else
{
SHGetFileInfo(
(LPCTSTR)strFileName,
FILE_ATTRIBUTE_NORMAL,
&sfi,
sizeof(SHFILEINFO),
SHGFI_ICON | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES);
}
return sfi.hIcon;
}
HICON CSHGetFileInfo::GetFolderIconHandle(BOOL bSmallIcon )
{
SHFILEINFO sfi;
if (bSmallIcon)
{
SHGetFileInfo(
(LPCTSTR)"Doesn't matter",
FILE_ATTRIBUTE_DIRECTORY,
&sfi,
sizeof(SHFILEINFO),
SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES);
}
else
{
SHGetFileInfo(
(LPCTSTR)"Does not matter",
FILE_ATTRIBUTE_DIRECTORY,
&sfi,
sizeof(SHFILEINFO),
SHGFI_ICON | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES);
}
return sfi.hIcon;
}
CString CSHGetFileInfo::GetFileType(CString strFileName)
{
SHFILEINFO sfi;
SHGetFileInfo(
(LPCTSTR)strFileName,
FILE_ATTRIBUTE_NORMAL,
&sfi,
sizeof(SHFILEINFO),
SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES);
return sfi.szTypeName;
}