private System.Collections.ArrayList _imageLists = new ArrayList(); //will hold ImageList objects #endregion Fields #region Constructors /// <summary> /// Creates an instance of <c>IconListManager</c> that will add icons to a single <c>ImageList</c> using the /// specified <c>IconSize</c>. /// </summary> /// <param name="imageList"><c>ImageList</c> to add icons to.</param> /// <param name="iconSize">Size to use (either 32 or 16 pixels).</param> public IconListManager(System.Windows.Forms.ImageList imageList, IconReader.IconSize iconSize) { // Initialise the members of the class that will hold the image list we're // targeting, as well as the icon size (32 or 16) _imageLists.Add(imageList); _iconSize = iconSize; }
/// <summary> /// Internal add a icon to class /// FOLDERS ONLY!!!!!! /// </summary> /// <param name="path">Icon Path on filesystem, or extension</param> /// <param name="iconSize">Icon Size</param> /// <param name="iconProp">Icon Properties to assign to list</param> /// <param name="folder">Folder type (open or closed)</param> private void Add(string path, IconReader.IconSize iconSize, IconProperties iconProp, IconReader.FolderType folder) { iconProp.IconsIndex[iconSize] = -1; iconProp.IconsInfo[iconSize] = new Shell32.SHFILEINFO(); Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO(); iconProp.Icons[iconSize] = IconReader.GetFolderIcon(iconSize, folder, ref shfi); iconProp.IconsInfo[iconSize] = shfi; if (IImageList[iconSize] != null) { iconProp.IconsIndex[iconSize] = IImageList[iconSize].Images.Count; IImageList[iconSize].Images.Add(path, iconProp.Icons[iconSize]); } }
/// <summary> /// Called publicly to add a file's icon to the ImageList. /// </summary> /// <param name="filePath">Full path to the file.</param> /// <returns>Integer of the icon's position in the ImageList</returns> public int AddFileIcon(string filePath) { // Check if the file exists, otherwise, throw exception. if (!System.IO.File.Exists(filePath)) { throw new System.IO.FileNotFoundException("File does not exist"); } // Split it down so we can get the extension string[] splitPath = filePath.Split(new Char[] { '.' }); string extension = (string)splitPath.GetValue(splitPath.GetUpperBound(0)); //Check that we haven't already got the extension, if we have, then //return back its index if (_extensionList.ContainsKey(extension.ToUpper())) { return((int)_extensionList[extension.ToUpper()]); //return existing index } else { // It's not already been added, so add it and record its position. int pos = ((ImageList)_imageLists[0]).Images.Count; //store current count -- new item's index if (ManageBothSizes == true) { //managing two lists, so add it to small first, then large ((ImageList)_imageLists[0]).Images.Add(IconReader.GetFileIcon(filePath, IconReader.IconSize.Small, false)); ((ImageList)_imageLists[1]).Images.Add(IconReader.GetFileIcon(filePath, IconReader.IconSize.Large, false)); } else { //only doing one size, so use IconSize as specified in _iconSize. ((ImageList)_imageLists[0]).Images.Add(IconReader.GetFileIcon(filePath, _iconSize, false)); //add to image list } AddExtension(extension.ToUpper(), pos); // add to hash table return(pos); } }
/// <summary> /// Remove icons information for a especified icon size /// </summary> /// <param name="path">Icon path or extension</param> /// <param name="iconSize">The icon size, support multi size flags</param> /// <param name="removeIconFromList">Did you want remove icon from ImageList? true or false</param> /// <example>Remove(".txt", IconReader.IconSize.Jumbo | IconReader.IconSize.ExtraLarge, true);</example> public bool Remove(string path, IconReader.IconSize iconSize, bool removeIconFromList) { if (!IsValid(path)) return false; Dictionary<IconReader.IconSize, int> removedIcons = IconList[path].Remove(iconSize); if (removeIconFromList) foreach (KeyValuePair<IconReader.IconSize, int> item in removedIcons) IImageList[item.Key].Images.RemoveAt(item.Value); if (!IconList[path].IsValid()) { IconList[path].Dispose(); IconList.Remove(path); } return true; }
/// <summary> /// Retrive the icon index from the ImageList /// </summary> /// <param name="extension">The extension, /// such as ex: ".mp3".</param> /// <param name="iconSize">The icon size.</param> /// <returns>Returns -1 if extension not exist on list, otherwise returns >= 0</returns> public int GetIconIndex(string extension, IconReader.IconSize iconSize, bool check) { if (check) if (!IsValid(extension)) return -1; return IconList[extension].IconsIndex[iconSize]; }
/// <summary> /// Retrive the icon index from the ImageList /// </summary> /// <param name="extension">The extension, /// such as ex: ".mp3".</param> /// <param name="iconSize">The icon size.</param> /// <returns>Returns icon index from ImageList, if not exist will trown an exception</returns> public int GetIconIndex(string extension, IconReader.IconSize iconSize) { return GetIconIndex(extension, iconSize, true); }
/// <summary> /// Util function to Add Folders icons and information to list /// </summary> /// <param name="iconSize">The icon size, support multi size flags</param> public void AddFolder(IconReader.IconSize iconSize) { IconProperties iconPropOpen = new IconProperties(); IconProperties iconPropClosed = new IconProperties(); if ((iconSize & IconReader.IconSize.Small) == IconReader.IconSize.Small) { Add(FolderOpen, IconReader.IconSize.Small, iconPropOpen, IconReader.FolderType.Open); Add(FolderClosed, IconReader.IconSize.Small, iconPropClosed, IconReader.FolderType.Closed); } if ((iconSize & IconReader.IconSize.Large) == IconReader.IconSize.Large) { Add(FolderOpen, IconReader.IconSize.Large, iconPropOpen, IconReader.FolderType.Open); Add(FolderClosed, IconReader.IconSize.Large, iconPropClosed, IconReader.FolderType.Closed); } if ((iconSize & IconReader.IconSize.ExtraLarge) == IconReader.IconSize.ExtraLarge) { Add(FolderOpen, IconReader.IconSize.ExtraLarge, iconPropOpen, IconReader.FolderType.Open); Add(FolderClosed, IconReader.IconSize.ExtraLarge, iconPropClosed, IconReader.FolderType.Closed); } if ((iconSize & IconReader.IconSize.Jumbo) == IconReader.IconSize.Jumbo) { Add(FolderOpen, IconReader.IconSize.Jumbo, iconPropOpen, IconReader.FolderType.Open); Add(FolderClosed, IconReader.IconSize.Jumbo, iconPropClosed, IconReader.FolderType.Closed); } IconList.Add(FolderOpen, iconPropOpen); IconList.Add(FolderClosed, iconPropClosed); }
/// <summary> /// Add an extension to List /// </summary> /// <param name="path">The extension, /// such as ex: ".mp3" or full path "C:\\mymusic.mp3".</param> /// <param name="iconSize">The icon size, support multi size flags</param> /// <returns>Returns Icon and thier information.</returns> public IconProperties AddEx(string path, IconReader.IconSize iconSize) { IconProperties iconProp = IsValidEx(path); if ((iconSize & IconReader.IconSize.Small) == IconReader.IconSize.Small) { if (!iconProp.IsValidEx(IconReader.IconSize.Small)) Add(path, IconReader.IconSize.Small, iconProp); } if ((iconSize & IconReader.IconSize.Large) == IconReader.IconSize.Large) { if (!iconProp.IsValidEx(IconReader.IconSize.Large)) Add(path, IconReader.IconSize.Large, iconProp); } if ((iconSize & IconReader.IconSize.ExtraLarge) == IconReader.IconSize.ExtraLarge) { if (!iconProp.IsValidEx(IconReader.IconSize.ExtraLarge)) Add(path, IconReader.IconSize.ExtraLarge, iconProp); } if ((iconSize & IconReader.IconSize.Jumbo) == IconReader.IconSize.Jumbo) { if (!iconProp.IsValidEx(IconReader.IconSize.Jumbo)) Add(path, IconReader.IconSize.Jumbo, iconProp); } if (!IsValid(path)) IconList.Add(path, iconProp); return iconProp; }
/// <summary> /// Util function to Add Computer drivers icons and information to list /// </summary> /// <param name="iconSize">The icon size, support multi size flags</param> public void AddComputerDrives(IconReader.IconSize iconSize) { foreach (string drive in Directory.GetLogicalDrives()) { AddEx(drive, iconSize); } }
/// <summary> /// Check if class contain a icon /// </summary> public bool IsValid(IconReader.IconSize size) { return Icons.ContainsKey(size); }
/// <summary> /// Remove a especified icon size from class, /// Supports multi sizes flags /// </summary> /// <param name="iconSize">Icon Size to remove, support multi size flags</param> /// <returns>A dictionary with removed icons (size and thier index on ListImage)</returns> public Dictionary<IconReader.IconSize, int> Remove(IconReader.IconSize iconSize) { Dictionary<IconReader.IconSize, int> removedIcons = new Dictionary<IconReader.IconSize, int>(); if ((iconSize & IconReader.IconSize.Small) == IconReader.IconSize.Small) { if (Icons.ContainsKey(IconReader.IconSize.Small)) { if (IconsIndex[IconReader.IconSize.Small] >= 0) removedIcons.Add(IconReader.IconSize.Small, IconsIndex[IconReader.IconSize.Small]); Icons.Remove(IconReader.IconSize.Small); IconsInfo.Remove(IconReader.IconSize.Small); IconsIndex.Remove(IconReader.IconSize.Small); } } if ((iconSize & IconReader.IconSize.Large) == IconReader.IconSize.Large) { if (Icons.ContainsKey(IconReader.IconSize.Large)) { if (IconsIndex[IconReader.IconSize.Large] >= 0) removedIcons.Add(IconReader.IconSize.Large, IconsIndex[IconReader.IconSize.Large]); Icons.Remove(IconReader.IconSize.Large); IconsInfo.Remove(IconReader.IconSize.Large); IconsIndex.Remove(IconReader.IconSize.Large); } } if ((iconSize & IconReader.IconSize.ExtraLarge) == IconReader.IconSize.ExtraLarge) { if (Icons.ContainsKey(IconReader.IconSize.ExtraLarge)) { if (IconsIndex[IconReader.IconSize.ExtraLarge] >= 0) removedIcons.Add(IconReader.IconSize.ExtraLarge, IconsIndex[IconReader.IconSize.ExtraLarge]); Icons.Remove(IconReader.IconSize.ExtraLarge); IconsInfo.Remove(IconReader.IconSize.ExtraLarge); IconsIndex.Remove(IconReader.IconSize.ExtraLarge); } } if ((iconSize & IconReader.IconSize.Jumbo) == IconReader.IconSize.Jumbo) { if (Icons.ContainsKey(IconReader.IconSize.Jumbo)) { if (IconsIndex[IconReader.IconSize.Jumbo] >= 0) removedIcons.Add(IconReader.IconSize.Jumbo, IconsIndex[IconReader.IconSize.Jumbo]); Icons.Remove(IconReader.IconSize.Jumbo); IconsInfo.Remove(IconReader.IconSize.Jumbo); IconsIndex.Remove(IconReader.IconSize.Jumbo); } } return removedIcons; }
/// <summary> /// Check if class contain a icon /// And if that icon is not null /// </summary> /// <param name="size">Icon Size to check</param> public bool IsValidEx(IconReader.IconSize size) { if (!Icons.ContainsKey(size)) return false; return Icons[size] != null; }