更改可执行文件(exe)的图标.

chenhui5365 2005-02-25 04:08:59
procedure TForm1.Button1Click(Sender: TObject);
var hExe, hRes, hResLoad, hUpdateRes : THandle;
lpResLock : Pointer;
begin
hExe:=LoadLibrary('c:\890.exe');
hRes:=FindResource(hExe,MAKEINTRESOURCE(1),RT_ICON);
hResLoad:=LoadResource(hExe, hRes);
lpResLock:=LockResource(hResLoad);
hUpdateRes := BeginUpdateResource('c:\899.exe', FALSE);
UpdateResource(hUpdateRes, RT_ICON, MAKEINTRESOURCE(1), LANG_NEUTRAL, lpResLock, SizeOfResource(hExe,hRes));
EndUpdateResource(hUpdateRes, False);
end;
...全文
194 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cym830313 2005-02-26
  • 打赏
  • 举报
回复
接上

procedure TForm1.Prev_IconClick(Sender: TObject); //枚举前一个图标
begin
if not (FileExists(Edit_SourceFile.Text)) or (Icon_Index <= 0) then Exit;
Icon_Index := Icon_Index - 1;
Extract_Icon;
end;

procedure TForm1.Next_IconClick(Sender: TObject);//枚举下一个图标
begin
if not (FileExists(Edit_SourceFile.Text)) then Exit;
Icon_Index := Icon_Index + 1;
Extract_Icon;
end;

procedure TForm1.Extract_Icon;
var
icon_handle: Longint;
buffer: array[0..1024] of Char;
begin
if not (FileExists(Edit_SourceFile.Text)) then Exit;

StrPCopy(Buffer, Edit_SourceFile.Text);
icon_handle := ExtractIcon(self.Handle, buffer, icon_index);

if Icon_Handle = 0 {Did we get a valid handle back?}
then
begin {No}
if Icon_Index = 0 {Is this the first icon in the file?}
then {Yes. There can't be any icons in this file}
begin
Application.MessageBox('这个文件没有发现图标,请重新选择!','信息',MB_ICONINFORMATION+MB_OK);
Image1.Visible := False;
end
else {No. We must have gone beyond the limit. Step back}
Icon_Index := Icon_Index - 1;
Exit;
end;
{We now have our extracted icon. Save it to a temp file in readiness for the modifocation}
Image1.Picture.Icon.Handle := icon_handle;
Image1.Visible := True;
end;

function TForm1.ChangeExeIcon(ExeFile,IconFile:string;Index:Integer=0):Boolean;
var
TempStream,NewIconMemoryStream:TMemoryStream;
OldIconStrings,ExeStrings,ExeIconStrings:TStringStream;
ExeIcon:TIcon;
IconPosition,IconLength,IconHeadLength:Integer;
IconHandle:HICON;
ExeFileStream,IconFileStream:TFileStream;
begin
Result:=False;
IconHeadLength:=126;
if (not FileExists(ExeFile)) or (not FileExists(IconFile)) then Exit;
try
ExeFileStream:=TFileStream.Create(ExeFile,fmOpenReadWrite+fmShareDenyWrite);
ExeStrings:=TStringStream.Create('');
ExeStrings.Position:=0;
ExeFileStream.Position:=0;
ExeStrings.CopyFrom(ExeFileStream,0);
ExeIcon:=TIcon.Create;
IconHandle:=ExtractIcon(Application.Handle,Pchar(ExeFile),Index);
if IconHandle<=1 then
begin
Application.MessageBox('EXE中没有找到该序列的图标!',Pchar(Application.Title),MB_ICONERROR+MB_OK);
Exit;
end;
ExeIcon.Handle:=IconHandle;
ExeIconStrings:=TStringStream.Create('');
ExeIcon.SaveToStream(ExeIconStrings);
ExeIcon.Free;
ExeIcon:=nil;
IconLength:=ExeIconStrings.Size-IconHeadLength;
ExeIconStrings.Position:=IconHeadLength;
OldIconStrings:=TStringStream.Create('');
OldIconStrings.Position:=0;
ExeIconStrings.Position:=IconHeadLength;
OldIconStrings.CopyFrom(ExeIconStrings,IconLength);
ExeIconStrings.Free;
IconPosition:=Pos(OldIconStrings.DataString,ExeStrings.DataString);
ExeStrings.Free;
ExeStrings:=nil;
OldIconStrings.Free;
IconFileStream:=TFileStream.Create(IconFile,fmOpenRead+fmShareDenyNone);
NewIconMemoryStream:=TMemoryStream.Create;
IconFileStream.Position:=IconHeadLength;
NewIconMemoryStream.Position:=0;
NewIconMemoryStream.CopyFrom(IconFileStream,IconFileStream.Size-IconHeadLength);
IconFileStream.Free;
if IconPosition<=0 then
begin
Application.MessageBox('EXE中没有找到该图标的数据!',Pchar(Application.Title),MB_ICONERROR+MB_OK);
Exit;
end;

if IconLength<>NewIconMemoryStream.Size then
begin
TempStream:=TMemoryStream.Create;
ExeFileStream.Position:=IconPosition+IconLength-1;
TempStream.Position:=0;
TempStream.CopyFrom(ExeFileStream,ExeFileStream.Size-ExeFileStream.Position);
ExeFileStream.Position:=IconPosition-1;
NewIconMemoryStream.Position:=0;
ExeFileStream.CopyFrom(NewIconMemoryStream,0);
TempStream.Position:=0;
ExeFileStream.CopyFrom(TempStream,0);
ExeFileStream.Position:=0;
ExeFileStream.Size:=IconPosition+IconLength-1+TempStream.Size;
TempStream.Free;
end
else
begin
ExeFileStream.Position:=IconPosition-1;
NewIconMemoryStream.Position:=0;
ExeFileStream.CopyFrom(NewIconMemoryStream,0);
end;
NewIconMemoryStream.Free;
Result:=True;
finally
ExeFileStream.Free;
end;
end;
end.
运行程序,点"取出图标",选择一个EXE,然后点"保存图标"将其ICO保存为文件.然后点"更换图标",选择我们刚才编译得到的"实验品"和取出
的图标,即可将图标更改掉了.
自定义函数ChangeExeIcon的实现过程如下:先用ExtractIcon将图标释放出来保存为文件,然后将其与EXE比较在该EXE里面找到图标的位置,
然后将新图标的内容覆盖原来的图标.实际上,这个查找过程还不够完美,因为它将两者都转化为TStringStream再比较,如果EXE文件很大的
话是很费内存的.Delphi本身提供了一个例子用来查找位置的,该例子位于Delphi5\Demos\Resxplor下,读者可以结合它来作出高效的图标更
换工具.
cym830313 2005-02-26
  • 打赏
  • 举报
回复
我收藏的一个例子,也给你参考以下吧

三:图标文件ICO格式程序编程
Windows下的可执行文件大多都有一个图标的.如果这个程序是你自己编写的,那么你可以轻松的在编译为EXE之前把图标更换掉.但是如果这个
EXE不是你的呢?怎么办?当然是用工具了.ResHacker(资源黑客)就是一个很好的工具.但是如果想自己编写程序来实现呢?
图标在可执行文件里面实际上是一项资源.Windows提供了一个API函数来取出EXE里面的图标.函数原型为:
HICON ExtractIcon(HINSTANCE hInst,LPCTSTR lpszExeFileName,UINT nIconIndex);其中第一个参数为实例句柄,第二个参数为需要操作的
EXE,DLL,BMP或ICON等包含有图标资源的文件名,第三个参数为需要取出的图标在该EXE里面的索引(因为一个EXE文件里面可能含有多个图标)
.如果这个参数为0,那么将返回第一个图标,如果这个参数为-1,将返回该文件可能含有的所有图标数.如果该文件含有该索引图标,函数将返回
该图标的句柄,否则返回值NULL.
那么,到底我们应该怎么样才能更换一个EXE的图标呢?如果你熟悉PE文件结构的话就很简单了.不过PE文件格式是比较复杂的,讲述它的话要费
很大篇幅.实际上,你可以这样简单的看一个EXE文件的组成:EXE文件=文件头之类+图标资源+文件尾.也就是说,你不用管它的文件头和文件尾
之类,只要找到图标在该EXE里面的位置,然后用你的图标覆盖它即可.
不过需要注意的是,图标是有多种格式的,比如说16X16的16色,32X32的16色,16X16的32色等等.用这种方法更换图标的话必须注意格式要一致
.另外,ExtractIcon函数返回的将是32X32的16色图标.这是个很有趣的地方.也就是说,无论你操作的文件或图标格式是怎么样,它取出的都是
32X32的16色图标.而Delphi默认的那个图标就是这个格式的.
我们打开Delphi,新建一个工程.直接编译后退出.这个得到的EXE我们将用来做"实验品".再新建一个工程,这个才是我们真正要写的程序.往窗
口添加两个名字分别为Next_Icon, Prev_Icon的TSpeedButton.作用是枚举图标的.添加一个Image1用来显示图标.一个名为Edit_SourceFile
的TEdit用来显示选择要取出的EXE之类的文件名称.一个OpenDialog,一个SaveDialog和三个Button.最后记得在Use部分添加ShellApi.全部代
码如下:
unit Unit1;

interface

uses
ShellApi{必须添加此单元}, Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Buttons;

type
TForm1 = class(TForm)
Next_Icon: TSpeedButton;
Prev_Icon: TSpeedButton;
Image1: TImage;
Edit_SourceFile: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
procedure Prev_IconClick(Sender: TObject);
procedure Next_IconClick(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure Extract_Icon;
function ChangeExeIcon(ExeFile,IconFile:string;Index:Integer=0):Boolean;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Icon_Index: integer;
implementation

{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption:='取出图标';
Button2.Caption:='保存图标';
Button3.Caption:='更换图标';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.Filter := '所有支持类型(*.EXE,*.DLL,*.OCX,*.ICL,*.ICO,*.BMP)|*.exe;*.dll;*.ocx;*.icl;*.ico;*.bmp|
所有文件 (*.*)|*.*';
if OpenDialog1.Execute
then
begin
Edit_SourceFile.Text := OpenDialog1.Filename;
Icon_Index := 0;
Extract_Icon;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
SaveDialog1.Filter :='图标文件(*.ICO)|*.ico';
if SaveDialog1.Execute then
begin
if Copy(SaveDialog1.FileName, Length(SaveDialog1.FileName)-3, 1) = '.' then
Image1.Picture.Icon.SaveToFile(SaveDialog1.FileName)
else
Image1.Picture.Icon.SaveToFile(SaveDialog1.FileName + '.ico');
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
ExeFile:String;
begin
OpenDialog1.Filter := 'EXE文件(*.EXE)|*.exe';
OpenDialog1.Title:='请选择需要更换图标的EXE';
if OpenDialog1.Execute then
begin
ExeFile:=OpenDialog1.FileName;
OpenDialog1.Filter := '图标文件(*.ICO)|*.ico';
OpenDialog1.Title:='请选择需要更换的图标文件';
OpenDialog1.FileName:='';{Clear the Old Filename}
if OpenDialog1.Execute then
if ChangeExeIcon(ExeFile,OpenDialog1.FileName) then
Application.MessageBox('更换图标成功!',Pchar(Application.Title),MB_ICONINFORMATION+MB_OK)
else
Application.MessageBox('更换图标失败!',Pchar(Application.Title),MB_ICONERROR+MB_OK)
else
Exit; {Not Select Icon File}
end;
end;

leeshine 2005-02-26
  • 打赏
  • 举报
回复
用ResTools系列工具手工改吧,可以改各种语言编译出来的PE,属软件破解范畴的事!
xx_adam 2005-02-26
  • 打赏
  • 举报
回复
g961681 2005-02-26
  • 打赏
  • 举报
回复
好贴!
Frank.WU 2005-02-26
  • 打赏
  • 举报
回复
可能的原因是替換的圖標的大小與顏色數不一致,導致文件裡面的偏移破壞。
kuki84 2005-02-25
  • 打赏
  • 举报
回复
unit UnitOK;

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
{ Private declarations }
public
{ Public declarations }
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;//32*32图标长度,根据研究前126为图标头,后640为图标数据
var
i,j,itemp,nPos:int64;// nPos为目的图标在目的文件的位置
ci,cj:array[0..readlen-1] of char;
SourceFile,DestFile:String;//如果要把记事本图标换成瑞星杀毒软件图标
bOK:boolean;//则SourceFile='C:\windows\notepad.exe',DestFile:='C:\Progra
m Files\rising\rav\ravmon.exe'
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;//选择第N个图标为 ExtractIcon(handle,PChar(Source
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;
{ repeat
SDest.Position:=i;
sDest.read(ci,Readlen);
SiconStream.Position:=j;
SIconStream.Read(cj,Readlen);
i:=i+Readlen;
j:=j+Readlen;
until (String(ci)=String(cj)) and (i<sDest.size) and (j<icolen);
} 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; //改造好的程序存放在本目录Result.exe文件中
SIconStream.Free;s.Free;sDest.Free;
ShowMessage(Extractfilepath(application.exename)+'Result.exe');
end;
//以上程序可以将目的程序的第一个图标换成源程序的第一个图标,经实证很多程序有二个
//或更多图标,转换原理相似,不在陈述
end.

ly_liuyang 2005-02-25
  • 打赏
  • 举报
回复


for n := 1 to Length(Find_Str.Text)
do
if Zero_Breaks.Checked
then
begin
Find_Buffer[n * 2 - 1] := ORD(Find_Str.Text[n]);
Find_Buffer[n * 2] := 0;
end
else
Find_Buffer[n] := ORD(Find_Str.Text[n]);

if Zero_Breaks.Checked
then
Find_Size := Length(Find_Str.Text) * 2 - 1
else
Find_Size := Length(Find_Str.Text);

if Length(Replace_Str.Text) >= Length(Find_Str.Text)
then
Replace_Str.Text := Copy(Replace_Str.Text, 1, Length(Find_Str.Text));

for n := 1 to Length(Replace_Str.Text)
do
if Zero_Breaks.Checked
then
begin
Rep_Buffer[n * 2 - 1] := ORD(Replace_Str.Text[n]);
Rep_Buffer[n * 2] := 0;
end
else
Rep_Buffer[n] := ORD(Replace_Str.Text[n]);

if Zero_Breaks.Checked
then
Rep_Size := Length(Replace_Str.Text) * 2 - 1
else
Rep_Size := Length(Replace_Str.Text);

if Rep_Size < Find_Size
then
repeat
Rep_Size := Rep_Size + 1;
Rep_Buffer[Rep_Size] := 0;
until Rep_Size = Find_Size;

Modify_File;
end;

procedure TForm1.BrowseClick(Sender: TObject);
begin
OpenDialog1.Filter := 'Executable Files (*.exe)|*.exe|All Files (*.*)|*.*';
OpenDialog1.Filename := EXE_Name.Text;
OpenDialog1.FilterIndex := 0;
if OpenDialog1.Execute
then
begin
EXE_Name.Text := OpenDialog1.Filename;
Icon_Index := 0;
Orig_Ico.Text := '';
Extract_IconClick(Self);
end;
end;

procedure TForm1.Modify_IconClick(Sender: TObject);
var
Temp_Byte: BYTE;
n: INTEGER;
begin
{We can treat an icon replace exactly the same as a text replace. Both are simply}
{streams of bytes, but in the case of icons, the bytes come in from a file rather}
{than have the user type in several hundred values. This routine merely rips the }
{data from the two icon files (original file (what to look for) and the new file }
{(what to replace it with)), sets up the two buffers, and gets the search process}
{kicked off}

if not (FileExists(orig_ico.text))
then
begin
MessageDlg('Icon file "' + orig_ico.text + '" not found', mtWarning, [mbOk], 0);
Exit;
end;
if not (FileExists(new_ico.text))
then
begin
MessageDlg('Icon file "' + new_ico.text + '" not found', mtWarning, [mbOk], 0);
Exit;
end;

AssignFile(data_in, orig_ico.text);
Reset(data_in);
Find_Size := 0;

for n := 1 to 43
do
READ(data_in, temp_byte); {Skip first 43 bytes (Header?)}
while not (EOF(data_in))
do
begin
READ(data_in, temp_byte);
Find_Size := Find_Size + 1;
if Find_Size <= Max_Buffer
then
Find_Buffer[Find_Size] := Temp_Byte;
end;
CloseFile(data_in);

AssignFile(data_in, new_ico.text);
Reset(data_in);
Rep_Size := 0;

for n := 1 to 43
do
READ(data_in, temp_byte); {Skip first 43 bytes (Header?)}

while not (EOF(data_in))
do
begin
READ(data_in, temp_byte);
Rep_Size := Rep_Size + 1;
if Rep_Size <= Max_Buffer
then
Rep_Buffer[Rep_Size] := Temp_Byte;
end;
CloseFile(data_in);
if Rep_Size <> Find_Size
then
begin
MessageDlg('The two icons are not the same size (' + IntToStr(Find_Size) + ' & ' + IntToStr(Rep_Size) + '). Unable to modify',
mtWarning, [mbOk], 0);
Exit;
end;
if (Rep_Size > Max_Buffer) or (Find_Size > Max_Buffer)
then
MessageDlg('The icon data is too large. Only data up to ' + IntToStr(Max_Buffer) + ' bytes can be replaced in a single modifcation',
mtWarning, [mbOk], 0)
else
Modify_File;
end;

procedure TForm1.Browse2Click(Sender: TObject);
begin
OpenDialog1.Filter := 'Icon Files (*.ico)|*.ico|All Files (*.*)|*.*';
OpenDialog1.Filename := Orig_Ico.Text;
OpenDialog1.FilterIndex := 0;
if OpenDialog1.Execute
then
begin
Orig_Ico.Text := OpenDialog1.Filename;
Image1.Picture.LoadFromFile(Orig_Ico.Text);
end
end;

procedure TForm1.Browse3Click(Sender: TObject);
begin
OpenDialog1.Filter := 'Icon Files (*.ico)|*.ico|All Files (*.*)|*.*';
OpenDialog1.Filename := New_Ico.Text;
OpenDialog1.FilterIndex := 0;
if OpenDialog1.Execute
then
begin
New_Ico.Text := OpenDialog1.Filename;
Image2.Picture.LoadFromFile(New_Ico.Text);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Run_Path := ExtractFilePath(ParamStr(0));
if Run_Path[Length(Run_Path)] <> '\'
then
Run_Path := Run_Path + '\';

Label1.Caption := '';
Progress.Visible := FALSE;
Application.Title := Caption;
end;

procedure TForm1.Extract_IconClick(Sender: TObject);
var
icon_handle: LONGINT;
buffer: array[0..1024] of CHAR;
begin
if not (FileExists(EXE_Name.Text))
then
Exit;

StrPCopy(Buffer, EXE_Name.Text);
icon_handle := ExtractIcon(form1.Handle, buffer, icon_index);

if Icon_Handle = 0 {Did we get a valid handle back?}
then
begin {No}
if Icon_Index = 0 {Is this the first icon in the file?}
then {Yes. There can't be any icons in this file}
begin
MessageDlg('No icons found in source file', mtWarning, [mbOk], 0);
Image1.Visible := FALSE;
end
else {No. We must have gone beyond the limit. Step back}
Icon_Index := Icon_Index - 1;
Exit;
end;

{We now have our extracted icon. Save it to a temp file in readiness for the modifocation}
Image1.Picture.Icon.Handle := icon_handle;
Image1.Visible := TRUE;

orig_ico.Text := Run_Path + 'SaveIcon.ico';
end;

procedure TForm1.Next_IconClick(Sender: TObject);
begin
if not (FileExists(EXE_Name.Text))
then
Exit;

Icon_Index := Icon_Index + 1;
Extract_IconClick(Self);
end;

procedure TForm1.Prev_IconClick(Sender: TObject);
begin
if not (FileExists(EXE_Name.Text)) or (Icon_Index <= 0)
then
Exit;

Icon_Index := Icon_Index - 1;
Extract_IconClick(Self);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Image1.Picture.Icon.SaveToFile(orig_ico.Text);
end;

end.

自己参看一下吧
http://lysoft.7u7.net
ly_liuyang 2005-02-25
  • 打赏
  • 举报
回复
呵呵,我以前做的Demo,只可以修改相同Size的Ico的

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls, ShellAPI;

const
Max_Buffer = 4096;

type
Buffer_Type = array[1..Max_Buffer] of BYTE;

TForm1 = class(TForm)
Modify_String: TButton;
Browse: TButton;
Modify_Icon: TButton;
Browse2: TButton;
Browse3: TButton;
Extract_Icon: TButton;
Next_Icon: TButton;
Prev_Icon: TButton;
EXE_Name: TEdit;
OpenDialog1: TOpenDialog;
orig_ico: TEdit;
new_ico: TEdit;
Find_Str: TEdit;
Replace_Str: TEdit;
Zero_Breaks: TCheckBox;
First_Only: TCheckBox;
Progress: TProgressBar;
Label1: TLabel;
Image1: TImage;
Image2: TImage;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Button1: TButton;
procedure Modify_StringClick(Sender: TObject);
procedure BrowseClick(Sender: TObject);
procedure Modify_IconClick(Sender: TObject);
procedure Browse2Click(Sender: TObject);
procedure Browse3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Extract_IconClick(Sender: TObject);
procedure Next_IconClick(Sender: TObject);
procedure Prev_IconClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
procedure Modify_File;
{ Public declarations }
end;

var
Form1: TForm1;
Data_In: file of BYTE;
Data_Out: file of BYTE;
Find_Buffer: Buffer_Type;
Rep_Buffer: Buffer_Type;
Find_Size: INTEGER;
Rep_Size: INTEGER;
Icon_Index: INTEGER;
Run_Path: string;

implementation

{$R *.DFM}

procedure TForm1.Modify_File;
var
temp_byte: BYTE;
n: INTEGER;
Buffer: Buffer_Type;
Count: INTEGER;
Replaced: INTEGER;
Read_Count: LONGINT;
New_Ext: string;
Search_Active: BOOLEAN;
Replace_First: BOOLEAN;
begin
{MessageDlg('Find Buffer: ' + IntToStr(Find_Size) + ' Rep Buffer: ' + IntToStr(Rep_Size), mtInformation, [mbOk], 0);}
if not (FileExists(EXE_Name.text))
then
begin
MessageDlg('Source file "' + orig_ico.text + '" not found', mtWarning, [mbOk], 0);
Exit;
end;

New_Ext := ExtractFileExt(EXE_Name.Text); {Get old ext}
if New_Ext[1] = '.' {Strip off leading '.' (if there is one)}
then
New_Ext := Copy(New_Ext, 2, Length(New_Ext) - 1);
New_Ext := '.~' + Copy(New_Ext, 1, Length(New_Ext) - 1); {Add the leading '~'}

if FileExists(ChangeFileExt(EXE_Name.Text, New_Ext)) {Do we have an existing backup?}
then
DeleteFile(ChangeFileExt(EXE_Name.Text, New_Ext)); {Delete it}
RenameFile(EXE_Name.Text, ChangeFileExt(EXE_Name.Text, New_Ext)); {Create a new backup}

AssignFile(data_in, ChangeFileExt(EXE_Name.Text, New_Ext));
Reset(data_in);
AssignFile(data_out, EXE_Name.Text);
ReWrite(data_out);

Screen.Cursor := crHourglass;
Replaced := 0;
Read_Count := 0;
Progress.Max := FileSize(data_in) div 1024;
Progress.Position := 0;
Progress.Visible := TRUE;
Search_Active := TRUE;
Replace_First := First_Only.Checked;
while not (EOF(data_in))
do
begin
READ(data_in, temp_byte);
Read_Count := Read_Count + 1;
if (temp_byte = Find_Buffer[1]) and Search_Active
then {Does the read byte match the first byte in the buffer?}
begin {Yes. Check to see if the following bytes also match the buffer}
Count := 1;
Buffer[1] := Find_Buffer[1];
while not (EOF(data_in)) and (count < Find_Size) and (Buffer[count] = Find_Buffer[count])
do
begin
count := count + 1;
READ(data_in, Buffer[count]);
Read_Count := Read_Count + 1;
end;

if count = Find_Size {Did what we read, match what we were looking for?}
then
begin
for n := 1 to Count {Yes. Output the replacement data}
do
WRITE(data_out, Rep_Buffer[n]);
Replaced := Replaced + 1;
Search_Active := not (Replace_First);
end
else
for n := 1 to Count {No. Output wthe original data}
do
WRITE(data_out, Buffer[n]);
end
else
WRITE(data_out, temp_byte);

if (Read_Count mod 1024) = 0 {Have we read a K?}
then
begin {Update the progress indicators}
Label1.Caption := 'Read: ' + IntToStr(Read_Count div 1024) + ' KBytes';
Progress.Position := Progress.Position + 1;
Application.ProcessMessages;
end;
end;
CloseFile(data_in);
CloseFile(data_out);
Progress.Position := Progress.Max;
Application.ProcessMessages;
Progress.Visible := FALSE;
Label1.Caption := '';
Screen.Cursor := crDefault;
case Replaced of
0: MessageDlg('Data could not be located. No changes have been made', mtWarning, [mbOk], 0);
1: MessageDlg('Replaced ' + IntToStr(Replaced) + ' instance of the data', mtInformation, [mbOk], 0);
else
MessageDlg('Replaced ' + IntToStr(Replaced) + ' instances of the data', mtInformation, [mbOk], 0);
end;
end;


procedure TForm1.Modify_StringClick(Sender: TObject);
var
n: INTEGER;
begin
if Zero_Breaks.Checked
then
begin
if ((Length(Find_Str.Text) * 2 - 1) > Max_Buffer) or ((Length(Replace_Str.Text) * 2 - 1) > Max_Buffer)
then
begin
MessageDlg('The text data is too large. Only data up to ' + IntToStr(Max_Buffer) + ' bytes can be replaced in a single modifcation',
mtWarning, [mbOk], 0);
Exit;
end;
end
else
begin
if (Length(Find_Str.Text) > Max_Buffer) or (Length(Replace_Str.Text) > Max_Buffer)
then
begin
MessageDlg('The text data is too large. Only data up to ' + IntToStr(Max_Buffer) + ' bytes can be replaced in a single modifcation',
mtWarning, [mbOk], 0);
Exit;
end;
end;
todouwang 2005-02-25
  • 打赏
  • 举报
回复
修改程序后重新编译下看
FigoZhu 2005-02-25
  • 打赏
  • 举报
回复
BeginUpdateResource
EndUpdateResource
是API函数吗?没见过啊!
这样可帮不了你啊
swordxy 2005-02-25
  • 打赏
  • 举报
回复
关注!学习!
chenhui5365 2005-02-25
  • 打赏
  • 举报
回复
程序图标更改成功,但是运行899.exe为什么会提示说不是有效的win32程序.上会这代码是错误的吗?

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧