|3||Code Libraries|| |
Icons without forms and document association icons in VB
Using resources rather than VB's weak icon selection to provide better binaries
this code has been superceded by the version at the new site.
VB allows you to choose an icon for an executable, but it offers a somewhat limited choice. The icon must be stored in a form included in your VB project, and you can only pick one icon to be exposed publicly per project. Whilst the single icon is the most important one, you might want to have additional icons for two reasons:
The first thing to note is that although VB has a resource editor (provided built-in for VB6, available as a Downloadable Add-in for VB5) this does not create public icon resources. To create icon resources which are visible to Explorer, you need to script the resources using the Resource Compiler tool directly.
1) Setting the Explorer Icon for ActiveX Binaries without forms
This is done simply by creating a resource compiler script with the icon you wish to show in explorer at resource ID 0, for example:
// Icons for this application
0 ICON MOVEABLE PRELOAD STIMPY.ICO
Compile this file to a .RES file, add it to your project and compile it. You will see that the project automatically gets the icon in the resource file.
Note that .OCX files are not normally set up to show an icon. To ask Explorer to show an icon extracted from a the file itself, you just set the DefaultIcon registry key to "%1" for the file association. You might want to add a file association for .OCX files to allow you to quickly and easily register and unregister the control in Explorer. Here are the entries you need:
.ocx (default) = "ocx_auto_file"
ocx_auto_file (default) = "ActiveX Control File"
DefaultIcon (default) = "%1"
command (default) = "C:\WINDOWS\SYSTEM\Regsvr32.exe "%1""
unregister (default) = "&Unregister"
command (default) = "C:\WINDOWS\SYSTEM\Regsvr32.exe /u "%1""
Note that you should also make the same entries under the HKEY_LOCAL_MACHINE\SOFTWARE\Classes key.
Having entered these, you will see when you right click on a .OCX file in Explorer you get an Open default option which will call regsvr32 to register the file, and also an Unregister option which allows you to remove the registration entries.
2. To provide alternative icons.
To do this you use the same approach as for the last section except you add icon resources with identifiers other than 0 (numeric or strings). Setting up a document type association to use these icons is just a matter of ensuring the DefaultIcon key gets populated with the executable followed by a comma and the 0 based index of the icon in the executable (remembering that if VB is set to create an icon for you in the executable's properties, this will get priority over any icons in your resource file.) To read about this in more detail, check out the article Passing command line parameters to an existing instance of your app, and making sure only one instance runs.