The new vbAccelerator Site - more VB and .NET Code and Controls
Source Code
3 Code Libraries &nbsp
&nbsp

Icons without forms and document association icons in VB

Using resources rather than VB's weak icon selection to provide better binaries



NOTE: this code has been superceded by the version at the new site.



&nbsp

Nice Icons in the Windows\System directory

Download the Icon Resource Demonstration files (25kb)

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:

  1. To allow the user to pick alternative icons in Explorer's "Change Icon" dialog for shortcuts.
  2. To allow a document icon to be associated with files for your application.
In Win32 applications, you can have as many public icon resources as you want for a binary. The icon used as the icon in explorer for EXEs and DLLs (and any other binary file where the DefaultIcon key in the file association for the file is set to %1 - more on this later) is the icon stored with resource id 0 in the resource file. Since VB allows you to compile resources into your application, you can use this information to set icons for ActiveX binaries which don't have forms. You can also add additional icons to EXEs.

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
0ICONMOVEABLEPRELOADSTIMPY.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:

HKEY_CLASSES_ROOT
.ocx (default) = "ocx_auto_file"
...
ocx_auto_file (default) = "ActiveX Control File"
shell
DefaultIcon (default) = "%1"
open
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.



TopBack to top
Source Code - What We're About!Back to Library Source Code
Source Code - What We're About!Back to Source Code

&nbsp

AboutContributeSend FeedbackPrivacy

Copyright 1998-2000, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 21 March 2000