This example shows how to control
icon images drawn by Windows shell.
The ExtractIcon handler is an ActiveX
library like all other Shell Extensions. The first step to be done is
to create a new ActiveX Library. Create it with the following sequence
of operations: open the Repository dialog using File | New | Other...
menu, then select the ActiveX tab and click the ActiveX Library icon.
Fig. 1. New ActiveX Library creation.
Save the created project.
The next step is to add the instance of TSxDataModule
to the project. Every Shell+ component should be placed on SxDataModule
1. This special descendant of TDataModule
supplies several internal methods that allow Shell+ components to safely
operates in multiple Shell threads and automates their registration and
initialization. Note that you can place several Shell+ components onto
the same SxDataModule as well as you can add multiple TSxDataModule instances
to your project. It is easy to add new SxDataModule to the project - just
click corresponding icon in the Repository:
Fig. 2. How to create new SxDataModule instance.
You can save your project now.
|
The next step
is to create the ExtractIcon itself. Shell+ Library contains a special
component that takes care about all ExtractIcon functionality. The
only thing you need to do is to place it on SxDataModule and set its
properties. Select the Shell+ tab in the palette, click the TsxExtractIcon
component and insert it into module. |
Take a look on ExtractIcon properties.
First of all you will see that the new GUID was created and assigned to
the component's CLSID property. It is this GUID will be used by system
to identify your CopyHook handler. Other properties are described in the
table below:
Property Name |
Description |
CLSID
|
GUID, used be system to identify ExtractIcon handler,
created automatically in design time
|
Description
|
Description of the ExtractIcon extension. Enter desired
text here.
|
ExtensionName
|
Short name of the extension, MyExtractIcon for example
|
FileType
|
File type. It is for this type of file Windows will
ask our extension for icon. Use the Object Inspector's lookup to
select appropriate file type or enter it manually.
|
Name
|
Component name, nothing special.
|
DontCache
|
This property controls whether Windows will ask the
icon every time it needs it or icon will be cached. If you need
to draw different icons for the same file type then set this property
to True.
|
IconFileName
IconIndex
IconIndexOpen
|
These properties give you a way to supply icon images
from exe or dll files. IconFileName is the name of file with icons
while IconIndex and IconIndexOpen are two indexes that indicate
what icons to be used. Note that IconIndex property is used in most
cases, the only case when the IconIndexOpen icon is used is the
folder-like shell item that is in opened state.
|
IconLarge
IconLargeOpen
|
Use these properties if you wish to supply your icons
directly with your module. In opposite to the previous properties
icon images will be stored in the dll itself. These icons will be
shown in the "Large Icons" mode.
|
IconSmall
IconSmallOpen
|
These properties are similar to IconLarge and IconLargeOpen
except they will be shown in"Small Icons" mode.
|
OverwriteExistent
|
Only one ExtractIcon handler can be assigned to the
same filetype at the moment. Playing with shell extensions you can
easy override the existing handler. Use this property to control
whether to override an existing icon handler or leave it unchanged.
In addition you can use the OnKeyAlreadyExist event to dynamically
get the previous handler and decide whether to override it.
|
Table 1. TSxExtractIcon component properties.
As you've just seen TSxExtractIcon
supplies several ways to store and handle icon images. In this example
we shell use the ImageList component to store icons. Place it on the form
and add two icons into it. You can use any icons as you wish, the first
one will be used to indicate the empty text document while the second
will represent the document with a text.
Now fill TSxExtractIcon properties as shown below:
Fig. TSxExtractIcon component properties
As it was mentioned above the TSxExtractIcon
component can ask the end user whether to override an existing icon handler.
Let's create a special query form that will be used below to illustrate
this opportunity. This form will be shown at Shell Extension registration
to confirm overwriting of an existing icon handler. Create a new form
and configure it as shown below:
Fig 4. Confirmation query window.
Every time the Shell needs the icon
to be displayed it calls the Shell extension's method to receive it. As
a result the TSxExtractIcon.OnExtractIcon event of is fired in
our example. This event gives you a lot of opportunities: you can return
any icon as you wish depending on file size, contents or any other condition.
In our example the size of file is used as a criteria what icon to be
shown.
Select the Events tab of the Object Inspector
and double click the OnExtractIcon event. Add the following code to the
event handler:
procedure TSxModule1.SxExtractIcon1ExtractIcon(Sender: TObject;
var IconLarge, IconSmall: TIcon);
begin
if FileGetSize((Sender as TSxExtractIcon).FileName)<>0 then
// this icon will be shown for file with contents
ImageList.GetIcon(1,IconLarge)
else
// this one will be used for an empty file
ImageList.GetIcon(0,IconLarge);
end;
|
If another icon-handler extension was registered
for the file type and the OverwriteExistent property was set to True then
the OnKeyAlreadyExists event will be fired. Let's implement its handler
as follows:
procedure TSxModule1.SxExtractIcon1KeyAlreadyExist(RegisteredClass: TGUID;
var Overwrite: Boolean);
begin
Overwrite:=MessageDlg('Extension already registered. Owerwrite?',
mtWarning, [mbYes, mbNo], 0)=mrYes;
if Overwrite then
begin
GUIDWindow:=TGUIDWindow.CreateParented(0);
try
GUIDWindow.Caption:='Store this GUID';
GUIDWindow.ShowModal;
finally
GUIDWindow.Free;
end;
end;
end;
|
Save the project and compile it
- it is ready now! As well as any other ActiveX library the Shell+ project
requires registration. You can register it in several ways:
- from command prompt using MS regsvr32 utility: regsvr32.exe Project1.dll
- from command prompt using Borland's tregsvr utility: tregsvr Project1.dll
To know more about shell extension installation please follow
the link.
Now is a time to test the example. To do it create a new
text document on your desktop: Right click | New | Text Document. Note
that the icon of newly created file is exactly that you've specified for
an empty text document:
To continue the test add a couple of words
to the document and save it. As you will see the file will be displayed
with a new icon. It is this icon was assigned to files with contents:
Don't forget to uninstall the example
when all tests are finished. Note that it is an example only and it will
show its confirmation window until you will not unregister it. As well
as installation the uninstallation can be done in many ways:
- from command prompt using MS regsvr32 utility: regsvr32.exe /u Project1.dll
- from command prompt using Borland's tregsvr utility: tregsvr -u Project1.dll
The detailed information about extension uninstallation
is available here.
Use links below to download source codes of this example
as well as binary files:
Top
|