PE文件的图标存储在资源文件中,而操作资源要用到的API函数就是UpdateResource
首先我们需要先了解一下ICO格式,参考资料:http://www.moon-soft.com/program/FORMAT/windows/icons.htm
ICO格式不复杂,就是由数据头、数据目录、数据三个部分组成
一个.ico文件中可能含有若干个图标,我们需要将数据目录和数据解析出来。简单地写了个单元
- unit Icons;
- interface
- uses
- Winapi.Windows, System.SysUtils, System.Classes;
- type
- { 用于ICO图标文件 }
- TIconDirEntry = packed record
- bWidth: Byte;
- bHeight: Byte;
- bColorCount: Byte;
- bReserved: Byte;
- wPlanes: Word;
- wBitCount: Word;
- dwBytesInRes: DWORD;
- dwImageOffset: DWORD;
- end;
- PIconDirEntry = ^TIconDirEntry;
- TIconDir = packed record
- idReserved: Word;
- idType: Word;
- idCount: Word;
- idEntries: array [0..0] of TIconDirEntry;
- end;
- PIconDir = ^TIconDir;
- { 用于PE文件中的图标 }
- TGroupIconDirEntry = packed record
- bWidth: Byte;
- bHeight: Byte;
- bColorCount: Byte;
- bReserved: Byte;
- wPlanes: Word;
- wBitCount: Word;
- dwBytesInRes: DWORD;
- nID: Word;
- end;
- TGroupIconDir = packed record
- idReserved: Word;
- idType: Word;
- idCount: Word;
- idEntries: array [0 .. 0] of TGroupIconDirEntry;
- end;
- PGroupIconDir = ^TGroupIconDir;
- { ICO图标文件 }
- TIcoFile = class
- public
- IconStream: TMemoryStream;
- IconDir: PIconDir;
- IconDirSize: DWORD;
- constructor Create; overload;
- constructor Create(const FileName: string); overload;
- destructor Destroy; override;
- // 加载ICO数据
- procedure LoadFromFile(const FileName: string);
- procedure LoadFromStream(Stream: TStream);
- end;
- implementation
- { TIcoFile }
- constructor TIcoFile.Create;
- begin
- IconStream := TMemoryStream.Create;
- IconDir := nil;
- IconDirSize := 0;
- end;
- constructor TIcoFile.Create(const FileName: string);
- begin
- Create;
- LoadFromFile(FileName);
- end;
- destructor TIcoFile.Destroy;
- begin
- FreeMem(IconDir);
- FreeAndNil(IconStream);
- inherited;
- end;
- procedure TIcoFile.LoadFromFile(const FileName: string);
- var
- MS: TMemoryStream;
- begin
- MS := TMemoryStream.Create;
- try
- MS.LoadFromFile(FileName);
- LoadFromStream(MS);
- finally
- FreeAndNil(MS);
- end;
- end;
- procedure TIcoFile.LoadFromStream(Stream: TStream);
- var
- Dir: TIconDir;
- begin
- Stream.Position := 0;
- IconStream.Clear;
- IconStream.CopyFrom(Stream, Stream.Size);
- IconStream.Position := 0;
- IconStream.ReadBuffer(Dir, SizeOf(Dir));
- FreeMem(IconDir);
- IconDirSize := SizeOf(TIconDirEntry) * (Dir.idCount - 1) + SizeOf(TIconDir);
- IconDir := AllocMem(IconDirSize);
- IconStream.Position := 0;
- IconStream.ReadBuffer(IconDir^, IconDirSize);
- end;
- end.
这里要注意一个问题,ICO文件中的TIconDirEntry结构和PE文件中的TGroupIconDirEntry结构是不同的
不同处在最后一个参数,ICO文件中表示的是图像数据偏移地址,是DWORD类型。而PE文件中表示的是图像数据的索引,是WORD类型,少了2个字节
替换图标需要写入2个部分:RT_GROUP_ICON 和 RT_ICON
RT_GROUP_ICON也就是ICO的数据头部分,RT_ICON就是图像数据部分了
数据部分直接写入即可,不用转换什么的。但是数据头需要稍微处理一下,因为前面说了,这个替换过程是把ICO文件写到PE文件中
而他们数据头部分结构略有不同(PE文件中每个数据头比起ICO数据头要少2字节),只用把这里处理下就行了
最后写个函数就可以替换PE文件图标了
- function TMainForm.UpdatePeIcon(IcoFile, PeFile: string): Boolean;
- var
- Ico: TIcoFile;
- I: Integer;
- hRes: THandle;
- GroupIconDir: PGroupIconDir;
- GroupIconDirSize: DWORD;
- Data: TBytes;
- begin
- Result := False;
- hRes := BeginUpdateResource(PChar(PeFile), False);
- if hRes <> 0 then
- begin
- Ico := TIcoFile.Create(IcoFile);
- // TGroupIconDirEntry结构要比TIconDirEntry结构少2字节
- GroupIconDirSize := Ico.IconDirSize - Ico.IconDir^.idCount * 2;
- GroupIconDir := AllocMem(GroupIconDirSize);
- GroupIconDir^.idReserved := 0;
- GroupIconDir^.idType := 1;
- GroupIconDir^.idCount := Ico.IconDir.idCount;
- for I := 0 to Ico.IconDir.idCount - 1 do
- begin
- CopyMemory(@GroupIconDir^.idEntries[I], @Ico.IconDir^.idEntries[I],
- SizeOf(TGroupIconDirEntry));
- // 索引从1开始
- GroupIconDir^.idEntries[I].nID := I + 1;
- // 写入图标数据
- SetLength(Data, Ico.IconDir^.idEntries[I].dwBytesInRes);
- Ico.IconStream.Position := Ico.IconDir^.idEntries[I].dwImageOffset;
- Ico.IconStream.ReadBuffer(Data[0], Length(Data));
- UpdateResource(hRes, RT_ICON, MakeIntResource(I + 1), 0, @Data[0], Length(Data));
- end;
- // 写入 RT_GROUP_ICON
- UpdateResource(hRes, RT_GROUP_ICON, MakeIntResource('MAINICON'), 0, GroupIconDir, GroupIconDirSize);
- FreeMem(GroupIconDir);
- FreeAndNil(Ico);
- EndUpdateResource(hRes, False);
- Result := True;
- end;
- end;
http://blog.csdn.net/aqtata/article/details/7710720