Boite de dialogue affichant les lecteurs et leurs répertoires (shell treeview)

Description

Dialogue box représentant les lecteurs et leurs répertoires dans une treeview, il sert à séléctionner un répertoire.

Source / Exemple :


// created on 4/10/2002 at 15:49
//*******************************************************//
//						//
//	Shell TreeView sous forme de dialog box		 //
//	Pour tout renseignement:DaViDe_CaPe@hotmail.com	//
//						//
//******************************************************//

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace FolderSelector
{
	public class ShellTreeView	:	Form
	{
		[DllImport("Shell32.dll")]
		public extern static int ExtractIconEx( string nomFich,
							int iconIndex,
							IntPtr[] tabLargeIcon,
							IntPtr[] tabSmallIcon,
							int nbIcons );
		[DllImport("kernel32.dll")]
		public static extern DriveType GetDriveType(string drivename);
		
		public enum DriveType
		{
			Unknown		=	0,
			NoRoot		=	1,
			Removeable	=	2,
			Fixed		=	3,
			Remote		=	4,
			Cdrom		=	5,
			Ramdisk		=	6
		}

		private TreeView tvForm;
		private Button btOK;
		private Button btCancel;
		private ImageList imlist;
		private TextBox tb;

		public string fullPathFolderSelected;

		public ShellTreeView()
		{
			InitializeComponents();
		}

		void InitializeComponents()
		{
			LoadImageList();
			this.SuspendLayout();
			this.MaximizeBox	=	false;
			this.Size	=	new Size( 298, 350 );
			this.FormBorderStyle	=	FormBorderStyle.FixedDialog;
			this.ShowInTaskbar	=	false;
			this.AcceptButton	=	btOK;
			this.CancelButton	=	btCancel;
			this.Text	=	"Selectionnez un répertoire";
		
			tb	=	new TextBox();
			tb.Size	=	new Size( 100, 25 );
			tb.Location	=	new Point( 100, 235 );
			this.Controls.Add( tb );

			btCancel	=	new Button();
			btCancel.DialogResult	=	DialogResult.Cancel;
			btCancel.TabIndex	=	2;
			btCancel.Text		=	"Cancel";
			btCancel.Location	=	new Point(199, 280 );
			btCancel.Click	+=	new EventHandler( ClickCancel );
			this.Controls.Add( btCancel );

			btOK	=	new Button();
			btOK.TabIndex	=	1;
			btOK.Text	=	"OK";
			btOK.Location	=	new Point( 115, 280 );
			btOK.DialogResult	=	DialogResult.OK;
			btOK.Click	+=	new EventHandler( ClickOK );
			this.Controls.Add( btOK );

			tvForm	=	new TreeView();
			tvForm.Size	=	new Size(296, 224);
			tvForm.SelectedImageIndex	=	-1;
			tvForm.TabIndex	=	0;
			tvForm.ImageList	=	imlist;
			tvForm.AfterSelect	+=	new TreeViewEventHandler( TVItemSelected );
			tvForm.AfterExpand	+=	new TreeViewEventHandler( TVExpand );
			this.Controls.Add( tvForm );

			AddDrives();
			if(( this.ShowDialog() ) == DialogResult.Cancel )
				this.fullPathFolderSelected	==	null;

			this.ResumeLayout( false );
		}

		void TVItemSelected( object sender, TreeViewEventArgs e )
		{
			string path	=	e.Node.FullPath;
			tb.Text	=	path;
		}

		void ClickOK( object sender, EventArgs e )
		{
			this.fullPathFolderSelected	=	tvForm.SelectedNode.FullPath;
			this.Close();
		}

		void ClickCancel( object sender, EventArgs e )
		{
			this.Close();
		}

		void AddDrives()
		{
			string[] drives	= Directory.GetLogicalDrives();
			
			foreach( string drive in drives )
			{
			TreeNode node =	new TreeNode( drive);
			
			switch( GetDriveType( drive ) )
			{
			case DriveType.Removeable:
				node.ImageIndex	= node.SelectedImageIndex = 2;
				break;
			case DriveType.Fixed:
				node.ImageIndex = node.SelectedImageIndex = 3;
				break;
			case DriveType.Cdrom:
				node.ImageIndex = node.SelectedImageIndex = 4;
				break;
			default:
				break;
			}
			TreeNode spacenode = new TreeNode(" ");
			node.Nodes.Add( spacenode );
			tvForm.Nodes.Add( node );
			}
		}

		string GetParentString( TreeNode node )
		{
			if( node.Parent ==  null)
				return node.Text;
			else
				return GetParentString( node.Parent) + node.Text +
					(node.Nodes.Count == 0 ? "" : "\\" );
		}
		

		void TVExpand( object sender, TreeViewEventArgs e )
		{
			string path = GetParentString( e.Node );
			e.Node.Nodes.Clear();
			DirectoryInfo	dir	=	new DirectoryInfo( path );
			DirectoryInfo[] dirT=	dir.GetDirectories();
			this.Cursor	=	Cursors.WaitCursor;
		
			foreach( DirectoryInfo folder in dirT )
			{
				string fulldir = folder.FullName;
				FileAttributes attr	=	File.GetAttributes( fulldir);
				if( (attr & FileAttributes.Hidden) == 0)
				{
				TreeNode node	=	new TreeNode( folder.Name );
				node.ImageIndex = 0;
				DirectoryInfo[] subdirT = folder.GetDirectories();
				foreach( DirectoryInfo subfolder in subdirT )
				{
				TreeNode subnode = new TreeNode ( subfolder.Name );
				node.Nodes.Add( subnode );
				}
				e.Node.Nodes.Add( node );
				}
			}
			this.Cursor	=	Cursors.Default;
		}
			
		void LoadImageList()
		{
			imlist	=	new ImageList();
			string syspath	=	Environment.SystemDirectory;
			int n	=	ExtractIconEx( syspath + @"\shell32.dll", 
      -1, null, null, 0);
			IntPtr[]	tabLargeIcon	=	new IntPtr[1];
			IntPtr[]	tabSmallIcon	=	new IntPtr[1];
		
			// dossier fermer ---->	0
			n	=	ExtractIconEx( syspath + @"\shell32.dll", 3, 
      null, tabSmallIcon, 1 );
			Icon ic	=	Icon.FromHandle( tabSmallIcon[0]);
			imlist.Images.Add( ic );

			// dossier ouvert ---->	1
			n	=	ExtractIconEx( syspath + @"\shell32.dll", 4, 
      null, tabSmallIcon, 1 );
			ic	=	Icon.FromHandle( tabSmallIcon[0]);
			imlist.Images.Add( ic );

			// floppy ---->	2
			n	=	ExtractIconEx( syspath + @"\shell32.dll", 6,
      null, tabSmallIcon, 1 );
			ic	=	new Icon.FromHandle( tabSmallIcon[0]);
			imlist.Images.Add( ic );

			// hdd ---->	3
			n	=	ExtractIconEx( syspath + @"\shell32.dll", 8, 
      null, tabSmallIcon, 1 );
			ic	=	Icon.FromHandle( tabSmallIcon[0]);
			imlist.Images.Add( ic );

			// cd rom ---->	4
			n	=	ExtractIconEx( syspath + @"\shell32.dll", 11, 
      null, tabSmallIcon, 1 );
			ic	=	Icon.FromHandle( tabSmallIcon[0]);
			imlist.Images.Add( ic );

			// icone de la boite dialog
			n	=	ExtractIconEx( syspath + @"\shell32.dll", 42, 
      null, tabSmallIcon, 1 );
			ic	=	Icon.FromHandle( tabSmallIcon[0]);
			imlist.Images.Add( ic );

			this.Icon	=	ic;
		}
	}
}

Conclusion :


Le code source a été corrigé il fonctionne maintenant.
Il y a une capture pour voir à quoi cela doit ressembler.
Attention cette classe n'est qu'une boite de dialogue réutilisable dans un projet!
Pas la source d'un executable!

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.