2012.04.09
CString strResPath;
int nIconIndex = 0;// 在注册表中查找文件对象扩展名的关联资源文件BOOL bRet = IconParser::Parse(pAppInfo->Url(), (LPTSTR)(LPCTSTR)strResPath.GetBuffer(MAX_PATH), &nIconIndex);_ASSERT(bRet);strResPath.ReleaseBuffer();
static BOOL _ParseAssociateIcon(LPCTSTR strFilePath, LPTSTR pIconPath, LPINT pIconIndex)
{ MyTrace(L"_ParseAssociateIcon Enter\n"); if (!strFilePath || !pIconPath || !pIconIndex) return FALSE;if (PathIsDirectory(strFilePath))
{ MyTrace(L"_ParseAssociateIcon 1\n"); CString strDirectroyIconAndIndex; DWORD dwType = 0; DWORD dwSize = 0; SHGetValue(HKEY_CLASSES_ROOT, _T("Directory\\DefaultIcon"), NULL, &dwType, NULL, &dwSize); if (dwType != REG_EXPAND_SZ && dwType != REG_SZ) return FALSE; SHGetValue(HKEY_CLASSES_ROOT, _T("Directory\\DefaultIcon"), NULL, NULL, strDirectroyIconAndIndex.GetBuffer(MAX_PATH), &dwSize); strDirectroyIconAndIndex.ReleaseBuffer();CString strIconPath;
INT nIconIndex; _SplitPathAndIndex(strDirectroyIconAndIndex, &strIconPath, &nIconIndex);//如果是磁盘目录
if (::PathIsRoot(strFilePath)) { MyTrace(L"_ParseAssociateIcon PathIsRoot %s\n",strFilePath); nIconIndex = 8;//磁盘icon的资源 if (DTFenceUtil::SYSTEM::IsVistaOrLater()) { MyTrace(L"_ParseAssociateIcon IsVistaOrLater \n"); strIconPath = L"imageres.dll";TCHAR szSys[MAX_PATH] = {};
ExpandEnvironmentStrings(L"%systemroot%", szSys, MAX_PATH); CString strSys(szSys); if (strSys.Find(strFilePath) == 0) { MyTrace(L"_ParseAssociateIcon IsVistaOrLater -36\n"); nIconIndex = -36; } else { MyTrace(L"_ParseAssociateIcon IsVistaOrLater -32\n"); nIconIndex = -32; } } }ExpandToFullPath(strIconPath);
MyTrace(L"_ParseAssociateIcon checkpath %s\n",strIconPath); if (strIconPath.IsEmpty() || !PathFileExists(strIconPath)) return FALSE; MyTrace(L"_ParseAssociateIcon checkpath ok \n"); StrCpy(pIconPath, strIconPath); *pIconIndex = nIconIndex; return TRUE; }LPCTSTR pExtName = PathFindExtension(strFilePath);
DWORD dwType = 0;
DWORD dwSize = 0;CString strTypeName;
SHGetValue(HKEY_CLASSES_ROOT, pExtName, NULL, &dwType, NULL, &dwSize); if (dwType != REG_SZ) { MyTrace(L"_ParseAssociateIcon SHGetValue(HKEY_CLASSES_ROOT, pExtName, NULL, &dwType, NULL, &dwSize)\n"); return FALSE; } SHGetValue(HKEY_CLASSES_ROOT, pExtName, NULL, NULL, strTypeName.GetBuffer(dwSize / 2 + 8), &dwSize); strTypeName.ReleaseBuffer();CString strDefaultIconKey;
strDefaultIconKey.Format(_T("%s\\DefaultIcon"), (LPCTSTR)strTypeName); dwType = 0; SHGetValue(HKEY_CLASSES_ROOT, strDefaultIconKey, NULL, &dwType, NULL, &dwSize); if (dwType == REG_EXPAND_SZ || dwType == REG_SZ) { MyTrace(L"_ParseAssociateIcon dwType == REG_EXPAND_SZ || dwType == REG_SZ\n"); CString strIconUnexpandPath; SHGetValue(HKEY_CLASSES_ROOT, strDefaultIconKey, NULL, NULL, strIconUnexpandPath.GetBuffer(dwSize / 2 + 8), &dwSize); strIconUnexpandPath.ReleaseBuffer();CString strIconPathAndIndex;
ExpandEnvironmentStrings(strIconUnexpandPath, strIconPathAndIndex.GetBuffer(MAX_PATH), MAX_PATH); strIconPathAndIndex.ReleaseBuffer();CString strIconPath;
INT nIconIndex; _SplitPathAndIndex(strIconPathAndIndex, &strIconPath, &nIconIndex);ExpandToFullPath(strIconPath);
MyTrace(L"_ParseAssociateIcon checkpath %s\n",strIconPath); if (strIconPath.IsEmpty() || !PathFileExists(strIconPath)) return FALSE; MyTrace(L"_ParseAssociateIcon checkpath ok\n"); StrCpy(pIconPath, strIconPath); *pIconIndex = nIconIndex; } else { MyTrace(L"_ParseAssociateIcon ! (dwType == REG_EXPAND_SZ || dwType == REG_SZ)\n"); CString strShellOpenKey; strShellOpenKey.Format(_T("%s\\shell\\open\\command"), (LPCTSTR)strTypeName); dwType = 0; SHGetValue(HKEY_CLASSES_ROOT, strShellOpenKey, NULL, &dwType, NULL, &dwSize); if (dwType != REG_EXPAND_SZ && dwType != REG_SZ) { MyTrace(L"_ParseAssociateIcon command %s\n",strShellOpenKey); return FALSE; }CString strOpenWith;
SHGetValue(HKEY_CLASSES_ROOT, strShellOpenKey, NULL, NULL, strOpenWith.GetBuffer(dwSize / 2 + 8), &dwSize); strOpenWith.ReleaseBuffer(); int nSpacePos = strOpenWith.Find(_T(" ")); if (nSpacePos) strOpenWith = strOpenWith.Left(nSpacePos); strOpenWith.Trim(_T(" \"'\t"));StrCpy(pIconPath, strOpenWith);
*pIconIndex = 0; } MyTrace(L"_ParseAssociateIcon return %s\n",pIconPath); return TRUE;}
/* 同步获取本地ICON图标
* pszPath : 图标资源文件路径(PE、X:\windows\system32\imageres.dll)* dwIcoIdx: 图标文件索引(X:\windows\system32\imageres.dll 索引值为-int)* bMaxIcon: 大图标或小图标(只适用.icon) TRUE:48 * 48 FALSE:32 * 32* nSizeAppropriate( _in && _out ): 查找最匹配的ICON尺寸(默认 48 * 48 )( if nSizeAppropriate > 0 ) */HICON LoadIconFromCache(LPCWSTR pszPath, DWORD dwIcoIdx, BOOL bMaxIcon, int& nSizeAppropriate);X:\windows\system32\imageres.dll 索引值为-int
2012.04.10
IShellLink::GetIconLocation 查找 X:\Program Files\Tencent\QQ\Bin\QQProtect\Bin\QQProtect.exe 的图标资源文件
"C:\Users\lishupeng\AppData\Roaming\Microsoft\Installer\{052CFB79-9D62-42E3-8A15-DE66C2C97C3E}\NewShortcut311_0951773981FA4AB2BC21B7DCEC95892A.exe"。
2012.04.11
图标优化
图标分类:
PE可执行文件(*.exe);
动态连接库(*.dll);
快捷方式(*.lnk);
文件对象;
文件夹对象;
未知文件(无扩展名);
非法文件(用户自建的非32位的 EXE\DLL文件)。
查找最匹配当前显示尺寸的图标 - 查找方式:
1、在PE文件、DLL、内部包含图标的文件中查找;2、查找文件对象关联的可执行程序的资源文件。3、查找Windows系统资源文件X:\windows\system32\imageres.dll、X:\windows\System32\shell32.dll。
2012.04.13
调查开源项目Explorer++的右键环境菜单