1.实现方法:
使用::ExtractIconEx(参数列表)函数实现获取.exe/.dll./.icon文件的Icon图标资源。
2.::ExtractIconEx函数详解
UINT ExtractIconEx(
__in LPCTSTR lpszFile,
__in int nIconIndex,
__out HICON *phiconLarge, //大图标
__out HICON *phiconSmall, //小图标
__in UINT nIcons
);
lpszFile [in] -- LPCTSTR type
Pointer to a null-terminated string specifying the name of an executable file, DLL, or icon file from which icons will be extracted.
指向一个将要提取icons的指定文件(可执行文件、DLL、icon文件)名字符串(以‘\0’结尾)。
nIconIndex [in] -- int type
Specifies the zero-based index of the first icon to extract. For example, if this value is zero, the function extracts the first icon in the specified file.
If this value is –1 and phiconLarge and phiconSmall are both NULL, the function returns the total number of icons in the specified file. If the file is an executable file or DLL, the return value is the number of RT_GROUP_ICON resources. If the file is an .ico file, the return value is 1.
If this value is a negative number and either phiconLarge or phiconSmall is not NULL, the function begins by extracting the icon whose resource identifier is equal to the absolute value of nIconIndex. For example, use -3 to extract the icon whose resource identifier is 3.
指定从0开始的索引为第一个提取的icon.例如,如果这个值是0,则函数提取指定文件的第一个图标。
如果这个值为-1并且phiconLarge和phiconSmall同时为NULL,函数返回指定文件中包含的图标数目。
如果这个文件是executable文件、DLL,返回值是RT_GROUP_ICON的数量,如果这个文件是.ico文件,
返回值为1。
如果这个值是负数,要么phiconLarge或phiconSmall不为NULL,则函数开始通过提取其资源
标识符等于nIconIndex的绝对值的图标。例如,使用-3来提取其资源标识符为3的图标。
phiconLarge [out] -- HICON type
Pointer to an array of icon handles that receives handles to the large icons extracted from the file. If this parameter is NULL, no large icons are extracted from the file.
指针指向一个用于接收指定文件大图标的句柄数组。如果为NULL则不提取。
phiconSmall [out] -- HICON type
Pointer to an array of icon handles that receives handles to the small icons extracted from the file. If this parameter is NULL, no small icons are extracted from the file.
指针指向一个用于接收指定文件小图标的句柄数组。如果为NULL则不提取。
nIcons [in] -- UINT type
Specifies the number of icons to extract from the file.
指定从文件中提取icons的数目。
Return Value -- UINT type
If the nIconIndex parameter is -1, the phiconLarge parameter is NULL, and the phiconSmall parameter is NULL, then the return value is the number of icons contained in the specified file. Otherwise, the return value is the number of icons successfully extracted from the file.
如果参数nIconIndex值为-1,那么phiconLarge参数应该为NULL,并且phiconSmall参数也为NULL,
则返回值是指定文件中包含的图标数目。否则,返回值是从指定文件中成功提取图标的数量。
Remarks
This function is not supported for icons in 16-bit executables and DLLs.
You must destroy all icons extracted by ExtractIconEx by calling the DestroyIcon function.
To retrieve the dimensions of the large and small icons, use the GetSystemMetrics function with the SM_CXICON, SM_CYICON, SM_CXSMICON, and SM_CYSMICON flags.
此函数不支持16位可执行程序和dll的图标。
你不需销毁所有从ExtractIconExecutable函数提取的icons通过调用DestroyIcon函数。
要提取大/小图标的尺寸,使用GetSystemMetrics函数和SM_CXICON, SM_CYICON, SM_CXSMICON, SM_CYSMICON 配合使用。
3.程序实例
运行环境:windows操作系统、VS2010--MFC Dialog程序
void CGetFileIconDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_ICON, m_ExeFileIcon); //图像控件,Icon模式
DDX_Control(pDX, IDC_ICOLARGE, m_SmallIcon); //图像控件,Icon模式
}
//获取文件的Icon图标
void CGetFileIconDlg::GetFileIcon()
{
HICON *phiconLarge, *phiconSmall;
phiconLarge = new HICON();
phiconSmall = new HICON();
int nIcon = ::ExtractIconEx(_T("C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE"),
1, //测试后发现从0~14都有对应的图标
phiconLarge,
phiconSmall,2);
HICON hiconLarge,hiconSmal ;
hiconLarge = m_SmallIcon.SetIcon(*phiconLarge);
hiconSmal = m_ExeFileIcon.SetIcon(*phiconSmall);
m_SmallIcon.ShowWindow(SW_NORMAL);
m_ExeFileIcon.ShowWindow(SW_NORMAL);
BOOL bDesLarg = DestroyIcon(*phiconLarge); //相当于delete phiconLarge;
BOOL bDesSmal = DestroyIcon(*phiconSmall); //相当于delete phiconSmall;
}
//显示获取的Icon图标资源
void CGetFileIconDlg::OnPaint()
{
if (IsIconic())
{
//框架代码,略
}
else
{
GetFileIcon();
CDialogEx::OnPaint(); //一定要放在最后再调用,不信你可以试试
}
}
4、运行效果
5.不足之处:
.exe文件路径是指定好的,如果要实现利用代码通过后缀名(如,.txt/.doc/.ppt/.pdf)找到关联运行程序的路径,然后通过这个路径找到.exe程序的完整路径,最后利用上面的代码就可以实现为不同后缀名文件指定对应的Icon图标。
现在还在努力实现这个功能中,完成这项功能后将完善这篇博文。Mark:20140104