我正在编写自己的基于C#的应用程序启动器,当我让它填充TreeView
并在其中启动应用程序快捷方式时,我似乎无法弄清楚如何将图标添加为图像到TreeView
。我目前获取文件的代码是:
private void homeMenu_Load(object sender, EventArgs e)
{
this.ShowInTaskbar = false;
if (Directory.Exists((Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName + "\\Roaming\\Launcher")))
{
}
else
{
Directory.CreateDirectory(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName + "\\Roaming\\Launcher");
}
DirectoryInfo launcherFiles = new DirectoryInfo(Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName + "\\Roaming\\Launcher");
lstPrograms.Nodes.Add(CreatingDirectoryTreeNode(launcherFiles));
lstPrograms.Sort();
}
private static TreeNode CreatingDirectoryTreeNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeNode(directoryInfo.Name);
foreach (var directory in directoryInfo.GetDirectories())
{
directoryNode.Nodes.Add(CreatingDirectoryTreeNode(directory));
}
foreach (var file in directoryInfo.GetFiles())
{
directoryNode.Nodes.Add(new TreeNode(file.Name));
}
return directoryNode;
}
我遇到的主要问题是将TreeList的ImageList图标添加到特定节点。我知道我需要补充一下:
lstPrograms.ImageList.Images.Add(Icon.ExtractAssociatedIcon());
实际将图标添加到图像列表中,如何获取该特定图像的索引,然后将其添加到TreeView
及其相关文件?
答案 0 :(得分:13)
首先,将图像添加为资源并定义图像列表:
static ImageList _imageList;
public static ImageList ImageList
{
get
{
if (_imageList == null)
{
_imageList = new ImageList();
_imageList.Images.Add("Applications", Properties.Resources.Image_Applications);
_imageList.Images.Add("Application", Properties.Resources.Image_Application);
}
return _imageList;
}
}
然后,设置ImageList
的<{1}}属性:
TreeView
然后,在创建节点时,对于特定节点,请使用:
treeView1.ImageList = Form1.ImageList;