Pak.pas
上传用户:hndmjx
上传日期:2014-09-16
资源大小:3369k
文件大小:6k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. {
  2.    Copyright 2003-2004 by Sima Huapeng 
  3.    smhp@163.net
  4. }
  5. unit Pak;
  6. interface
  7. uses
  8.   Windows, Messages, SysUtils, Classes, Graphics, shellApi;
  9. type
  10.   TexePak = class(TComponent)
  11.   private
  12.     fSourceFile: string;
  13.     fDestFile: string;
  14.     fOutFile: String;
  15.     
  16.     procedure SetSourceFile(const Value: string);
  17.     procedure SetDestFile(const Value: string);
  18.     procedure SetOutFile(const Value: string);
  19.   protected
  20.   public
  21.     constructor Create(AOwner: TComponent); reintroduce;
  22.     destructor Destroy; override;
  23.     function ExchangeExeIcon:Boolean;
  24.     property SourceFile: string read fSourceFile write SetSourceFile;
  25.     property DestFile: string read fDestFile write SetDestFile;
  26.     property OutFile: string read fOutFile write SetOutFile;
  27.   published
  28.   end;
  29. implementation
  30. constructor TexePak.Create(AOwner: TComponent);
  31. begin
  32.    inherited Create(AOwner);
  33. end;
  34. destructor TexePak.Destroy;
  35. begin
  36.   inherited;
  37. end;
  38. function TexePak.ExchangeExeIcon():Boolean;
  39. const
  40.     readlen=10;                     //每次读取字节数,可改变
  41.     icolen=766;                     //32*32图标长度,根据研究前126为图标头,后640为图标数据
  42. var
  43.     i,j,itemp,nPos:int64;           // nPos为目的图标在目的文件的位置
  44.     ci,cj:array[0..readlen-1] of char;
  45.     bOK:boolean;
  46.     SourceIcon,DestIcon:TIcon;
  47.     SIconStream,s,sDest:TMemoryStream;
  48. begin
  49.     Result:=False;
  50.     bOK:=false;
  51.     if ExtractIcon(0,PChar(fSourceFile),UINT(-1))=0 then exit;
  52.     SourceIcon:=TIcon.Create;
  53.     try
  54.         try
  55.             SourceIcon.Handle:=ExtractIcon(0,PChar(fSourceFile),0);      //选择第一个图标
  56.             if ExtractIcon(0,PChar(fDestFile),UINT(-1))=0 then exit;
  57.             DestIcon:=TIcon.Create;
  58.             try
  59.                 DestIcon.Handle:=ExtractIcon(0,PChar(fDestFile),0);//选择第一个图标
  60.                 SIconStream:=TMemoryStream.Create;
  61.                 try
  62.                   DestIcon.SaveToStream(sIconStream);
  63.                   SDest:=TMemoryStream.Create;
  64.                   try
  65.                     sDest.LoadFromFile(fDestFile);
  66.                     i:=0;j:=0;   //以下程序查找目的图标在目的程序中的位置
  67.                     while  i<sDest.size do
  68.                     begin
  69.                          itemp:=i;
  70.                          j:=126;
  71.                          ci:='';cj:='';
  72.                          while (String(ci)=String(cj)) and (i<SDest.size) and (j<icolen) do
  73.                          begin
  74.                                   i:=i+readlen;
  75.                                   j:=j+readlen;
  76.                                   SDest.Position:=i;
  77.                                   SDest.read(ci,readlen);
  78.                                   SiconStream.Position:=j;
  79.                                   SiconStream.Read(cj,readlen);
  80.                          end;
  81.                          if j<icolen then
  82.                               i:=itemp+1  //&#27809;&#25214;&#21040;
  83.                          else
  84.                          begin
  85.                              nPos:=itemp;  //&#25214;&#21040;
  86.                              bOK:=true;
  87.                              break;
  88.                          end;
  89.                     end;
  90.                     if bOK=false then exit;//&#30446;&#26631;&#25991;&#20214;&#20108;&#36827;&#21046;&#30721;&#20013;&#26410;&#25214;&#21040;&#22270;&#26631;
  91.                     SIconStream.Clear;//&#23558;&#28304;&#31243;&#24207;&#22270;&#26631;&#23384;&#20837;
  92.                     SourceIcon.SaveToStream(SIconStream);
  93.                     SIconStream.position:=126;
  94.                     s:=TMemoryStream.Create;
  95.                     try
  96.                         sDest.Position:=0;
  97.                         s.CopyFrom(sDest,nPos);//&#23558;&#30446;&#30340;&#31243;&#24207;&#22270;&#26631;&#21069;&#25968;&#25454;&#25335;&#20837;
  98.                         s.CopyFrom(SIconStream,640); //&#23558;&#28304;&#31243;&#24207;&#22270;&#26631;&#25335;&#20837;
  99.                         if sDest.size>sDest.Position+640 then //&#23558;&#30446;&#30340;&#31243;&#24207;&#21097;&#20313;&#25968;&#25454;&#25335;&#20837;
  100.                         begin
  101.                              sDest.Position:=sDest.Position+640;
  102.                              s.CopyFrom(sDest,sDest.Size-sDest.Position);
  103.                         end;
  104.                         s.SaveToFile(fOutFile);   //&#25913;&#36896;&#22909;&#30340;&#31243;&#24207;&#23384;&#25918;&#22312;OutFile&#25991;&#20214;&#20013;
  105.                     finally
  106.                         S.Free;
  107.                     end;
  108.                   finally
  109.                     sDest.Free;
  110.                   end;
  111.                 finally
  112.                   SIconStream.Free;
  113.                 end;
  114.             finally
  115.                 DestIcon.Free;
  116.             end;
  117.         finally
  118.             SourceIcon.Free;
  119.         end;
  120.     except
  121.         Result:=False;
  122.         Exit;
  123.     end;
  124.     Result:=True;
  125. end;
  126. procedure TexePak.SetDestFile(const Value: string);
  127. begin
  128.   fDestFile := value;
  129. end;
  130. procedure TexePak.SetOutFile(const Value: string);
  131. begin
  132.   fOutFile := Value;
  133. end;
  134. procedure TexePak.SetSourceFile(const Value: string);
  135. begin
  136.   fSourceFile := Value;
  137. end;
  138. end.
  139.