Listview загрузка сыстемних иконок
#39229496
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
Добрый день, помогите решить проблему я написал вот такой код. Код для загрузки иконок нашел в интернете и переделал его под свой проводник. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141.
public partial class Form1 : Form
{
IntPtr hImgLarge;
System.Drawing.Icon largeIcon;
SHFILEINFO shinfo = new SHFILEINFO();
public struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 256)]
public string szDisplayName;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
[System.Runtime.InteropServices.DllImport("Shell32.dll")]
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, int cbFileInfo, uint uFlags);
public Form1()
{
InitializeComponent();
GetDriveNodes();
}
private void GetDriveNodes()
{
try
{
foreach (string path in Environment.GetLogicalDrives())
if (new DirectoryInfo(path).Exists)
{
TreeNode driveNode = new TreeNode { Text = path };
FillTreeNode(driveNode, path);
treeView1.Nodes.Add(driveNode);
}
}
catch (Exception) { }
}
void treeView1_GetFolders(object sender, TreeViewCancelEventArgs e)
{
try
{
foreach (var dir in Directory.GetDirectories(e.Node.FullPath))
{
TreeNode dirNode = new TreeNode(new DirectoryInfo(dir).Name);
FillTreeNode(dirNode, dir);
e.Node.Nodes.Add(dirNode);
}
}
catch (Exception) { }
}
void treeView1_GetFiles(object sender, TreeViewCancelEventArgs e)
{
try
{
foreach (var dir in Directory.GetFiles(e.Node.FullPath))
{
TreeNode dirNode = new TreeNode(new DirectoryInfo(dir).Name);
FillTreeNode(dirNode, dir);
e.Node.Nodes.Add(dirNode);
}
}
catch (Exception) { }
}
void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
e.Node.Nodes.Clear();
treeView1_GetFolders(sender, e);
treeView1_GetFiles(sender, e);
GetItems(e.Node.FullPath);
}
private void FillTreeNode(TreeNode driveNode, string path)
{
try
{
foreach (string dir in Directory.GetFileSystemEntries(path))
{
TreeNode dirNode = new TreeNode();
driveNode.Nodes.Add(dirNode);
}
}
catch (Exception) { }
}
private void listView1_GetFolders(string[] folders, ImageList largeImageList)
{
try
{
foreach (string f in folders)
{
hImgLarge = SHGetFileInfo(f, 0, ref shinfo, System.Runtime.InteropServices.Marshal.SizeOf(shinfo), 0x100 | 0x0);
largeIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
string[] ss = f.Split(new char[] { '\\' });
largeImageList.Images.Add(ss[ss.Length - 1], largeIcon);
listView1.Items.Add(ss[ss.Length - 1], ss[ss.Length - 1]);
}
}
catch (Exception) { }
}
private void listView1_GetFiles(string[] files, ImageList largeImageList)
{
try
{
foreach (string f in files)
{
hImgLarge = SHGetFileInfo(f, 0, ref shinfo, System.Runtime.InteropServices.Marshal.SizeOf(shinfo), 0x100 | 0x0);
largeIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
string[] ss = f.Split(new char[] { '\\' });
largeImageList.Images.Add(ss[ss.Length - 1], largeIcon);
listView1.Items.Add(ss[ss.Length - 1], ss[ss.Length - 1]);
}
}
catch (Exception) { }
}
private void GetItems(string path)
{
try
{
if (Directory.Exists(path))
{
ImageList largeImageList = new ImageList();
largeImageList.ColorDepth = ColorDepth.Depth32Bit;
largeImageList.ImageSize = new System.Drawing.Size(32, 32);
listView1.LargeImageList = largeImageList;
listView1.Items.Clear();
listView1_GetFolders(Directory.GetDirectories(path), largeImageList);
listView1_GetFiles(Directory.GetFiles(path), largeImageList);
}
}
catch (Exception) { }
}
}
У меня возникла проблема, когда с самого начала я выбираю какой-то диск, подгружает все нормально, но потом, я так понял, берет самую последнею иконку и подставляет под все папки и файлы, не знаю как исправить. Заранее спасибо.
|
|