功能概覽
- 透過 `TOpenDialog` 選擇 `.exe` / `.dll` 檔案。
- 使用 `ExtractIcon` API 列舉並顯示所有內建圖示 (在 `TImage` 上)。
- 使用 `TSaveDialog` 將目前顯示的圖示存為 `.ico` 檔案。
介面元件配置
| 元件 |
Name |
關鍵屬性 |
| TEdit |
Edit1 |
ReadOnly=True (顯示檔案路徑) |
| TButton |
Button1 |
Caption="瀏覽..." / OnClick=Button1Click |
| TButton |
Button2 |
Caption="下一個圖示" / OnClick=Button2Click |
| TButton |
Button3 |
Caption="另存 ICO" / OnClick=Button3Click |
| TImage |
Image1 |
AutoSize=False, Stretch=True, Proportional=True |
| TOpenDialog |
OpenDialog1 |
Filter='可執行檔|*.exe;*.dll|所有檔案|*.*' |
| TSaveDialog |
SaveDialog1 |
Filter='圖示檔|*.ico', DefaultExt='ico' |
核心程式碼 (整理優化版)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Image1: TImage;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
FCurrentFile: string;
FCurrentIndex: Integer;
FIconCount: Integer;
procedure LoadIconList(const FileName: string);
procedure ShowIcon(Index: Integer);
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
Edit1.Text := OpenDialog1.FileName;
LoadIconList(OpenDialog1.FileName);
end;
end;
procedure TForm1.LoadIconList(const FileName: string);
begin
FCurrentFile := FileName;
FCurrentIndex := 0;
// 取得圖示總數 (ExtractIcon 第 3 參數為 $FFFFFFFF = -1)
FIconCount := ExtractIcon(Handle, PChar(FileName), $FFFFFFFF);
if FIconCount = 0 then
begin
ShowMessage('該檔案不包含圖示資源。');
Image1.Picture.Icon.ReleaseHandle;
Exit;
end;
ShowIcon(0);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if FCurrentFile = '' then Exit;
Inc(FCurrentIndex);
if FCurrentIndex >= FIconCount then FCurrentIndex := 0; // 循環
ShowIcon(FCurrentIndex);
end;
procedure TForm1.ShowIcon(Index: Integer);
var
hIcon: HICON;
begin
hIcon := ExtractIcon(Handle, PChar(FCurrentFile), Index);
if hIcon <> 0 then
begin
Image1.Picture.Icon.Handle := hIcon; // TIcon 會取得所有權,銷毀時自動 DestroyIcon
Caption := Format('圖示 %d / %d - %s', [Index + 1, FIconCount, ExtractFileName(FCurrentFile)]);
end
else
ShowMessage('無法提取此索引圖示。');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if SaveDialog1.Execute then
begin
// TImage.Picture.SaveToFile 依副檔名決定格式,.ico 會存為圖示
Image1.Picture.SaveToFile(SaveDialog1.FileName);
ShowMessage('已儲存至 ' + SaveDialog1.FileName);
end;
end;
end.
關鍵技術點
ExtractIcon(Handle, PChar(FileName), Index):
- Index = `$FFFFFFFF` (-1) → 回傳圖示總數 (不提取)。
- Index = 0..Count-1 → 回傳該索引的 **大圖示 (32x32) HICON**。
- 若需小圖示 (16x16),請使用 `ExtractIconEx` 並取 `SmallIcon`。
TImage.Picture.Icon.Handle := hIcon:將 HICON 指派給 `TIcon`,**所有權轉移**,`TIcon` 釋放時會自動呼叫 `DestroyIcon`,無需手動釋放 (但若直接用 `DrawIcon` 則必須手動 `DestroyIcon`)。
TImage.Picture.SaveToFile('xxx.ico'):依副檔名 `.ico` 儲存為圖示格式 (內含多種尺寸/色深的單一 ICO 檔)。
進階:提取所有尺寸並建立標準 ICO (含 16/32/48/256px)
uses ..., PngImage; // 需支援 PNG 壓縮的 ICO (Vista+)
procedure ExtractAllIconsToIco(const ExeFile, IcoFile: string);
var
i, Count: Integer;
Large, Small: HICON;
Icon: TIcon;
Png: TPngImage;
MStream: TMemoryStream;
IconDir: TIconDir; // 自訂結構:ICONDIR + ICONDIRENTRY[] + 各圖片資料
begin
Count := ExtractIconEx(PChar(ExeFile), -1, nil, nil, 0);
// 這裡需手工構建 ICO 檔案結構,包含多個尺寸 (16,32,48,256) 和色深
// 建議使用第三方庫 (如 https://github.com/davidbernard/delphi-ico) 或
// 呼叫 Windows API CreateIconFromResourceEx + WriteIconToStream
// 範例略過完整 ICO 結構寫入代碼 (篇幅較長)
end;
簡易需求下,`TIcon.SaveToFile` 產生的 ICO 僅含單一尺寸 (通常 32x32),足夠大多數桌面捷徑使用。
常見問題
| 問題 |
原因/解決 |
| 圖示顯示空白/黑框 |
ExtractIcon 回傳 0 (索引越界、檔案無圖示、權限不足)。檢查 `FIconCount` 與索引範圍。 |
| 另存 ICO 後透明背景變黑 |
舊版 Delphi `TIcon` 不支援 Alpha 通道。使用 `TPngImage` 轉存或第三方 ICO 庫。 |
| 大量提取導致 GDI 洩漏 |
若直接用 `DrawIcon` 繪製,**必須配對 `DestroyIcon`**;用 `TIcon.Handle :=` 則由 VCL 管理。 |
| 64 位元應用程式提取 32 位元 EXE 圖示 |
ExtractIcon 支援跨位元,但舊版 Delphi 可能需 `Wow64DisableWow64FsRedirection`。現代 Delphi (10.4+) 通常無問題。 |