⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iconlistmanager.cs

📁 自己写的可扩展插件文件管理器
💻 CS
字号:
using System;
using System.Collections;
using System.Windows.Forms;

namespace Core.Services
{
    //此类的源代码来自Code Project
    //XML文档已翻译
    //原文链接:http://www.codeproject.com/KB/files/fileicon.aspx
	/// <summary>
	/// 管理一个已加载文件图标的列表
	/// </summary>
	public class IconListManager
	{
		private Hashtable _extensionList = new Hashtable();
		private System.Collections.ArrayList _imageLists = new ArrayList();			//will hold ImageList objects
		private IconReader.IconSize _iconSize;
		bool ManageBothSizes = false; //flag, used to determine whether to create two ImageLists.

		/// <summary>
		/// 创建一个IconListManager的实例并将特定大小的图标加入ImageList
		/// </summary>
		/// <param name="imageList">用于存放图标的ImageList</param>
		/// <param name="iconSize">图标的大小(32x32或16x16)</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>
		/// 实例化一个用于存放两个ImageList的IconListManager
		/// </summary>
		/// <param name="smallImageList">存放小图标的ImageList</param>
        /// <param name="largeImageList">存放大图标的ImageList</param>
		public IconListManager(System.Windows.Forms.ImageList smallImageList, System.Windows.Forms.ImageList largeImageList)
		{
			//add both our image lists
			_imageLists.Add( smallImageList );
			_imageLists.Add( largeImageList );

			//set flag
			ManageBothSizes = true;
		}


		private void AddExtension( string Extension, int ImageListPosition )
		{
			_extensionList.Add( Extension, ImageListPosition );
		}

		/// <summary>
		/// 将图标加入ImageList
		/// </summary>
		/// <param name="filePath">文件的路径</param>
		/// <returns>图标在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[] {'.'});
            System.IO.FileInfo fi=new System.IO.FileInfo(filePath);
			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>
		/// 清空ImageList中的图标
		/// </summary>
		public void ClearLists()
		{
			foreach( ImageList imageList in _imageLists )
			{
				imageList.Images.Clear();	//clear current imagelist.
			}
			
			_extensionList.Clear();			//empty hashtable of entries too.
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -