Icon Handler Interfaces

To create an icon handler, you must implement the IPersistFile interface (described earlier in this chapter) and the IExtractIcon interface. IExtractIcon supports the GetIconLocation and ExtractIcon member functions, in addition to the standard IUnknown functions.

GetIconLocation

This member function retrieves the location of the icon. Normally, this location is an executable or DLL filename, but it can be any file. The function fills in the szIconFile parameter with the fully qualified path of the file that contains the icon, the piIndex parameter with the index to the icon in the file, and the pwFlags parameter with the type of icon. In the following example, the flag is specified as GIL_PERINSTANCE, which means that different files of this type have different icons:

STDMETHODIMP CIconExt::XIconExt::GetIconLocation (UINT
uFlags,
LPSTR szIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
{
METHOD_PROLOGUE (CIconExt, IconExt);
::GetModuleFileName (AfxGetInstanceHandle (), szIconFile, cchMax);
*piIndex = 0;
*pwFlags |= GIL_PERINSTANCE;
return S_OK;
}

Other supported flags include GIL_SIMULATEDOC, which specifies that the icon is the one registered for this file type's document type; and GIL_PERCLASS, which stipulates that icons are the same for all files of this class. (As mentioned earlier, however, don't create an icon handler for all files of a certain class—just update the Registry entry instead.)

ExtractIcon

This member function is called when the interface needs to display an icon that does not reside in an executable or a DLL. When the icon for a file is in a separate ICO file (or any other type of file), the icon handler must extract the icon and return it. Since applications usually have file icons in executables or DLLs, icon handlers can simply implement ExtractIcon as a return-only function that returns S_FALSE:

STDMETHODIMP CIconExt::XIconExt::ExtractIcon (
LPCSTR pszFile, UINT nIconIndex, HICON *phiconLarge,
HICON *phiconSmall, UINT nIconSize)
{
return S_FALSE;
}