Support
Joined: 18 Jul 2005 Posts: 731
|
Posted: Tue Jun 24, 2008 8:26 am Post subject: HOWTO : Use icons mode (IconExtractMode.Icons) to specify different icons for items. |
|
|
To use icons mode (IconExtractMode.Icons) to specify icons for items, override the GetIconFileAndIndex and GetIcons methods of the namespace extension.
In the GetIconFileAndIndex method, set the IconExtractMode member to IconExtractMode.Icons. This causes EZNamespaceExtensions to later call the GetIcons method which is used to retrieve the actual icons. The IconFile and IconIndex members must also be set to a value which uniquely identifies the icon (used as a key). Windows Explorer avoids multiple calls to the GetIcons method if the same key is specified.
The following code (adapted from the FileSystemBrowser sample) displays 3 different icons for the folders depending upon length of the display name of the folder.
[C#]
public override void GetIconFileAndIndex(GetIconFileAndIndexEventArgs e)
{
// Causes GetIcons function to be called.
e.IconExtractMode = IconExtractMode.Icons;
//name is the display name of the folder that we will be using in the code
if(name.Length < 5)
{
// iconIndex and iconFile together uniquely identify the icon
e.IconIndex = 0;
e.IconFile = "MyNSE";
}
else if(name.Length < 10)
{
// iconIndex and iconFile together uniquely identify the icon
e.IconIndex = 1;
e.IconFile = "MyNSE";
}
else
{
// iconIndex and iconFile together uniquely identify the icon
e.IconIndex = 2;
e.IconFile = "MyNSE";
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern IntPtr LoadImage(IntPtr hinst, string lpszName,int uType,int cxDesired, int cyDesired,int fuLoad);
public override void GetIcons(GetIconsEventArgs e)
{
int LR_LOADFROMFILE = 0x0010;
int IMAGE_ICON = 1;
if(name.Length < 5)
{
//Set to the desired icon
e.HLargeIcon = LoadImage((IntPtr)0, "C:\\iso.ico", IMAGE_ICON, 0, 0,LR_LOADFROMFILE);
e.HSmallIcon = LoadImage((IntPtr)0, "C:\\iso.ico", IMAGE_ICON, 0, 0,LR_LOADFROMFILE);
}
else if(name.Length < 10)
{
//Set to the desired icon
e.HLargeIcon = LoadImage((IntPtr)0, "C:\\data.ico", IMAGE_ICON, 0, 0,LR_LOADFROMFILE);
e.HSmallIcon = LoadImage((IntPtr)0, "C:\\data.ico", IMAGE_ICON, 0, 0,LR_LOADFROMFILE);
}
else
{
//Set to the desired icon
e.HLargeIcon = LoadImage((IntPtr)0, "C:\\audio.ico", IMAGE_ICON, 0, 0,LR_LOADFROMFILE);
e.HSmallIcon = LoadImage((IntPtr)0, "C:\\audio.ico", IMAGE_ICON, 0, 0,LR_LOADFROMFILE);
}
}
[VB .Net]
Public Overrides Sub GetIconFileAndIndex(ByVal e As GetIconFileAndIndexEventArgs)
' Causes GetIcons function to be called.
e.IconExtractMode = IconExtractMode.Icons
'name is the display name of the folder that we will be using in the code
If (name.Length < 5) Then
'iconIndex and iconFile together uniquely identify the icon
e.IconIndex = 0
e.IconFile = "MyNSE"
ElseIf (name.Length < 10) Then
'iconIndex and iconFile together uniquely identify the icon
e.IconIndex = 1
e.IconFile = "MyNSE"
Else
'iconIndex and iconFile together uniquely identify the icon
e.IconIndex = 2
e.IconFile = "MyNSE"
End If
End Sub
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function LoadImage(ByVal hinst As IntPtr, ByVal lpszName As String, ByVal uType As Integer, ByVal cxDesired As Integer, ByVal cyDesired As Integer, ByVal fuLoad As Integer) As IntPtr
End Function
Public Overrides Sub GetIcons(ByVal e As GetIconsEventArgs)
Dim LR_LOADFROMFILE As Integer = &H10
Dim IMAGE_ICON As Integer = 1
If (name.Length < 5) Then
'Set to the desired icon
e.HLargeIcon = LoadImage(Nothing, "C:\\iso.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE)
e.HSmallIcon = LoadImage(Nothing, "C:\\iso.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE)
ElseIf (name.Length < 10) Then
'Set to the desired icon
e.HLargeIcon = LoadImage(Nothing, "C:\\data.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE)
e.HSmallIcon = LoadImage(Nothing, "C:\\data.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE)
Else
'Set to the desired icon
e.HLargeIcon = LoadImage(Nothing, "C:\\audio.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE)
e.HSmallIcon = LoadImage(Nothing, "C:\\audio.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE)
End If
End Sub |
|