用UpdateResource修改EXE文件图标(已修正)

 

//请自行添加到 Type 处
PICONDIRENTRY = ^ICONDIRENTRY;
ICONDIRENTRY = packed record
bWidth: Byte;
bHeight: Byte;" A% f% ?( F* f' A1 R
bColorCount: Byte;
bReserved: Byte;! l/ c3 ?6 V7 S8 k, z& B9 t0 n
wPlanes: Word;5 Q4 a+ ~: R* O) v
wBitCount: Word;8 u7 R( K! t) W0 W: W
dwBytesInRes: DWORD;, z# G" A ~; Q6 y; x/ y/ j
dwImageOffset: DWORD;+ @$ I' k9 ~: ?( P7 E
end;5 _: [3 z5 g! v7 [ G
! _5 c6 n; a5 Q- R) _5 o1 r
PICONDIR = ^ICONDIR;
ICONDIR = packed record) h- w1 ]1 g, I4 O
idReserved: Word;/ e: c; s: B5 B4 `
idType: Word;. c2 A# {$ i; ~' _& r3 }1 L9 Y
idCount: Word;
idEntries: ICONDIRENTRY;
end;

PGRPICONDIRENTRY = ^GRPICONDIRENTRY;# ^) H; S; q: g
GRPICONDIRENTRY = packed record; ^' }6 ~6 n4 d: t
bWidth: Byte;
bHeight: Byte;) h; A0 [% V0 q
bColorCount: Byte;
bReserved: Byte;4 K4 U& c, J7 q X4 s
wPlanes: Word;3 i; k) U) Z! u
wBitCount: Word;# r, o y, N* a" Y# z x
dwBytesInRes: DWORD;4 V6 l; |, p. X2 f% V9 i+ l: \
nID: Word;
end;

PGRPICONDIR = ^GRPICONDIR;5 n% _. G/ t& {% H% o0 [
GRPICONDIR = packed record
idReserved: Word;
idType: Word;
idCount: Word;
idEntries: GRPICONDIRENTRY; ) w+ y$ y- G) V1 E+ N
end;

////////////////////////////////////////////// P1 Y, {4 h0 O# a6 _8 O# _3 v
//函数说明:修改EXE图标
//! u% R! F5 H8 W. e- d& @
//参 数:IconFile 图标文件
// ExeFile 被修改的EXE文件
//
//返回值: 成功为True,否则False. R& @9 g! e6 [# a0 }$ w
/////////////////////////////////////////////5 f6 G2 i$ ?- B$ Q. l; s0 v
function ChangeExeIcon(IcoFile, ExeFile: string): Boolean;: s/ ^3 n- e# }) S+ e
var0 B) [6 h* t' n6 k- P
stID: ICONDIR;/ d& i! e# e* H& w% b: D* Y4 H: m4 C
stGID: GRPICONDIR;
/ Z( Z6 g. M& s/ A7 T7 P
pGrpIcon: PBYTE;
pIcon: PBYTE;0 `" i" F2 V2 \% L
hUpdate: DWORD;
nSize, nGSize: DWORD;
hFile: THandle;9 q6 f& q& e' X" W7 i
dwReserved: DWORD; * i' i) M/ C9 B3 g' B
ret: Boolean;
begin
Result := False;

hFile := CreateFile(PChar(IcoFile), GENERIC_READ, 0, nil, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);/ {$ E8 r* K. K/ o7 s4 \$ F' l+ [
if hFile = INVALID_HANDLE_VALUE then
Exit;
& y9 T; e' {5 `: a
try
ReadFile(hFile, stID, Sizeof(ICONDIR), dwReserved, nil);+ N; B: v" Z4 T C, v

nSize := stID.idEntries.dwBytesInRes;, I, W: y+ }5 P* I7 p
GetMem(pIcon, nSize);
SetFilePointer(hFile, stID.idEntries.dwImageOffset, nil, FILE_BEGIN);" `! C9 q' j& ^ p
ReadFile(hFile, pIcon^, nSize, dwReserved, nil);# C5 ~. v. Q; p1 s4 _
6 q8 K) t! W/ J# Z5 L
stGID.idType := 1;
stGID.idCount := stID.idCount;
stGID.idReserved := 0;$ D9 T$ j3 A' M7 v1 ~
CopyMemory(@stGID.idEntries.bWidth, @stID.idEntries.bWidth, 12);
stGID.idEntries.nID := 0;

nGSize := Sizeof(GRPICONDIR);2 Z5 l8 d( s0 h6 Y8 N4 A
GetMem(pGrpIcon, nGSize);5 N. a& g t \
CopyMemory(pGrpIcon, @stGID, nGSize);) n: T! u4 X8 L3 Z# G
. y, i% c6 \' n
hUpdate := BeginUpdateResource(PChar(ExeFile), False);( T% w! ~% l, {7 {' S: X9 K( F
try
ret := UpdateResource(hUpdate, RT_GROUP_ICON, MAKEINTRESOURCE(1), 0, pGrpIcon, nGSize);
ret := UpdateResource(hUpdate, RT_ICON, MAKEINTRESOURCE(1), 0, pIcon, nSize);
finally" j: g7 x4 [* W& n$ t% \
EndUpdateResource(hUpdate, False);
end;3 b2 [) v* s3 F- R: l' T+ O* P/ @2 I& ^
finally$ W# B, i4 L% N" \" ?. }3 ]9 M
CloseHandle(hFile);
end;3 W: l! f( P7 Z/ I6 r

Result := ret; w% Q: d) |4 u: ^. h: U) H
end;

你可能感兴趣的:(resource)