IconImageProvider

来源:互联网 发布:送巧克力知乎 编辑:程序博客网 时间:2024/06/12 22:43

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace DropDrapFileSimple
{
    class IconImageProvider
    {

        ImageList _SmallImageList;
        ImageList _LargeImageList;
        Dictionary<string, int> _ImageIndexDict = new Dictionary<string, int>();

        public IconImageProvider(ImageList smallImageList, ImageList largeImageList)
        {
            _SmallImageList = smallImageList;
            _LargeImageList = largeImageList;
        }

        /// <summary>
        /// Get Iamge Index of this filename's Icon
        /// </summary>
        /// <param name="fileName">file name</param>
        /// <returns></returns>
        public int GetIconImageIndex(string fileName)
        {
            String extension = System.IO.Path.GetExtension(fileName).ToLower().Trim();

            int index = -1;

            if (_ImageIndexDict.TryGetValue(extension, out index))
            {
                return index;
            }

            if (_SmallImageList != null)
            {
                _SmallImageList.Images.Add(FileIcon.GetFileIcon(fileName, false));
                index = _SmallImageList.Images.Count - 1;
            }

            if (_LargeImageList != null)
            {
                _LargeImageList.Images.Add(FileIcon.GetFileIcon(fileName, true));
                index = _LargeImageList.Images.Count - 1;
            }

            _ImageIndexDict.Add(extension, index);
            return index;
        }
    }
}

原创粉丝点击