提取应用程序图标的Delphi程序代码
2013年10月23日
⁄ 综合
⁄ 共 4891字 ⁄ 字号
小 中 大
- unit Exeico
-
- interface
-
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, ComCtrls,shellapi;
-
- type
- TForm1 = class(TForm)
- Label1: TLabel;
- Edit1: TEdit;
- Label2: TLabel;
- Edit2: TEdit;
- Button1: TButton;
- Button2: TButton;
- OpenDialog1: TOpenDialog;
- OpenDialog2: TOpenDialog;
- StatusBar1: TStatusBar;
- procedure Button2Click(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- private
-
- public
-
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.dfm}
-
- procedure TForm1.Button2Click(Sender: TObject);
- begin
- Close;
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- const
- readlen=10;
- icolen=766;
- var
- i,j,itemp,nPos:int64;
- ci,cj:array[0..readlen-1] of char;
- SourceFile,DestFile:String;
- bOK:boolean;
- SourceIcon,DestIcon:TIcon;
- SIconStream,s,sDest:TMemoryStream;
- begin
- bOK:=false;
- if OpenDialog1.Execute then
- SourceFile:=OpenDialog1.FileName
- else
- exit;
- if AnsiUpperCase(ExtractFileExt(SourceFile))<>'.EXE' then
- begin
- ShowMessage(AnsiUpperCase(ExtractFileExt(SourceFile)));
- exit;
- end;
- Edit1.Text:=SourceFile;
- if OpenDialog2.Execute then
- DestFile:=OpenDialog2.FileName
- else
- exit;
- if AnsiUpperCase(ExtractFileExt(DestFile))<>'.EXE' then
- exit;
- Edit2.Text:=DestFile;
- SourceIcon:=TIcon.Create;
- case ExtractIcon(handle,PChar(SourceFile),UINT(-1)) of
- 0:begin ShowMessage('源程序没有图标');exit;end;
- 1:;
- else ShowMessage('源程序有多个图标,本程序选择第一个图标');
- end;
- SourceIcon.Handle:=ExtractIcon(handle,PChar(SourceFile),0);
- 标
- DestIcon:=TIcon.Create;
- File),N-1)
- case ExtractIcon(handle,PChar(DestFile),UINT(-1)) of
- 0:begin ShowMessage('目的程序没有图标');exit;end;
- 1:;
- else ShowMessage('目的程序有多个图标,本程序选择第一个图标替换');
- end;
- DestIcon.Handle:=ExtractIcon(handle,PChar(DestFile),0);
- SIconStream:=TMemoryStream.Create;
- DestIcon.SaveToStream(sIconStream);
- if sIconStream.size<>icolen then
- ShowMessage('SIcon.size<>icolen');
- SDest:=TMemoryStream.Create;
- sDest.LoadFromFile(DestFile);
- i:=0;j:=0;
- while i< sDest.size do
- begin
- itemp:=i;
- j:=126;
-
- ci:='';cj:='';
- while (String(ci)=String(cj)) and (i<SDest.size) and (j<
- icolen) do
- begin
- i:=i+readlen;
- j:=j+readlen;
- SDest.Position:=i;
- SDest.read(ci,readlen);
- SiconStream.Position:=j;
- SiconStream.Read(cj,readlen);
- end;
- if j<icolen then
- i:=itemp+1
- else
- begin
- nPos:=itemp;
- bOK:=true;
- break;
- end;
- end;
- if bOK=false then
- exit;
- SIconStream.Clear;
- SourceIcon.SaveToStream(SIconStream);
-
- SIconStream.position:=126;
- s:=TMemoryStream.Create;
-
- sDest.Position:=0;
- s.CopyFrom(sDest,nPos);
- s.CopyFrom(SIconStream,640);
- if sDest.size>sDest.Position+640 then
- begin
- sDest.Position:=sDest.Position+640;
- s.CopyFrom(sDest,sDest.Size-sDest.Position);
- end;
- s.SaveToFile(Extractfilepath(application.exename)+'Result.exe');
- SourceIcon.Free;DestIcon.Free;
- SIconStream.Free;s.Free;sDest.Free;
- ShowMessage(Extractfilepath(application.exename)+'Result.exe');
- end;