Windows图标-Icon文件格式分析
最近想做个随时间变化显示为时间的秒数的动态变化图标,查找了很多资料,基本都是使用BitBlt+BuildIcon或CreateIcon等API函数来生成图标,感觉操作过程比较复杂,而且在Picture使用这些函数制造图标的话,运行效果不太理想,因此想到用现有的BMP位图数据换算为ICON文件保存后再用LoadPicture载入后使用还比较合用。找了很多关于图标的资料后,中文的VB构造ICON文件说明都没找到,最后只能自己利用图标制作工具生成些图标分析结合英文的关于C语言的图标构造说明来自己摸索出合适VB使用的ICON文件格式资料,因为在CSDN也没找到VB的图标文件说明,因此将自己的一些分析心得post上来以供大家交流。
ICON文件结构有点类似BMP文件,不过因为ICON文件支持多资源,所以比BMP文件多了一个索引目录的结构,以供检索文件内的各个图标资源。图像数据部分除了多一段1bpp的掩码部分以外,剩余的部分和BMP文件的位图信息段及图像信息段是相同的。
1.文件头 ' f: P( D! v% I$ f. R. N
C原型定义: $ ^+ o% }3 S' P* b8 G2 M
typedef struct 0 I1 c. [- y( L) p
{ 1 @! F; ~. ]. /$ C9 A" T! U
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 u, w% f) u% ^' V( I! Y: H
VB定义: 6 c8 H2 I! W& m& _0 t+ A
Public Type icondir
idreserved As Integer '; word // reserved (must be 0):保留字必须是0: ~" o8 E6 C" G/ I2 G8 v* o
idtype As Integer '; word // resource type (1 for icons):资源类型,1是图标,2就是光标了?
idcount As Integer '; word // how many images?:有几个图像资源) p. h1 H% f4 n5 w; s+ U% J
identries() As icondirentry ' [1]'icondirentry; // an entry for each image (idcount of 'em):每个图像的入口定义3 d9 Q% v6 A. O6 ~
End Type % S v. Y$ O- D2 B
文件内的资源个数由idcount来定义,读取文件的时候,先读入
idreserved As Integer
idtype As Integer ' X' ]' f: ?% R( I! ~
idcount As Integer ! c# p" f3 C. S, z7 z; c# k, j
3个变量后,再用idcount来定义identries(idcount)类型的数量。
1.1 icondirentry结构,图标资源索引目录结构。
C原型定义:
typedef struct
{ . t9 T' W( ]" I* S
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) 0 X% Y( u$ o2 y) f$ N
byte breserved; // reserved ( must be 0) 3 {6 t( _2 /$ E- O
word wplanes; // color planes
word wbitcount; // bits per pixel 0 b. u+ L' z# v
dword dwbytesinres; // how many bytes in this resource? 4 _& j; F. ^) ~% x5 n7 l8 R
dword dwimageoffset; // where in the file is this image?
} icondirentry, *lpicondirentry;
VB定义:* P0 P, c1 Y! V6 d$ ]+ W1 W0 F& G" v
Public Type icondirentry
bwidth As Byte ';byte // width, in pixels, of the image:图像宽度,以象素为单位。一个字节
bheight As Byte ';byte // height, in pixels, of the image:图像高度,以象素为单位。一个字节
bcolorcount As Byte ';byte // number of colors in image (0 if >=8bpp):图像中的颜色数(如果是>=8bpp的位图则为0) ; Q7 L9 S0 ~5 p- d4 i. |
breserved As Byte ';byte // reserved ( must be 0):保留字必须是0" {4 f3 m; ?3 R' }& G+ o, Q
wplanes As Integer ';word // color planes:为目标设备说明位面数,其值将总是被设为1. }+ r3 z/ S# o
wbitcount As Integer ';word // bits per pixel:每象素所占位数。( N8 D$ // M/ N; i9 `5 b9 }
dwbytesinres As Long ';dword // how many bytes in this resource?:这份资源所占字节数) Q$ }, J- a1 G; O# T2 g* v9 r
dwimageoffset As Long ' ;dword // where in the file is this image?:图像数据(iconimage)起点偏移位置。, K: ^# i8 E2 m7 `4 Z6 i! I2 W
End Type ' icondirentry
其中dwbytesinres记录了该目录指向的图像数据区的尺寸,dwimageoffset指的是该段目录指向的图像数据段的起点在整个文件中的偏移量。
ICON文件结构有点类似BMP文件,不过因为ICON文件支持多资源,所以比BMP文件多了一个索引目录的结构,以供检索文件内的各个图标资源。图像数据部分除了多一段1bpp的掩码部分以外,剩余的部分和BMP文件的位图信息段及图像信息段是相同的。
1.文件头 ' f: P( D! v% I$ f. R. N
C原型定义: $ ^+ o% }3 S' P* b8 G2 M
typedef struct 0 I1 c. [- y( L) p
{ 1 @! F; ~. ]. /$ C9 A" T! U
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 u, w% f) u% ^' V( I! Y: H
VB定义: 6 c8 H2 I! W& m& _0 t+ A
Public Type icondir
idreserved As Integer '; word // reserved (must be 0):保留字必须是0: ~" o8 E6 C" G/ I2 G8 v* o
idtype As Integer '; word // resource type (1 for icons):资源类型,1是图标,2就是光标了?
idcount As Integer '; word // how many images?:有几个图像资源) p. h1 H% f4 n5 w; s+ U% J
identries() As icondirentry ' [1]'icondirentry; // an entry for each image (idcount of 'em):每个图像的入口定义3 d9 Q% v6 A. O6 ~
End Type % S v. Y$ O- D2 B
文件内的资源个数由idcount来定义,读取文件的时候,先读入
idreserved As Integer
idtype As Integer ' X' ]' f: ?% R( I! ~
idcount As Integer ! c# p" f3 C. S, z7 z; c# k, j
3个变量后,再用idcount来定义identries(idcount)类型的数量。
1.1 icondirentry结构,图标资源索引目录结构。
C原型定义:
typedef struct
{ . t9 T' W( ]" I* S
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) 0 X% Y( u$ o2 y) f$ N
byte breserved; // reserved ( must be 0) 3 {6 t( _2 /$ E- O
word wplanes; // color planes
word wbitcount; // bits per pixel 0 b. u+ L' z# v
dword dwbytesinres; // how many bytes in this resource? 4 _& j; F. ^) ~% x5 n7 l8 R
dword dwimageoffset; // where in the file is this image?
} icondirentry, *lpicondirentry;
VB定义:* P0 P, c1 Y! V6 d$ ]+ W1 W0 F& G" v
Public Type icondirentry
bwidth As Byte ';byte // width, in pixels, of the image:图像宽度,以象素为单位。一个字节
bheight As Byte ';byte // height, in pixels, of the image:图像高度,以象素为单位。一个字节
bcolorcount As Byte ';byte // number of colors in image (0 if >=8bpp):图像中的颜色数(如果是>=8bpp的位图则为0) ; Q7 L9 S0 ~5 p- d4 i. |
breserved As Byte ';byte // reserved ( must be 0):保留字必须是0" {4 f3 m; ?3 R' }& G+ o, Q
wplanes As Integer ';word // color planes:为目标设备说明位面数,其值将总是被设为1. }+ r3 z/ S# o
wbitcount As Integer ';word // bits per pixel:每象素所占位数。( N8 D$ // M/ N; i9 `5 b9 }
dwbytesinres As Long ';dword // how many bytes in this resource?:这份资源所占字节数) Q$ }, J- a1 G; O# T2 g* v9 r
dwimageoffset As Long ' ;dword // where in the file is this image?:图像数据(iconimage)起点偏移位置。, K: ^# i8 E2 m7 `4 Z6 i! I2 W
End Type ' icondirentry
其中dwbytesinres记录了该目录指向的图像数据区的尺寸,dwimageoffset指的是该段目录指向的图像数据段的起点在整个文件中的偏移量。