method ExFileView.LoadIcon (Icon as Long, IconKey as Long)

Appends a new icon image to control images collection.

TypeDescription
Icon as Long A long expression that indicates the handle of the icon being added.
IconKey as Long A long expression that indicates the icon's key used by the IconIndex property.

Use the LoadIcon to add an icon to the control images collection. Use the LoadIcons method to load multiple icons. The images collection is used to replace the default file/folder's icon. Use the IconIndex property of FileType object to change the file's icon. Use the BrowseFolderPath property to specify the path to the browsed folder.

The following VB sample replaces the default icon for files of BMP and JPG types with the icon:

With ExFileView1
    .LoadIcon LoadPicture("C:\Temp\sample.ico").Handle, 1234
    With .FileTypes.Add("*.bmp *.jpg")
        .IconIndex = 1234
        .Apply
    End With
End With

After running the sample the default icons for BMP and JPG files is changed like:

The following C++ sample replaces the default icon for files of BMP and JPG types:

IPictureDisp* pPicture = NULL;
if ( LoadPicture( "c:\\temp\\sample.ico", &pPicture ) )
{
	OLE_HANDLE hIcon = NULL;
	if ( CComQIPtr<IPicture> spPicture( pPicture ) )
		spPicture->get_Handle( &hIcon );
	m_fileview.LoadIcon( hIcon, 1234 );

	CFileType fileType = m_fileview.GetFileTypes().Add("*.bmp *.jpg");
	fileType.SetIconIndex( 1234 );
	fileType.Apply();
}

where the LoadPicture function loads a picture from a file, and gets the IPictureDisp interface:

#include 
BOOL LoadPicture( LPCTSTR szFileName, IPictureDisp** ppPictureDisp )
{
	BOOL bResult = FALSE;
	if ( szFileName )
	{
		OFSTRUCT of;
		HANDLE hFile = NULL;;
#ifdef _UNICODE
		USES_CONVERSION;
		if ( (hFile = (HANDLE)OpenFile( W2A(szFileName), &of, OF_READ | OF_SHARE_COMPAT)) != (HANDLE)HFILE_ERROR )
#else
		if ( (hFile = (HANDLE)OpenFile( szFileName, &of, OF_READ | OF_SHARE_COMPAT)) != (HANDLE)HFILE_ERROR )
#endif
		{
			*ppPictureDisp = NULL;
			DWORD dwHighWord = NULL, dwSizeLow = GetFileSize( hFile, &dwHighWord );
			DWORD dwFileSize = dwSizeLow;
			HRESULT hResult = NULL;
			if ( HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize) )
				if ( void* pvData = GlobalLock( hGlobal ) )
				{
					DWORD dwReadBytes = NULL;
					BOOL bRead = ReadFile( hFile, pvData, dwFileSize, &dwReadBytes, NULL );
					GlobalUnlock( hGlobal );
					if ( bRead )
					{
						CComPtr spStream;
						_ASSERTE( dwFileSize == dwReadBytes );
						if ( SUCCEEDED( CreateStreamOnHGlobal( hGlobal, TRUE, &spStream) ) )
							if ( SUCCEEDED( hResult = OleLoadPicture( spStream, 0, FALSE, IID_IPictureDisp, (void**)ppPictureDisp ) ) )
								bResult = TRUE;
					}
				}
			CloseHandle( hFile );
		}
	}
	return bResult;
}

The following VB.NET sample replaces the default icon for files of BMP and JPG types:

With AxExFileView1
    Dim spPicture As stdole.IPictureDisp = IPDH.GetIPictureDisp(Image.FromFile("c:\temp\sample.ico"))
    .LoadIcon(spPicture.Handle, 1234)
    With .FileTypes.Add("*.bmp *.jpg")
        .IconIndex = 1234
        .Apply()
    End With
End With

where the IPDH class is defined like follows:

Public Class IPDH
    Inherits System.Windows.Forms.AxHost

    Sub New()
        MyBase.New("")
    End Sub

    Public Shared Function GetIPictureDisp(ByVal image As Image) As Object
        GetIPictureDisp = AxHost.GetIPictureDispFromPicture(image)
    End Function

End Class

The following C# sample replaces the default icon for files of BMP and JPG types:

stdole.IPictureDisp spPicture = IPDH.GetIPictureDisp(Image.FromFile("c:\\temp\\sample.ico")) as stdole.IPictureDisp;
axExFileView1.LoadIcon( spPicture.Handle, 1234);
EXFILEVIEWLib.FileType fileType = axExFileView1.FileTypes.Add("*.bmp *.jpg");
fileType.IconIndex = 1234;
fileType.Apply();

where the IPDH class is defined like follows:

internal class IPDH : System.Windows.Forms.AxHost
{
	public IPDH() : base("")
	{
	}

	public static object GetIPictureDisp(System.Drawing.Image image)
	{
		return System.Windows.Forms.AxHost.GetIPictureDispFromPicture( image );
	}
}

The following VFP sample replaces the default icon for files of BMP and JPG types:

With thisform.ExFileView1
	local i
	with LoadPicture("C:\temp\sample.ico")
		i = .Handle()
	endwith
    .Object.LoadIcon(i, 1234)
    With .FileTypes.Add("*.bmp *.jpg")
        .IconIndex = 1234
        .Apply
    EndWith
EndWith