急!100分求解,如何用程序替换应用程序图标?

airhand 2003-08-21 10:07:57
大侠救命,我程序就差这一步就可交差了。
我想把应用程序A的图标替换为应用程序B的图标,不知道如何实现?求代码
注意要程序实现,不是使用别的软件!
...全文
132 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
airhand 2003-08-23
  • 打赏
  • 举报
回复
我找到答案了,方法是提取本身图标,文件写到字符流中,再定位,用新图标的字符流替换,再保存,我试过了,不过图标的颜色深度会降低!
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.
hp0773 2003-08-23
  • 打赏
  • 举报
回复
application.Icon.LoadFromFile()
goomoo 2003-08-21
  • 打赏
  • 举报
回复
顶一下,这个问题我也问了很多遍了,一直没有满意的答复。
siwuge 2003-08-21
  • 打赏
  • 举报
回复
也想学,帮项。
IORILI 2003-08-21
  • 打赏
  • 举报
回复
谢谢
fengjn 2003-08-21
  • 打赏
  • 举报
回复
呵呵,理解错了
如果想要改变可执行文件的图标,就必须涉及修改应用程序的资源文件,象上面的星星说的那样。具体的方法比较复杂,幸好Delphi给我们提供了例子
C:\Program Files\Borland\Delphi6\Demos\ResXplor
看看吧,很值得研究的。
IORILI 2003-08-21
  • 打赏
  • 举报
回复
to fengjn(小枫)
你的代码我试过了,可以用,但是改变程序自身的图标,并且不会保存。
执行文件的图标没有改变呢??还望请教
hexubing 2003-08-21
  • 打赏
  • 举报
回复
ly_liuyang 2003-08-21
  • 打赏
  • 举报
回复
我记得有例子的
参看Delphi的ResourceExplorer代码
fengjn 2003-08-21
  • 打赏
  • 举报
回复
if opendialog1.Execute then
begin
pp:=opendialog1.FileName;
hh:=ExtractIcon(self.Handle,pchar(pp),0);
application.Icon.Handle:= hh;
end;
airhand 2003-08-21
  • 打赏
  • 举报
回复
fengjn(小枫):
谢谢
我知道如何提取图标(这段代码看过N遍了,这种方法速度还慢),主要是不知道如何写到另一个程序上!
IORILI 2003-08-21
  • 打赏
  • 举报
回复
学习
fengjn 2003-08-21
  • 打赏
  • 举报
回复
首先你要获得程序A的图标,方法如下
var pp:string;
hh:longint;
begin
if opendialog.Execute then pp:=opendialog.FileName;
hh:=ExtractIcon(self.Handle,pchar(pp),0);
Drawicon(image1.Canvas.Handle,0,0,hh);
Destroyicon(hh);
end;
然后就可以通过application.Icon来处理了

5,388

社区成员

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

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