Windows图标-Icon文件格式分析。[1]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

[原创]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.文件头

C原型定义:
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;

VB定义:
Public Type icondir
    idreserved As Integer       '; word   // reserved (must be 0):保留字必须是0
    idtype As Integer           '; word     // resource type (1 for icons):资源类型,1是图标,2就是光标了?
    idcount As Integer          '; word      // how many images?:有几个图像资源
    identries() As icondirentry '[1]'icondirentry; // an entry for each image (idcount of 'em):每个图像的入口定义
End Type

文件内的资源个数由idcount来定义,读取文件的时候,先读入   
    idreserved As Integer      
    idtype As Integer          
    idcount As Integer
3个变量后,再用idcount来定义identries(idcount)类型的数量。

1.1 icondirentry结构,图标资源索引目录结构。

C原型定义:
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;

VB定义:
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)
    breserved  As Byte ';byte    // reserved ( must be 0):保留字必须是0
    wplanes  As Integer ';word   // color planes:为目标设备说明位面数,其值将总是被设为1
    wbitcount  As Integer ';word   // bits per pixel:每象素所占位数。
    dwbytesinres  As Long  ';dword   // how many bytes in this resource?:这份资源所占字节数
    dwimageoffset  As Long ';dword   // where in the file is this image?:图像数据(iconimage)起点偏移位置。
End Type ' icondirentry

其中dwbytesinres记录了该目录指向的图像数据区的尺寸,dwimageoffset指的是该段目录指向的图像数据段的起点在整个文件中的偏移量。

2.图像数据段

本文关键:Windows图标-Icon文件格式分析。
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top