#pragma pack(push, 1)
//ICONDIR structure
typedef struct _tagIconDir_t
{
WORD ZeroID; //Reserved. Must always be 0.
WORD ImageType; //Specifies image type: 1 for icon (.ICO) image, 2 for cursor (.CUR) image
WORD ImageCount; // Specifies number of images in the file.
} ICONDIR, *LPICONDIR;
//ICONDIRENTRY structure
typedef struct _tagIconDirEntry_t
{
BYTE bWidth; // Width of the image
BYTE bHeight; // Height of the image (times 2)
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?
DWORD dwImageOffset; // where in the file is this image
} ICONDIRENTRY, *LPICONDIRENTRY;
#pragma pack(pop)
//asc转换成16进制字节数据
int ConvAsc2Hex(LPCSTR srcData, LPBYTE *pData)
{
int iSrclen = (int)strlen(srcData);
int iBytesLen = iSrclen / 2;
*pData = new BYTE[iBytesLen];
BYTE *pByteData = *pData;
for(int src=0,dst=0; src<iSrclen;)
{
BYTE bTemp = 0;
char chH = srcData[src++], chL = srcData[src++];
if(chH >= 'A')
bTemp |= (chH - 'A' + 0x0A)<<4;
else
bTemp |= (chH - '0' + 0x00)<<4;
if(chL >= 'A')
bTemp |= (chL - 'A' + 0x0A)<<0;
else
bTemp |= (chL - '0' + 0x00)<<0;
*pByteData++ = bTemp;
}
return iBytesLen;
}
//ASC数据写成文件
BOOL ConvertAsc2File(LPCSTR srcData, LPCTSTR sFileName)
{
BOOL bRet = FALSE;
BYTE *pData = NULL;
int iBytesLen = ConvAsc2Hex(srcData, &pData);
if(pData && iBytesLen)
{
HANDLE hFile = CreateFile(sFileName,
GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwW = 0;
WriteFile(hFile, pData, iBytesLen, &dwW, NULL);
CloseHandle(hFile);
bRet = TRUE;
}
}
if(pData)
{
delete [] pData;
}
return bRet;
}
//绘制到HDC上
int DrawIconData(LPCSTR srcData, HDC hdc)
{
//转换成源数据
BYTE *pSrcData = NULL;
int iBytesLen = ConvAsc2Hex(srcData, &pSrcData);
ICONDIR *pIconDir = (ICONDIR *)pSrcData;
ICONDIRENTRY *pImgEntryArray = (ICONDIRENTRY *)(pSrcData + sizeof(*pIconDir));
int iErr = 0;
do
{
if(pIconDir->ZeroID != 0)
{
iErr = -1;//头非0
break;
}
if(!(pIconDir->ImageType == 1 //icon
|| pIconDir->ImageType == 2 //cursor
|| 0))
{
iErr = -2; //格式不对
break;
}
int x=0, y=0;
for(int id=0; id<pIconDir->ImageCount; id++)
{
ICONDIRENTRY *pEntry = &pImgEntryArray[id];
HICON hIcon = CreateIconFromResourceEx(
pSrcData + pEntry->dwImageOffset, //pointer to icon or cursor bits
pEntry->dwBytesInRes, // number of bytes in bit buffer
(pIconDir->ImageType == 1), // icon or cursor flag
0x00030000, // format version
pEntry->bWidth, // desired width of icon or cursor
pEntry->bHeight, // desired height of icon or cursor
LR_DEFAULTCOLOR // load resource flags
);
if(hIcon)
{
DrawIcon(hdc, x, y, hIcon);
}
x+= pEntry->bWidth + 2;
}
} while (0);
//结束清理
if(pSrcData)
{
delete []pSrcData;
}
return 0;
}
//测试源数据
LPCSTR picData =
"0000010001002020000001000800A80800001600000028000000200000004000"
"000001000800000000008004000000000000000000000001000000000000FF00"
"0000323232003E3E3E004A4A4A00FFFFFF0056565600626262006E6E6E007A7A"
"7A0086868600929292009E9E9E00AAAAAA00B6B6B600C2C2C200CECECE00DADA"
"DA00E6E6E600F2F2F200F0FAFD00FFFFFF000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"FF000000EF000000DF000000CF000000BF000000AF0000009F0000008F000000"
"7F000000700000006000000050000000400000003000000020000000000000FF"
"000000EF000000DF000000CF000000BF000000AF0000009F0000008F0000007F"
"0000007000000060000000500000004000000030000000200000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000001400"
"0000000000000000000000000000000000000000000000000000000000140000"
"0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0000000E"
"0E14141414141414141414141414141414141414141414141414140E0E00000E"
"001212121212121212121212121212121212121212121212121212140E00000E"
"001212120000000000000000000000000000000000000000121214140E00000E"
"001212120000000000000000000000000000000000000000121214140E00000E"
"001212121212121212121212120000121212121212121212121214140E00000E"
"001212000012121212121212120000121212121212121212121214140E00000E"
"001212000000121212000000000000000000001212000000001214140E00000E"
"001212000000000012000000000000000000001200000000000012140E00000E"
"001212121212000000121212120000121212120000000000121212140E00000E"
"001212121212120000001212120000121212000000121212121212140E00000E"
"001212121212121200000012121212121200000012121212121212140E00000E"
"001212000000000000000000000000000000000000000000001212140E00000E"
"001212000000000000000000000000000000000000000000001212140E00000E"
"001212121212121212000012121212121200001212121212121212140E00000E"
"001212121212121212000000000000000000001212121212121212140E00000E"
"001212121212121212000000000000000000001212121212121212140E00000E"
"001212121212121212000012121212121200001212121212121212140E00000E"
"001212121212121212000000000000000000001212121212121212140E00000E"
"001212121212121212000000000000000000001212121212121212140E00000E"
"001212121212121212000012121212121200001212121212121212140E00000E"
"001212120000000000000000000000000000000000000000121212140E00000E"
"001312120000000000000000000000000000000000000000121212140E00000E"
"001313121212121212000012121212121200001212121212121212140E00000E"
"001313131212121212000012121212121200001212121212121212140E00000E"
"001213131212121212000012121212121200001212121212121212140E00000E"
"001213131212121212121212121212121212121212121212121212140E00000E"
"001212121212121212121212121212121212121212121212121212140E00000E"
"0E00000000000000000000000000000000000000000000000000000E0E000000"
"0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E0E00001400"
"0000000000000000000000000000000000000000000000000000000000140000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"0000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000000";