你先看 D:/Program Files/Borland/Delphi7/Demos/ResXplor
自带的 demo
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, shellapi, ExeImage, rxtypes;
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
OpenDialog2: TOpenDialog;
Button4: TButton;
Label1: TLabel;
procedure Button4Click(Sender: TObject);
private
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure ChageExeIcon(IconFileName, EXEFileName: string);
var
NewIcon: TIcon;
NewSize: integer;
NewIcon_ms: TMemoryStream;
fn: string;
ResOffset,
ms_size: Longint;
t, t2: Longint;
Exe: TExeImage;
ms: TMemoryStream;
fs: TFileStream;
i: integer;
b: byte;
procedure LoadResources(ResList: TResourceList);
var
I: Integer;
begin
for I := 0 to ResList.Count - 1do
with ResListdo
begin
if IsList then
begin
if sametext('Icon', ResList.Name) or sametext('MAINICON',
ResList.Name) then
LoadResources(List);
end
else
begin
ms_size := ResList.Size + 22;
if NewSize = ms_size then
begin
ResOffset := ResList.Offset;
Break;
end;
end;
end;
end;
begin
NewIcon := TIcon.Create;
NewIcon_ms := TMemoryStream.Create;
try
NewIcon.LoadFromFile(IconFileName);
// NewIcon.Handle
NewIcon.SaveToStream(NewIcon_ms);
NewSize := NewIcon_ms.Size;
ms_size := 0;
exe := TExeImage.CreateImage(nil, EXEFileName);
try
LoadResources(exe.Resources);
T := Exe.ResSectHdr.PointerToRawData;
t2 := Exe.ResSectHdr.VirtualAddress;
finally
FreeAndNil(exe);
end;
if ms_size <> NewSize then
begin
showmessage('大小不一致!');
end
else
begin
fs := TFileStream.Create(EXEFileName, fmopenwrite);
try
fs.Position := ResOffset + t - t2;
NewIcon_ms.Position := 22;
for i := 1 to NewSize - 22do
begin
NewIcon_ms.Read(b, 1);
fs.Write(b, 1);
end;
except
;
end;
FreeAndNil(fs);
end;
except
end;
FreeAndNil(NewIcon_ms);
FreeAndNil(NewIcon);
end;
procedure TForm1.Button4Click(Sender: TObject);
var
IconFileName, EXEFileName: string;
begin
if OpenDialog2.Execute then
begin
IconFileName := OpenDialog2.FileName;
end;
if OpenDialog1.Execute then
begin
EXEFileName := OpenDialog1.FileName;
end;
ChageExeIcon(IconFileName, EXEFileName);
end;
end.