Extract Icons from .EXE's and .DLL's
Extract all icons from an .Exe or .Dll file to a PictureBox control, or any
other control with a device context handle (.hdc)
property. Better yet, draw them to a control without an .hdc property
such as an ImageList. This is useful when creating toolbars
where you need to dynamically add icons at run time. I'll show you how to
do that as well! The key API calls used are the DrawIconEx
and ExtractIconEx functions.
Download Source Code
This program uses the VB6 version of the Microsoft Windows Common Controls and
Microsoft Common Dialog Control and will not work under VB5. You can
download the program and open the files with Wordpad or Notepad to view the
source code.
Background:
This program relies on the ExtractIconEx and
DrawIconEx API functions. Drawing an icon in a PictureBox is
easy since it has the device context handle property required by the
DrawIconEx function. The ImageList control, however, does not
have an .hdc property meaning we cannot draw directly to it.
Also shown is how to dynamically populate a ToolBar control at runtime. I
admit, this is a primitive example. See some of my other toolbar projects
for more detailed examples.
Process:
The form load event initializes the ToolBar and PictureBox controls. What
is important here is the size of the PictureBoxes. They must match the
size of the embedded icons for the resultant images to come out right.
Clicking the Browse button clears all Toolbar buttons and removes the ties
between the Toolbars and ImageLists by setting the Toolbars' ImageList
properties to Nothing. Once these controls are unbound, the ImageList
images are cleared and the image size properties set. Again, the size
must match that of the embedded icons. An Open dialog is
displayed to let you select the .exe or .dll to retrieve the icon from.
Once a file is opened the number of icons in the file is determined by calling
ExtractIconEx with the nIconIndex parameter
set to -1. To draw the first icon, ExtractIconEx
is called again. This time nIconIndex is set
to the index of the icon to extract, phiconLarge
and phiconSmall are pointers to arrays of handles
of large and small icons respectively and nIcons is
the number of icons to extract. Onto the PictureBoxes. A simple
call to DrawIconEx draws the icon provided the
PictureBox's AutoReDraw property is True.
Now the icons are added to the ImageLists using the ListImages.Add method.
The secret here is to use the Image property of the PictureBox.
Finally the ImageList is bound to the Toolbar and a button is added via the
button object. Setting the Key field of the ImageList and the
Toolbar controls to the same value tell the ToolBar what image to display for
the new button.
Scrolling through the other icons is just more of the same. Simple, isn't it?
Download the source code and press F5 to run the program. Click the Browse
button to display the Open a dialog box. Point to any executable or dll
file and open it. The First icon will be displayed and added to
the two toolbar controls. Both small (16x16) and large icons (32x32) are
extracted.
If there is more than one icon present, the Next button will be
enabled. Click it to scroll through the rest of the embedded
icons. As you will see, the small and large icons may be very different.
|