Windows图标-Icon文件格式分析

本文深入探讨了Windows图标(Icon)文件的格式,包括其结构、颜色处理、压缩方式等关键信息,为理解及处理Icon文件提供了基础。
摘要由CSDN通过智能技术生成

Windows图标-Icon文件格式分析

最近想做个随时间变化显示为时间的秒数的动态变化图标,查找了很多资料,基本都是使用BitBlt+BuildIcon或CreateIcon等API函数来生成图标,感觉操作过程比较复杂,而且在Picture使用这些函数制造图标的话,运行效果不太理想,因此想到用现有的BMP位图数据换算为ICON文件保存后再用LoadPicture载入后使用还比较合用。找了很多关于图标的资料后,中文的VB构造ICON文件说明都没找到,最后只能自己利用图标制作工具生成些图标分析结合英文的关于C语言的图标构造说明来自己摸索出合适VB使用的ICON文件格式资料,因为在CSDN也没找到VB的图标文件说明,因此将自己的一些分析心得post上来以供大家交流。
+ t7 e) ^. j! }: B+ F/ W4 aICON文件结构有点类似BMP文件,不过因为ICON文件支持多资源,所以比BMP文件多了一个索引目录的结构,以供检索文件内的各个图标资源。图像数据部分除了多一段1bpp的掩码部分以外,剩余的部分和BMP文件的位图信息段及图像信息段是相同的。
1 `4 e1 |6 Q4 ]$ o  B6 z5 z  @ 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)
/ d1 A: Y0 p8 k: k  C- f# g8 jword           idtype;       // resource type (1 for icons)
3 X4 M& N: J* z1 O& w# `word           idcount;      // how many images?
# E0 // ~$ }" W+ |  S" |1 g! _; /5 k+ Kicondirentry   identries[1]; // an entry for each image (idcount of 'em)
0 U7 T2 q. H5 D) i; {+ L: S' p} icondir, *lpicondir; 1 u, w% f) u% ^' V( I! Y: H
VB定义: 6 c8 H2 I! W& m& _0 t+ A
Public Type icondir
- u, @, c4 }! R; R$ w    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就是光标了?
7 X! o2 r/ Z8 f4 S% X4 U: L
    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来定义,读取文件的时候,先读入   
1 y3 {( /2 a: g9 @; t* v2 x$ Y    idreserved As Integer      
5 g+ S; E; V3 Z) G6 _" w" Z! j/ C    idtype As Integer            ' X' ]' f: ?% R( I! ~
    idcount As Integer ! c# p" f3 C. S, z7 z; c# k, j
3个变量后,再用idcount来定义identries(idcount)类型的数量。
" }: T4 l' K& D! }1 O" B' q9 Z 1.1 icondirentry结构,图标资源索引目录结构。
) I7 Z7 h3 }$ V1 P# ?( Q* { C原型定义:
9 S0 i" W6 |7 W' C& t9 |' r" N% v/ /typedef struct
& W7 [& y  Q2 }% Y$ s. D4 m{ . t9 T' W( ]" I* S
byte        bwidth;          // width, in pixels, of the image
7 O) T  Q* [/ J  j, Nbyte        bheight;         // height, in pixels, of the image
6 S: {- J1 a$ l# y$ R; obyte        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
- v" r6 p9 m# {& x! G: qword        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?
8 y( K: u, s; M2 Y! w0 g} icondirentry, *lpicondirentry;
% A, ^7 E  H8 i% w! ], J VB定义:* P0 P, c1 Y! V6 d$ ]+ W1 W0 F& G" v
Public Type icondirentry
% }# H- K5 q4 z6 P) M+ v3 _    bwidth  As Byte ';byte    // width, in pixels, of the image:图像宽度,以象素为单位。一个字节
- B' ]; O) R, K    bheight  As Byte ';byte   // height, in pixels, of the image:图像高度,以象素为单位。一个字节
3 [! r- @* {4 i% k8 C9 c6 D2 B
    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
- N8 e" W& `& X1 y0 /其中dwbytesinres记录了该目录指向的图像数据区的尺寸,dwimageoffset指的是该段目录指向的图像数据段的起点在整个文件中的偏移量。
& Q' c* x$ c* T3 N3 D+ m) V6 |0 z.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值