本实例实现的功能是能够从用户选择的可执行文件(后缀名为exe)中提取所有图标并且显示在窗体上。
在窗体中添加TImage 组件、TOpenDialog组件和TButton组件,TImage组件充当显示文件内图标的容器,TOpenDialog组件和TButton组件用来激活提示用户选择GIF动画的对话框。添加组件后的窗体如图1所示。
图1 添加组件后的窗体 |
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; |
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. |
图2 程序运行的初始画面 图3 程序运行结果 |
本文来源:网狐 作者:佚名