项目:X保密项目
2012.03.28
// #pragmas are used here to insure that the structure's
// packing in memory matches the packing of the EXE or DLL.#pragma pack( push )#pragma pack( 2 )typedef struct{ BYTE bWidth; // Width, in pixels, of the image BYTE bHeight; // Height, in pixels, of the image BYTE bColorCount; // Number of colors in image (0 if >=8bpp) BYTE bReserved; // Reserved WORD wPlanes; // Color Planes WORD wBitCount; // Bits per pixel DWORD dwBytesInRes; // how many bytes in this resource? WORD nID; // the ID} GRPICONDIRENTRY, *LPGRPICONDIRENTRY;typedef struct
{ WORD idReserved; // Reserved (must be 0) WORD idType; // Resource type (1 for icons) WORD idCount; // How many images? GRPICONDIRENTRY idEntries[1]; // The entries for each image} GRPICONDIR, *LPGRPICONDIR;#pragma pack( pop )
枚举图标资源名
Callback for enumerating resources in a DLL/EXE
// Using an ID from the group, Find, Load and Lock the RT_ICON, Get the group icon.
if( (hRsrc = FindResourceW( hInstance, nIndex, RT_GROUP_ICON )) == NULL ) return NULL; if( (hGlobal = LoadResource( hInstance, hRsrc )) == NULL ) return NULL; if( (lpRes = LockResource(hGlobal)) == NULL ) return NULL;// Get the identifier of the icon that is most appropriate for the video display.
LPGRPICONDIR lpGrpIconDir = (LPGRPICONDIR)lpRes; LPGRPICONDIRENTRY lpIconEntryAppropriate = QueryAppropriateSizeIcon(lpGrpIconDir, nSizeAppropriate);int nID;
if (lpIconEntryAppropriate) { nID = lpIconEntryAppropriate->nID; } else { nID = LookupIconIdFromDirectoryEx((PBYTE)lpRes, TRUE, nSizeAppropriate, nSizeAppropriate, LR_DEFAULTCOLOR); }if( (hRsrc = FindResource( hInstance, MAKEINTRESOURCE(nID), RT_ICON )) == NULL )
return NULL; if( (hGlobal = LoadResource( hInstance, hRsrc )) == NULL ) return NULL; // Here, lpRes points to an ICONIMAGE structure if( (lpRes = LockResource(hGlobal)) == NULL ) return NULL;// Let the OS make us an icon
hIcon = CreateIconFromResourceEx( (LPBYTE)lpRes, SizeofResource(hInstance,hRsrc), TRUE, 0x00030000 , nSizeAppropriate, nSizeAppropriate, LR_DEFAULTCOLOR );
LPGRPICONDIRENTRY CImageMgr::QueryAppropriateSizeIcon(LPGRPICONDIR lpGrpIconDir, int& nSizeAppropriate)
{ LPGRPICONDIRENTRY lpIconRet = NULL; int nTmp;vector<LPGRPICONDIRENTRY> vector_icon_fit;
for (int i = 0; i < lpGrpIconDir->idCount; ++i) { LPGRPICONDIRENTRY lpIconEntry = &(lpGrpIconDir->idEntries[i]); // 查找最匹配的尺寸 nTmp = (0 == lpIconEntry->bWidth) ? 256 : lpIconEntry->bWidth; if (nSizeAppropriate == nTmp) { vector_icon_fit.push_back(lpIconEntry); } }if (0 == vector_icon_fit.size())
{ int nBetterSize = 257; // 如果没有最匹配的尺寸则查找最小的更大尺寸 for (int i = 0; i < lpGrpIconDir->idCount; ++i) { LPGRPICONDIRENTRY lpIconEntry = &(lpGrpIconDir->idEntries[i]); nTmp = (0 == lpIconEntry->bWidth) ? 256 : lpIconEntry->bWidth; if ((nTmp > nSizeAppropriate) && (nBetterSize > nTmp)) { nBetterSize = nTmp; } } if (257 == nBetterSize) { nBetterSize = -1; // 如果没有最匹配的尺寸则查找最大的更小尺寸 for (int i = 0; i < lpGrpIconDir->idCount; ++i) { LPGRPICONDIRENTRY lpIconEntry = &(lpGrpIconDir->idEntries[i]); nTmp = (0 == lpIconEntry->bWidth) ? 256 : lpIconEntry->bWidth; if ((nTmp < nSizeAppropriate) && (nBetterSize < nTmp)) { nBetterSize = nTmp; } } }nSizeAppropriate = nBetterSize;
for (int i = 0; i < lpGrpIconDir->idCount; ++i)
{ LPGRPICONDIRENTRY lpIconEntry = &(lpGrpIconDir->idEntries[i]); nTmp = (0 == lpIconEntry->bWidth) ? 256 : lpIconEntry->bWidth; if (nBetterSize == nTmp) { vector_icon_fit.push_back(lpIconEntry); } } }// Bits per pixel 最高颜色位数
int nBitsPixel = 0; int nPosMax = -1; for (int i = 0; i < vector_icon_fit.size(); ++i) { LPGRPICONDIRENTRY lpIconEntry = vector_icon_fit[i]; if (nBitsPixel < lpIconEntry->wBitCount) { nBitsPixel = lpIconEntry->wBitCount; nPosMax = i; } }if (nPosMax > -1)
{ lpIconRet = vector_icon_fit[nPosMax]; }return lpIconRet;
}
对比图标