Windows系统SHELL图标获取的功能类分享
1.用途
- 可以获取系统图标列表
- 可以获取任意文件/文件夹/磁盘的图标
- 可以获取任意文件/文件夹/磁盘图标在系统图标列表中的位置
2.头文件
#pragma once #include <winnt.h> class CSHGetFileInfo { public: /// 获取系统图像列表 /// @param[in] bSmallIcon ture则为小图标,false则为大图标 static HIMAGELIST GetSystemImageListHandle(BOOL bSmallIcon = TRUE); /// 获取系统图片列表的文件图片索引 /// @param[in] strFileName 文件名 /// @param[in] bSmallIcon ture则为小图标,false则为大图标 static int GetFileIconIndex(CString strFileName, BOOL bSmallIcon = TRUE); /// 获取系统图片列表的文件夹图片索引 /// @param[in] bSmallIcon static int GetDirIconIndex(BOOL bSmallIcon = TRUE); /// 获取文件的icon的句柄 /// @param[in] diskName 磁盘名 /// @param[in] bSmallIcon ture则为小图标,false则为大图标 static int GetDiskIconIndex(LPCTSTR diskName, BOOL bSmallIcon = TRUE); /// 获取文件的icon的句柄 /// @param[in] strFileName 文件名 /// @param[in] bSmallIcon ture则为小图标,false则为大图标 static HICON GetFileIconHandle(CString strFileName, BOOL bSmallIcon = TRUE); /// 获取文件夹的icon的句柄 /// @param[in] bSmallIcon ture则为小图标,false则为大图标 static HICON GetFolderIconHandle(BOOL bSmallIcon = TRUE); /// 获取磁盘的icon的句柄 /// @param[in] diskName 磁盘名 /// @param[in] bSmallIcon ture则为小图标,false则为大图标 static HICON GetDiskIconHandle(LPCTSTR diskName, BOOL bSmallIcon = TRUE); /// 获取文件类型 /// @param[in] strFileName 文件名 static CString GetFileType(CString strFileName); };
3.源文件
#include "pch.h" #include "CSHGetFileInfo.h" 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)_T("Doesn‘t matter"), FILE_ATTRIBUTE_DIRECTORY, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES); } else { SHGetFileInfo( (LPCTSTR)_T("Doesn‘t matter"), FILE_ATTRIBUTE_DIRECTORY, &sfi, sizeof(SHFILEINFO), SHGFI_SYSICONINDEX | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES); } return sfi.iIcon; } int CSHGetFileInfo::GetDiskIconIndex(LPCTSTR diskName, BOOL bSmallIcon) { SHFILEINFO sfi; DWORD attr = ::GetFileAttributes(diskName); if (bSmallIcon) { ::SHGetFileInfo(diskName, attr, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES); //获取文件信息 } else { ::SHGetFileInfo(diskName, attr, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_ICON | 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)_T("Doesn‘t matter"), FILE_ATTRIBUTE_DIRECTORY, &sfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES); } else { SHGetFileInfo( (LPCTSTR)_T("Doesn‘t matter"), FILE_ATTRIBUTE_DIRECTORY, &sfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_LARGEICON | SHGFI_USEFILEATTRIBUTES); } return sfi.hIcon; } HICON CSHGetFileInfo::GetDiskIconHandle(LPCTSTR diskName, BOOL bSmallIcon) { SHFILEINFO sfi; DWORD attr = ::GetFileAttributes(diskName); if (bSmallIcon) { ::SHGetFileInfo(diskName, attr, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | SHGFI_ICON | SHGFI_SMALLICON | SHGFI_USEFILEATTRIBUTES); //获取文件信息 } else { ::SHGetFileInfo(diskName, attr, &sfi, sizeof(sfi), SHGFI_SYSICONINDEX | 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; }
相关推荐
Victoryxu 2020-09-21
86976172 2020-09-20
yuanbin 2020-08-24
88236637 2020-08-19
zhaojp0 2020-08-15
85206633 2020-08-15
梦想软件设计师 2020-08-14
comeonxueRong 2020-08-06
cunxinwenwu的IT 2020-07-20
84549234 2020-07-18
80296630 2020-07-04
86206132 2020-06-20
龙衣 2020-06-09
89236831 2020-06-04
88236637 2020-06-03
oZaoHua 2020-06-02
tengyuan 2020-05-31
丁丁爸爸的技术 2020-05-28