| 12
 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
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 
 | using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
 
namespace Bilb0tSpace
{
	/// <summary>
	/// Description résumée de DirTreeView.
	/// </summary>
	public class DirTreeView : TreeView
	{
		public DirTreeView():base()
		{
			this.ImageList = new ImageList();
			//Event
			this.AfterExpand+=new TreeViewEventHandler(DirTreeView_AfterExpand);
		}
		private static string TempNode = "TEMP_NODE";
 
		public void AddRootNodes()
		{
			//foreach(string drive in Directory.GetLogicalDrives())
			{
				DirectoryInfo di =  new DirectoryInfo(/*drive*/"C:/");
				if(di.Exists)
				{
					TreeNode tn = new TreeNode(/*drive*/"C:/");
 
					ImageList.Images.Add(ExtractIcons.GetIcon(di.FullName,false,ImageList));
					tn.ImageIndex = ImageList.Images.Count-1;
 
					ImageList.Images.Add(ExtractIcons.GetIcon(di.FullName,true,ImageList));
					tn.SelectedImageIndex = ImageList.Images.Count-1;
 
					tn.Tag = di;
					Nodes.Add(tn);
					ExpandDir(tn);
				}
			}
		}
 
		public void ExpandDir(TreeNode tnParent)
		{
			DirectoryInfo di = (DirectoryInfo)tnParent.Tag;
			if(di.Exists)
			{
				foreach(DirectoryInfo diChild in di.GetDirectories())
				{
					TreeNode tn = new TreeNode(diChild.Name);
					tn.Tag = diChild;
 
					ImageList.Images.Add(ExtractIcons.GetIcon(diChild.FullName,false,ImageList));
					tn.ImageIndex = ImageList.Images.Count-1;
 
					ImageList.Images.Add(ExtractIcons.GetIcon(diChild.FullName,true,ImageList));
					tn.SelectedImageIndex = ImageList.Images.Count-1;
 
					tnParent.Nodes.Add(tn);
					if(diChild.GetDirectories().Length > 0)
						tn.Nodes.Add(new TreeNode(TempNode));
				}
			}
		}
 
		private void DirTreeView_AfterExpand(object sender, TreeViewEventArgs e)
		{
			TreeNode tn = e.Node;
			if(tn.Nodes.Count == 1)
			{
				if(tn.Nodes[0].Text == TempNode)
				{
					tn.Nodes.Clear();
					ExpandDir(tn);
				}
			}
		}
	}
 
	#region ExtractIcons Class
 
	public class ExtractIcons
	{
		#region Structs & Enum
 
		[StructLayout(LayoutKind.Sequential)]
			private struct SHFILEINFO
		{
			public SHFILEINFO(bool b)
			{
				hIcon=IntPtr.Zero;iIcon=0;dwAttributes=0;szDisplayName="";szTypeName="";
			}
			public IntPtr hIcon;
			public int iIcon;
			public uint dwAttributes;
			[MarshalAs(UnmanagedType.LPStr, SizeConst=260)]
			public string szDisplayName;
			[MarshalAs(UnmanagedType.LPStr, SizeConst=80)]
			public string szTypeName;
		};
 
		private enum SHGFI
		{
			SHGFI_ICON =             0x000000100,     // get icon
			SHGFI_DISPLAYNAME =      0x000000200,     // get display name
			SHGFI_TYPENAME =         0x000000400,     // get type name
			SHGFI_ATTRIBUTES =       0x000000800,     // get attributes
			SHGFI_ICONLOCATION =     0x000001000,     // get icon location
			SHGFI_EXETYPE =          0x000002000,     // return exe type
			SHGFI_SYSICONINDEX =     0x000004000,     // get system icon index
			SHGFI_LINKOVERLAY =      0x000008000,     // put a link overlay on icon
			SHGFI_SELECTED =         0x000010000,     // show icon in selected state
			SHGFI_ATTR_SPECIFIED =   0x000020000,     // get only specified attributes
			SHGFI_LARGEICON =        0x000000000,     // get large icon
			SHGFI_SMALLICON =        0x000000001,     // get small icon
			SHGFI_OPENICON =         0x000000002,     // get open icon
			SHGFI_SHELLICONSIZE =    0x000000004,     // get shell size icon
			SHGFI_PIDL =             0x000000008,     // pszPath is a pidl
			SHGFI_USEFILEATTRIBUTES = 0x000000010     // use passed dwFileAttribute
		}
 
		#endregion
 
		#region Get Folder Icons
 
		[DllImport("Shell32.dll")]
		private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, 
			out SHFILEINFO psfi, uint cbfileInfo, SHGFI uFlags );
 
		public static Icon GetIcon(string strPath, bool selected, ImageList imageList)
		{
			SHFILEINFO info = new SHFILEINFO(true);
			int cbFileInfo = Marshal.SizeOf(info);
			SHGFI flags;
			if (!selected)
				flags = SHGFI.SHGFI_ICON|SHGFI.SHGFI_SMALLICON;
			else
				flags = SHGFI.SHGFI_ICON|SHGFI.SHGFI_SMALLICON|SHGFI.SHGFI_OPENICON;
 
			SHGetFileInfo(strPath, 256, out info,(uint)cbFileInfo, flags);
			return Icon.FromHandle(info.hIcon);
		}
 
		#endregion
 
		#region Get Desktop Icon
 
		// Retreive the desktop icon from Shell32.dll - it always appears at index 34 in all shell32 versions.
		// This is probably NOT the best way to retreive this icon, but it works - if you have a better way
		// by all means let me know..
 
		//		[DllImport("Shell32.dll", CharSet=CharSet.Auto)]
		//		public static extern IntPtr ExtractIcon(int hInst, string lpszExeFileName, int nIconIndex);
		//
		//		public static Icon GetDesktopIcon()
		//		{
		//			IntPtr i = ExtractIcon(0, Environment.SystemDirectory + "\\shell32.dll", 34);
		//			return Icon.FromHandle(i);
		//		}
 
		// Updated this method in v1.11 so that the icon returned is a small icon, not a large icon as
		// returned by the old method above
 
		[DllImport("Shell32.dll", CharSet=CharSet.Auto)]
		public static extern uint ExtractIconEx(
			string lpszFile, int nIconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, uint nIcons );
 
		public static Icon GetDesktopIcon()
		{
			IntPtr[] handlesIconLarge = new IntPtr[1];
			IntPtr[] handlesIconSmall = new IntPtr[1];
			uint i = ExtractIconEx(Environment.SystemDirectory + "\\shell32.dll", 34, 
				handlesIconLarge, handlesIconSmall, 1);
 
			return Icon.FromHandle(handlesIconSmall[0]);
		}
 
		#endregion
 
	}
 
	#endregion
} | 
Partager