当前位置: 首页 > 技术文档, 资源共享 > 正文

先在type下面添加如下代码: 在需要执行替换图标的地方添加如下代码:

20100107185130-1954733889
先在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号位置 我写的程序位置都是1
endupdateresource(Uh,false);

   icofile.Free;

本文固定链接: https://blog.meyisi.cn/jishu/460.html | 么意思博客
标签:,

delphi 动态替换图标:等您坐沙发呢!

发表评论

快捷键:Ctrl+Enter