| 【C#】LISTVIEW控件:文件/目录增加图标显示实例
					当前位置:点晴教程→知识管理交流
					
					→『 技术文档交流 』
					
				 
 主程序:大图标:列表:详细信息:Form1.cs:1.  public partial class Form1 : Form 2.      { 3.          FileInfoList fileList; 4.    5.    6.          public Form1() 7.          { 8.              InitializeComponent(); 9.          } 10. 11. 12.        private void 加载文件ToolStripMenuItem_Click(object sender, EventArgs e) 13.        { 14.            FolderBrowserDialog dlg = new FolderBrowserDialog(); 15.            if (dlg.ShowDialog() == DialogResult.OK) 16.            { 17.                string[] filespath = Directory.GetFiles(dlg.SelectedPath); 18.                fileList = new FileInfoList(filespath); 19.                InitListView(); 20.            } 21.        } 22. 23. 24.        private void InitListView() 25.        { 26.            listView1.Items.Clear(); 27.            this.listView1.BeginUpdate(); 28.            foreach (FileInfoWithIcon file in fileList.list) 29.            { 30.                ListViewItem item = new ListViewItem(); 31.                item.Text = file.fileInfo.Name.Split('.')[0]; 32.                item.ImageIndex = file.iconIndex; 33.                item.SubItems.Add(file.fileInfo.LastWriteTime.ToString()); 34.                item.SubItems.Add(file.fileInfo.Extension.Replace(".","")); 35.                item.SubItems.Add(string.Format(("{0:N0}"), file.fileInfo.Length)); 36.                listView1.Items.Add(item); 37.            } 38.            listView1.LargeImageList = fileList.imageListLargeIcon; 39.            listView1.SmallImageList = fileList.imageListSmallIcon; 40.            listView1.Show(); 41.            this.listView1.EndUpdate(); 42.        } 43. 44. 45.        private void 大图标ToolStripMenuItem_Click(object sender, EventArgs e) 46.        { 47.            listView1.View = View.LargeIcon; 48.        } 49. 50. 51.        private void 小图标ToolStripMenuItem_Click(object sender, EventArgs e) 52.        { 53.            listView1.View = View.SmallIcon; 54.        } 55. 56. 57.        private void 平铺ToolStripMenuItem_Click(object sender, EventArgs e) 58.        { 59.            listView1.View = View.Tile; 60.        } 61. 62. 63.        private void 列表ToolStripMenuItem_Click(object sender, EventArgs e) 64.        { 65.            listView1.View = View.List; 66.        } 67. 68. 69.        private void 详细信息ToolStripMenuItem_Click(object sender, EventArgs e) 70.        { 71.            listView1.View = View.Details; 72.        } 73. } FileInfoList.cs:说明:主要用于后台数据的存储 1.  class FileInfoList 2.      { 3.          public List<FileInfoWithIcon> list; 4.          public ImageList imageListLargeIcon; 5.          public ImageList imageListSmallIcon; 6.    7.    8.          /// <summary> 9.          /// 根据文件路径获取生成文件信息,并提取文件的图标 10.        /// </summary> 11.        /// <param name="filespath"></param> 12.        public FileInfoList(string[] filespath) 13.        { 14.            list = new List<FileInfoWithIcon>(); 15.            imageListLargeIcon = new ImageList(); 16.            imageListLargeIcon.ImageSize = new Size(32, 32); 17.            imageListSmallIcon = new ImageList(); 18.            imageListSmallIcon.ImageSize = new Size(16, 16); 19.            foreach (string path in filespath) 20.            { 21.                FileInfoWithIcon file = new FileInfoWithIcon(path); 22.                imageListLargeIcon.Images.Add(file.largeIcon); 23.                imageListSmallIcon.Images.Add(file.smallIcon); 24.                file.iconIndex = imageListLargeIcon.Images.Count - 1; 25.                list.Add(file); 26.            } 27.        } 28.    } 29.    class FileInfoWithIcon 30.    { 31.        public FileInfo fileInfo; 32.        public Icon largeIcon; 33.        public Icon smallIcon; 34.        public int iconIndex; 35.        public FileInfoWithIcon(string path) 36.        { 37.            fileInfo = new FileInfo(path); 38.            largeIcon = GetSystemIcon.GetIconByFileName(path, true); 39.            if (largeIcon == null) 40.                largeIcon = GetSystemIcon.GetIconByFileType(Path.GetExtension(path), true); 41. 42. 43.            smallIcon = GetSystemIcon.GetIconByFileName(path, false); 44.            if (smallIcon == null) 45.                smallIcon = GetSystemIcon.GetIconByFileType(Path.GetExtension(path), false); 46.        } 47.    } 
 GetSystemIcon:说明:定义两种图标获取方式,从文件提取和从文件关联的系统资源中提取。 1.  public static class GetSystemIcon 2.      { 3.          /// <summary> 4.          /// 依据文件名读取图标,若指定文件不存在,则返回空值。  5.          /// </summary> 6.          /// <param name="fileName">文件路径</param> 7.          /// <param name="isLarge">是否返回大图标</param> 8.          /// <returns></returns> 9.          public static Icon GetIconByFileName(string fileName, bool isLarge = true) 10.        { 11.            int[] phiconLarge = new int[1]; 12.            int[] phiconSmall = new int[1]; 13.            //文件名 图标索引 14.            Win32.ExtractIconEx(fileName, 0, phiconLarge, phiconSmall, 1); 15.            IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]); 16.            17.            if (IconHnd.ToString() == "0") 18.                return null; 19.            return Icon.FromHandle(IconHnd); 20.        } 21. 22. 23.        /// <summary>  24.        /// 根据文件扩展名(如:.*),返回与之关联的图标。 25.        /// 若不以"."开头则返回文件夹的图标。  26.        /// </summary>  27.        /// <param name="fileType">文件扩展名</param>  28.        /// <param name="isLarge">是否返回大图标</param>  29.        /// <returns></returns>  30.        public static Icon GetIconByFileType(string fileType, bool isLarge) 31.        { 32.            if (fileType == null || fileType.Equals(string.Empty)) return null; 33. 34. 35.            RegistryKey regVersion = null; 36.            string regFileType = null; 37.            string regIconString = null; 38.            string systemDirectory = Environment.SystemDirectory + "\\"; 39. 40. 41.            if (fileType[0] == '.') 42.            { 43.                //读系统注册表中文件类型信息  44.                regVersion = Registry.ClassesRoot.OpenSubKey(fileType, false); 45.                if (regVersion != null) 46.                { 47.                    regFileType = regVersion.GetValue("") as string; 48.                    regVersion.Close(); 49.                    regVersion = Registry.ClassesRoot.OpenSubKey(regFileType + @"\DefaultIcon", false); 50.                    if (regVersion != null) 51.                    { 52.                        regIconString = regVersion.GetValue("") as string; 53.                        regVersion.Close(); 54.                    } 55.                } 56.                if (regIconString == null) 57.                { 58.                    //没有读取到文件类型注册信息,指定为未知文件类型的图标  59.                    regIconString = systemDirectory + "shell32.dll,0"; 60.                } 61.            } 62.            else 63.            { 64.                //直接指定为文件夹图标  65.                regIconString = systemDirectory + "shell32.dll,3"; 66.            } 67.            string[] fileIcon = regIconString.Split(new char[] { ',' }); 68.            if (fileIcon.Length != 2) 69.            { 70.                //系统注册表中注册的标图不能直接提取,则返回可执行文件的通用图标  71.                fileIcon = new string[] { systemDirectory + "shell32.dll", "2" }; 72.            } 73.            Icon resultIcon = null; 74.            try 75.            { 76.                //调用API方法读取图标  77.                int[] phiconLarge = new int[1]; 78.                int[] phiconSmall = new int[1]; 79.                uint count = Win32.ExtractIconEx(fileIcon[0], Int32.Parse(fileIcon[1]), phiconLarge, phiconSmall, 1); 80.                IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]); 81.                resultIcon = Icon.FromHandle(IconHnd); 82.            } 83.            catch { } 84.            return resultIcon; 85.        } 86.    } 87. 88. 89.    /// <summary>  90.    /// 定义调用的API方法  91.    /// </summary>  92.    class Win32 93.    { 94.        [DllImport("shell32.dll")] 95.        public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons); 96. } 该文章在 2021/3/11 1:55:41 编辑过 | 关键字查询 相关文章 正在查询... |