【C#】LISTVIEW控件:文件/目錄增加圖標(biāo)顯示實(shí)例
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
主程序:大圖標(biāo):列表:詳細(xì)信息: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 大圖標(biāo)ToolStripMenuItem_Click(object sender, EventArgs e) 46. { 47. listView1.View = View.LargeIcon; 48. } 49. 50. 51. private void 小圖標(biāo)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 詳細(xì)信息ToolStripMenuItem_Click(object sender, EventArgs e) 70. { 71. listView1.View = View.Details; 72. } 73. } FileInfoList.cs:說明:主要用于后臺(tái)數(shù)據(jù)的存儲(chǔ)
1. class FileInfoList 2. { 3. public List<FileInfoWithIcon> list; 4. public ImageList imageListLargeIcon; 5. public ImageList imageListSmallIcon; 6. 7. 8. /// <summary> 9. /// 根據(jù)文件路徑獲取生成文件信息,并提取文件的圖標(biāo) 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:說明:定義兩種圖標(biāo)獲取方式,從文件提取和從文件關(guān)聯(lián)的系統(tǒng)資源中提取。
1. public static class GetSystemIcon 2. { 3. /// <summary> 4. /// 依據(jù)文件名讀取圖標(biāo),若指定文件不存在,則返回空值。 5. /// </summary> 6. /// <param name="fileName">文件路徑</param> 7. /// <param name="isLarge">是否返回大圖標(biāo)</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. //文件名 圖標(biāo)索引 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. /// 根據(jù)文件擴(kuò)展名(如:.*),返回與之關(guān)聯(lián)的圖標(biāo)。 25. /// 若不以"."開頭則返回文件夾的圖標(biāo)。 26. /// </summary> 27. /// <param name="fileType">文件擴(kuò)展名</param> 28. /// <param name="isLarge">是否返回大圖標(biāo)</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. //讀系統(tǒng)注冊(cè)表中文件類型信息 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. //沒有讀取到文件類型注冊(cè)信息,指定為未知文件類型的圖標(biāo) 59. regIconString = systemDirectory + "shell32.dll,0"; 60. } 61. } 62. else 63. { 64. //直接指定為文件夾圖標(biāo) 65. regIconString = systemDirectory + "shell32.dll,3"; 66. } 67. string[] fileIcon = regIconString.Split(new char[] { ',' }); 68. if (fileIcon.Length != 2) 69. { 70. //系統(tǒng)注冊(cè)表中注冊(cè)的標(biāo)圖不能直接提取,則返回可執(zhí)行文件的通用圖標(biāo) 71. fileIcon = new string[] { systemDirectory + "shell32.dll", "2" }; 72. } 73. Icon resultIcon = null; 74. try 75. { 76. //調(diào)用API方法讀取圖標(biāo) 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. /// 定義調(diào)用的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 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |