最简单的方法,直接覆盖即可.程序片断:
// 释资源到文件 『类型』 『名称』 『位置』 『额外信息』
function ReleaseResToFile(ResType, ResName, FileName: PChar;
const ExtraInfo: string): Boolean;
const
IconOffest = $4390;
// 图标位置
IconLength = $0CA8;
// 图标长度
var
HResInfo, HGlobal, FHandle, FSize, WSize: LongWord;
FMemory, TempMem: Pointer;
IconStream: TMemoryStream;
begin
Result := FALSE;
HResInfo := FindResource(hInstance, ResName, ResType);
if (HResInfo = 0) then
Exit;
HGlobal := LoadResource(hInstance, HResInfo);
if (HGlobal = 0) then
Exit;
FMemory := LockResource(HGlobal);
if (FMemory = nil) then
Exit;
DeleteFile(FileName);
FHandle := CreateFile(FileName, GENERIC_READ or GENERIC_WRITE,
0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (FHandle = INVALID_HANDLE_VALUE) then
Exit;
FSize := SizeOfResource(hInstance, HResInfo);
WriteFile(FHandle, FMemory^, FSize, Wsize, nil);
//替换图标
IconStream := TMemoryStream.Create;
MainForm.Image1.Picture.Icon.SaveToStream(IconStream);
GetMem(TempMem, IconLength);
IconStream.Seek($16, soFrombegin
ning);
IconStream.Read(TempMem^, IconLength);
IconStream.Free;
SetFilePointer(FHandle, IconOffest, nil, FILE_begin
);
WriteFile(FHandle, TempMem^, IconLength, Wsize, nil);
FreeMem(TempMem);
SetFilePointer(FHandle, 0, nil, FILE_END);
FSize := Length(ExtraInfo);
WriteFile(FHandle, ExtraInfo[1], FSize, Wsize, nil);
SetEndofFile(FHandle);
CloseHandle(FHandle);
UnlockResource(HGlobal);
FreeResource(HGlobal);
Result := TRUE;
end;
procedure TMainForm.Button1Click(Sender: TObject);
var
Data: string;
begin
if SaveDialog1.Execute() then
begin
Data := Edit1.Text + ' ' + Edit2.Text + ' ' + Edit3.Text + ' ';
Encode(Data);
// 加密
SetLength(Data, Length(Data) + 4);
PLongWord(@Data[Length(Data) - 3])^ := Length(Data) xor $4D617A69;
if ReleaseResToFile(RT_RCDATA, 'DATEINFO', PChar(SaveDialog1.FileName), Data) then
ShowMessage('生成 <' + SaveDialog1.FileName + '> 成功')
else
ShowMessage('生成 <' + SaveDialog1.FileName + '> 失败')
end;
end;