一个ico文件可以包含多幅 图像 一个exe文件的图标 也是多个。 delphi 的TIcon有缺陷。 ChageExeIcon目前只支持 单帧 的ico文件 另外: The ExtractIcon function retrieves the handle of an icon from the specified executable file, dynamic-link library (DLL), or icon file. HICON ExtractIcon( HINSTANCE hInst, // instance handle LPCTSTR lpszExeFileName, // filename of file with icon UINT nIconIndex // index of icon to extract ); ParametershInstIdentifies the instance of the application calling the function. lpszExeFileNamePoints to a null-terminated string specifying the name of an executable file, DLL, or icon file. nIconIndexSpecifies the index of the icon to retrieve. If this value is 0, the function returns the handle of the first icon in the specified file. If this value is -1, the function returns the total number of icons in the specified file. "value is -1" 但是 他可是 UINT类型的 复件 Project2.exe 就是修改过的多个图标的exe,在大图标和小图标 两种浏览方式 下 完全不一样 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; 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 - 1 do with ResList[I] do begin if IsList then begin if sametext('Icon', ResList[I].Name) or sametext('MAINICON', ResList[I].Name) then LoadResources(List); end else begin ms_size := ResList[I].Size + 22; if NewSize = ms_size then begin ResOffset := ResList[I].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 - 22 do begin NewIcon_ms.Read(b, 1); fs.Write(b, 1); end; except ; end; FreeAndNil(fs); end; except end; FreeAndNil(NewIcon_ms); FreeAndNil(NewIcon);end;
ExeImage 你看看 Program Files\Borland\Delphi6\Demos\ResXplor 就知道了
一个exe文件的图标 也是多个。
delphi 的TIcon有缺陷。
ChageExeIcon目前只支持
单帧 的ico文件
另外:
The ExtractIcon function retrieves the handle of an icon from the specified executable file, dynamic-link library (DLL), or icon file. HICON ExtractIcon( HINSTANCE hInst, // instance handle
LPCTSTR lpszExeFileName, // filename of file with icon
UINT nIconIndex // index of icon to extract
);
ParametershInstIdentifies the instance of the application calling the function. lpszExeFileNamePoints to a null-terminated string specifying the name of an executable file, DLL, or icon file. nIconIndexSpecifies the index of the icon to retrieve. If this value is 0, the function returns the handle of the first icon in the specified file.
If this value is -1, the function returns the total number of icons in the specified file.
"value is -1" 但是 他可是 UINT类型的
复件 Project2.exe 就是修改过的多个图标的exe,在大图标和小图标 两种浏览方式 下 完全不一样
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;
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 - 1 do
with ResList[I] do
begin
if IsList then
begin
if sametext('Icon', ResList[I].Name) or sametext('MAINICON',
ResList[I].Name) then
LoadResources(List);
end
else
begin ms_size := ResList[I].Size + 22;
if NewSize = ms_size then
begin
ResOffset := ResList[I].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 - 22 do
begin
NewIcon_ms.Read(b, 1);
fs.Write(b, 1);
end;
except
;
end;
FreeAndNil(fs);
end;
except
end;
FreeAndNil(NewIcon_ms);
FreeAndNil(NewIcon);end;
你看看 Program Files\Borland\Delphi6\Demos\ResXplor 就知道了