1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
program Project2; uses Windows,Classes; type icondirentry=packed record bwidth:Byte; //该目录对应的资源的宽度 bheight:Byte; //该目录对应的资源的高度 bcolorcount:Byte; //该目录对应的资源的颜色总数,大于8的资源该值为零 breserved:Byte; //保留 wplanes:word; //指定的设备号,如无意外,该值总是为1 wbitcount:word; //该目录对应的资源的位数(色深) dwbytesinres:Dword; //该目录对应的资源占用的字节数 dataoffset:Dword; //该目录对应的资源在文件中的位置(偏移量) end; iconheader=packed record idreserved:word; //保留 idtype:word; //资源类型,图标文件为1,光标文件为2 idcount:word; //该ico文件中共有几个图标,该值决定了icondirentry结构的数目,可见一个ico文件中可能包含几个图标的数据,替换到exe中要分别考虑。 end; var icofile:Tmemorystream; Uh:Cardinal; rdata:Pbyte; header:iconheader; Dgroup:array of icondirentry; i,besti,bestc,bestsize:integer; SourceIcon,TargetFile:String; begin SourceIcon:='C:/Program Files/58.ico'; //图标16x16规格 TargetFile:='C:/Program Files/1.exe'; icofile:=Tmemorystream.Create; icofile.LoadFromFile(Pchar(SourceIcon)); //SourceIcon可以换成ico文件的名称 icofile.Position:=0; icofile.ReadBuffer(header,sizeof(header)); setlength(Dgroup,header.idcount); //空出一位,防止出错 bestc:=0; bestsize:=0; for i := 0 to header.idcount-1 do //忽略空出的一位 begin icofile.ReadBuffer(Dgroup[i],Sizeof(Dgroup[i])); if Dgroup[i].wbitcount>bestc then //找到最适合做主图标的图标数据 if Dgroup[i].bwidth>=bestsize then //同上 begin bestc:=Dgroup[i].wbitcount; bestsize:=Dgroup[i].bwidth; besti:=i; end; end; icofile.Position:=Dgroup[besti].dataoffset; getmem(rdata,Dgroup[besti].dwbytesinres); icofile.ReadBuffer(rdata^,Dgroup[besti].dwbytesinres); Uh:=Beginupdateresource(Pchar(TargetFile),false); updateresource(Uh,RT_ICON,pchar(chr(6)),2052,rdata,Dgroup[besti].dwbytesinres); //替换6号位置 endupdateresource(Uh,false); icofile.Free; end. |
转载请注明:exchen's blog » Delphi 替换 PE 图标资源