本文分享&备忘最近了解到的icon资源在windows平台下相关的一部分知识,所有测试代码都尽可能的依赖win32 API实现。更全面的知识,参考文末列出的”参考资料“。
1
2
3
4
5
6
7 |
typedef
struct { WORD
idReserved; // Reserved (must be 0) WORD
idType; // Resource Type (1 for icons) WORD
idCount; // How many images? ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of ‘em) } ICONDIR, *LPICONDIR; |
1
2
3
4
5
6
7
8
9
10
11 |
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 ( must be 0) 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; |
1
2
3
4
5
6
7 |
typdef struct { BITMAPINFOHEADER icHeader; // DIB header RGBQUAD icColors[1]; // Color table BYTE
icXOR[1]; // DIB bits for XOR mask BYTE
icAND[1]; // DIB bits for AND mask } ICONIMAGE, *LPICONIMAGE; |
icon中含有一张png图片,文件头内容(前0x15字节是icon文件头 + ICONDIRENTRY 的内容):
1
2
3
4
5 |
//<strong>获取exe的32 * 32 icon</strong> SHFILEINFO sfi; ZeroMemory(&sfi, sizeof (SHFILEINFO)); ::SHGetFileInfo(L "D:\\a.exe" , FILE_ATTRIBUTE_NORMAL, &sfi, sizeof (SHFILEINFO), SHGFI_ICON | SHGFI_LARGEICON); HICON
icon_handle = sfi.hIcon; |
1
2
3
4
5 |
//<strong>获取*.rmvb文件类型的32 * 32 icon</strong> SHFILEINFO sfi; ZeroMemory(&sfi, sizeof (SHFILEINFO)); ::SHGetFileInfo(L "*.rmvb" , FILE_ATTRIBUTE_NORMAL, &sfi, sizeof (SHFILEINFO), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES); HICON
icon_handle = sfi.hIcon; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
//获取48 * 48 或者 256 * 256 pixels icon SHFILEINFO sfi; ZeroMemory(&sfi, sizeof (SHFILEINFO)); ::SHGetFileInfo(L "D:\\b.exe" , FILE_ATTRIBUTE_NORMAL, &sfi, sizeof (SHFILEINFO), SHGFI_SYSICONINDEX); HIMAGELIST* imageList = NULL; //SHIL_EXTRALARGE获取48 * 48的图标, SHIL_JUMBO 获取256 * 256的图标。 HRESULT
hResult = ::SHGetImageList(SHIL_EXTRALARGE , IID_IImageList, ( void **)&imageList); HICON
icon_handle = NULL; if
(hResult == S_OK) { if
(hResult == S_OK) { hResult = ((IImageList*)imageList)->GetIcon(sfi.iIcon, ILD_NORMAL, &icon_handle); } } |
原文:http://www.cnblogs.com/cswuyg/p/3603707.html