This example implements the ability to extract all the icons from the user's chosen executable file (the suffix name exe) and to display it on the form.
Add Timage components, Topendialog components, and TButton components to the form, Timage Components act as containers for icons within the display file, and Topendialog components and TButton components are used to activate the dialog box that prompts the user to select an animated GIF. The form after you add the component is shown in Figure 1.
Figure 1 form after adding a component
First add Shellapi to the uses section of the form, and then add the button response code as follows:
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
When a user selects an executable file while the program is running, the program first passes the self. The Image1.canvas. Refresh statement clears the display on the Timage component and then gets the number of icons in the executable by specifying the 2nd parameter of the Extracticonex function as-one. After the number is obtained, the Extracticonex (Pchar (self) In a loop is passed. Opendialog1.filename), I, Large, Small, 1) statements store large and small icons in the executable file in Large and Small variables respectively. Finally, the DrawIcon function is used to draw an icon on the Timage component.
The program code is as follows:
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.
Save the file, and then press F9 to run the program, the initial picture of the program running as shown in Figure 2.
Click the Open button to pop up a dialog box that prompts the user to select an executable file. Once confirmed, displays the icon stored in the selected executable on the Timge component of the form, as shown in Figure 3.
Figure 2 Initial screen of program operation
Figure 3 Program Run results