Delphi實(shí)現(xiàn)提取可執(zhí)行文件內(nèi)部所有圖標(biāo)

字號(hào):

本實(shí)例實(shí)現(xiàn)的功能是能夠從用戶選擇的可執(zhí)行文件(后綴名為exe)中提取所有圖標(biāo)并且顯示在窗體上。
    在窗體中添加TImage 組件、TOpenDialog組件和TButton組件,TImage組件充當(dāng)顯示文件內(nèi)圖標(biāo)的容器,TOpenDialog組件和TButton組件用來激活提示用戶選擇GIF動(dòng)畫的對(duì)話框。
    首先在窗體的uses段中添加ShellAPI,然后添加按鈕響應(yīng)代碼如下:
    procedure TfrmMain.btnOpenClick(Sender: TObject);
    var
    i: Integer;
    Large, Small: HICON;
    nIcons: Integer;
    begin
    if OpenDialog1.Execute then
    begin
    self.Image1.Canvas.Refresh;
    nIcons:=ExtractIconEx(PChar(OpenDialog1.FileName), -1, Large, Small, 1);
    for i:=0 to nIcons-1 do
    begin
    ExtractIconEx(PChar(self.OpenDialog1.FileName), i, Large, Small, 1);
    DrawIcon(self.Image1.Canvas.Handle,(i div 4)*40,(i mod 4)*40,Large);
    end;
    end;
    end;
    當(dāng)用戶在程序運(yùn)行過程中選擇一個(gè)可執(zhí)行文件后,程序首先通過self.Image1.Canvas. Refresh語句清除TImage組件上的顯示內(nèi)容,然后通過將ExtractIconEx函數(shù)的第2個(gè)參數(shù)指定為-1來取得可執(zhí)行文件中圖標(biāo)的數(shù)目。得到數(shù)目后,通過一個(gè)循環(huán)中的ExtractIconEx (PChar(self.OpenDialog1.FileName), i, Large, Small, 1)語句把可執(zhí)行文件中的大、小圖標(biāo)分別存儲(chǔ)在Large和Small變量中。最后通過DrawIcon函數(shù)在TImage組件上繪制圖標(biāo)。
    程序代碼如下:
    unit Unit1;
    interface
    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, ShellAPI, Grids, ExtCtrls;
    type
    TfrmMain = class(TForm)
    btnOpen: TButton;
    OpenDialog1: TOpenDialog;
    Image1: TImage;
    procedure btnOpenClick(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;
    var
    frmMain: TfrmMain;
    implementation
    {$R *.dfm}
    procedure TfrmMain.btnOpenClick(Sender: TObject);
    var
    i: Integer;
    Large, Small: HICON;
    nIcons: Integer;
    begin
    if OpenDialog1.Execute then
    begin
    self.Image1.Canvas.Refresh;
    nIcons:=ExtractIconEx(PChar(OpenDialog1.FileName), -1, Large, Small, 1);
    for i:=0 to nIcons-1 do
    begin
    ExtractIconEx(PChar(self.OpenDialog1.FileName), i, Large, Small, 1);
    DrawIcon(self.Image1.Canvas.Handle,(i div 4)*40,(i mod 4)*40,Large);
    end;
    end;
    end;
    end.