现在的位置: 首页 > 综合 > 正文

提取应用程序图标的Delphi程序代码

2013年10月23日 ⁄ 综合 ⁄ 共 4891字 ⁄ 字号 评论关闭
  1. unit   Exeico   
  2.     
  3.   interface   
  4.     
  5.   uses   
  6.       Windows,   Messages,   SysUtils,   Variants,   Classes,   Graphics,   Controls,   Forms,   
  7.       Dialogs,   StdCtrls,   ComCtrls,shellapi;   
  8.     
  9.   type   
  10.       TForm1   =   class(TForm)   
  11.           Label1:   TLabel;   
  12.           Edit1:   TEdit;   
  13.           Label2:   TLabel;   
  14.           Edit2:   TEdit;   
  15.           Button1:   TButton;   
  16.           Button2:   TButton;   
  17.           OpenDialog1:   TOpenDialog;   
  18.           OpenDialog2:   TOpenDialog;   
  19.           StatusBar1:   TStatusBar;   
  20.           procedure   Button2Click(Sender:   TObject);   
  21.           procedure   Button1Click(Sender:   TObject);   
  22.       private   
  23.           {   Private   declarations   }   
  24.       public   
  25.           {   Public   declarations   }   
  26.       end;   
  27.     
  28.   var   
  29.       Form1:   TForm1;   
  30.     
  31.   implementation   
  32.     
  33.   {$R   *.dfm}   
  34.     
  35.   procedure   TForm1.Button2Click(Sender:   TObject);   
  36.   begin   
  37.           Close;   
  38.   end;   
  39.     
  40.   procedure   TForm1.Button1Click(Sender:   TObject);   
  41.   const   
  42.     readlen=10;     //每次读取字节数,可改变   
  43.     icolen=766;//32*32图标长度,根据研究前126为图标头,后640为图标数据   
  44.   var   
  45.     i,j,itemp,nPos:int64;//   nPos为目的图标在目的文件的位置   
  46.     ci,cj:array[0..readlen-1]   of   char;   
  47.     SourceFile,DestFile:String;   
  48.     bOK:boolean;   
  49.     SourceIcon,DestIcon:TIcon;   
  50.     SIconStream,s,sDest:TMemoryStream;   
  51.   begin   
  52.     bOK:=false;   
  53.     if   OpenDialog1.Execute   then   
  54.             SourceFile:=OpenDialog1.FileName   
  55.     else   
  56.             exit;   
  57.     if     AnsiUpperCase(ExtractFileExt(SourceFile))<>'.EXE'   then   
  58.     begin   
  59.             ShowMessage(AnsiUpperCase(ExtractFileExt(SourceFile)));   
  60.             exit;   
  61.     end;   
  62.     Edit1.Text:=SourceFile;   
  63.     if   OpenDialog2.Execute   then   
  64.             DestFile:=OpenDialog2.FileName   
  65.     else   
  66.             exit;   
  67.     if     AnsiUpperCase(ExtractFileExt(DestFile))<>'.EXE'   then   
  68.             exit;   
  69.     Edit2.Text:=DestFile;   
  70.     SourceIcon:=TIcon.Create;   
  71.     case   ExtractIcon(handle,PChar(SourceFile),UINT(-1))   of   
  72.             0:begin   ShowMessage('源程序没有图标');exit;end;   
  73.             1:;   
  74.             else   ShowMessage('源程序有多个图标,本程序选择第一个图标');   
  75.     end;   
  76.     SourceIcon.Handle:=ExtractIcon(handle,PChar(SourceFile),0);//选择第一个图   
  77.   标   
  78.     DestIcon:=TIcon.Create;//选择第N个图标为   ExtractIcon(handle,PChar(Source   
  79.   File),N-1)   
  80.     case   ExtractIcon(handle,PChar(DestFile),UINT(-1))   of   
  81.             0:begin   ShowMessage('目的程序没有图标');exit;end;   
  82.             1:;   
  83.             else   ShowMessage('目的程序有多个图标,本程序选择第一个图标替换');   
  84.     end;   
  85.     DestIcon.Handle:=ExtractIcon(handle,PChar(DestFile),0);//选择第一个图标   
  86.     SIconStream:=TMemoryStream.Create;   
  87.     DestIcon.SaveToStream(sIconStream);   
  88.     if   sIconStream.size<>icolen   then   
  89.             ShowMessage('SIcon.size<>icolen');   
  90.     SDest:=TMemoryStream.Create;   
  91.     sDest.LoadFromFile(DestFile);   
  92.     i:=0;j:=0;       //以下程序查找目的图标在目的程序中的位置   
  93.     while     i<   sDest.size   do   
  94.     begin   
  95.                   itemp:=i;   
  96.                   j:=126;   
  97.   {                 repeat   
  98.               SDest.Position:=i;   
  99.               sDest.read(ci,Readlen);   
  100.               SiconStream.Position:=j;   
  101.               SIconStream.Read(cj,Readlen);   
  102.               i:=i+Readlen;   
  103.               j:=j+Readlen;   
  104.                   until   (String(ci)=String(cj))   and   (i<sDest.size)   and   (j<icolen);   
  105.   }                   ci:='';cj:='';   
  106.                       while   (String(ci)=String(cj))   and   (i<SDest.size)   and   (j<   
  107.   icolen)   do   
  108.                       begin   
  109.                         i:=i+readlen;   
  110.                         j:=j+readlen;   
  111.                         SDest.Position:=i;   
  112.                         SDest.read(ci,readlen);   
  113.                         SiconStream.Position:=j;   
  114.                         SiconStream.Read(cj,readlen);   
  115.                       end;   
  116.                   if   j<icolen   then   
  117.               i:=itemp+1     //没找到   
  118.                   else   
  119.                   begin   
  120.             nPos:=itemp;     //找到   
  121.             bOK:=true;   
  122.             break;   
  123.                   end;   
  124.     end;   
  125.     if   bOK=false   then   
  126.           exit;//目标文件二进制码中未找到图标   
  127.     SIconStream.Clear;//将源程序图标存入   
  128.     SourceIcon.SaveToStream(SIconStream);   
  129.     
  130.     SIconStream.position:=126;   
  131.     s:=TMemoryStream.Create;   
  132.     
  133.     sDest.Position:=0;   
  134.     s.CopyFrom(sDest,nPos);//将目的程序图标前数据拷入   
  135.     s.CopyFrom(SIconStream,640);   //将源程序图标拷入   
  136.     if   sDest.size>sDest.Position+640   then   //将目的程序剩余数据拷入   
  137.     begin   
  138.               sDest.Position:=sDest.Position+640;   
  139.               s.CopyFrom(sDest,sDest.Size-sDest.Position);   
  140.     end;   
  141.     s.SaveToFile(Extractfilepath(application.exename)+'Result.exe');   
  142.     SourceIcon.Free;DestIcon.Free;   //改造好的程序存放在本目录Result.exe文件中   
  143.     SIconStream.Free;s.Free;sDest.Free;   
  144.     ShowMessage(Extractfilepath(application.exename)+'Result.exe');   
  145.   end;

抱歉!评论已关闭.