Accueil > > > BOITE DE DIALOGUE AFFICHANT LES LECTEURS ET LEURS RÉPERTOIRES (SHELL TREEVIEW)
BOITE DE DIALOGUE AFFICHANT LES LECTEURS ET LEURS RÉPERTOIRES (SHELL TREEVIEW)
Information sur la source
Description
Dialogue box représentant les lecteurs et leurs répertoires dans une treeview, il sert à séléctionner un répertoire.
Source
- // 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;
- }
- }
- }
-
// 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!
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Ouvrir une boite de dialogue pour configurer un objet avant de le placer sur une form [ par Arthenius ]
Hello tout le monde...Voila je souhaiterais créer des objets que je pourrait intégrer à la boite à outils de vs...j'ai déja créer un certain nombre de
Boite de dialogue rechercher [ par tahiti_bob ]
Bonjour,Je suis en train de réaliser un éditeur type bloc note et actuellement, en train d'implémenter la fonction rechercher/remp
Focus [ par miguelitoX ]
je developpe sur visual studio 2003 en C# et j'ai un probleme dans l'IHM. J'ai des boites de dialogue qui attendent des selections dans une TreeView
Boite de dialogue [ par Pepin21 ]
Bonjour,J'ai une question vraiment très bête, mais je ne mis connais pas encore bien. Voilà : je voudrais utiliser une boite de dialogu
Probleme de boite de dialogue [ par tiju50 ]
J'ai commencé à créer un Windows Form avec un petit menu et tout et tout... et j'aurais voulu en cliquant dans un champ du menu que ca ouvre une boite
boite de dialogue [ par xelr90 ]
Bonour,Je suis un débutant dans la programmation, j'utilise SharpDevelop, je n'y connai presque rien et je souhaite afficher une boite de dilogueLe co
Boite de dialogue OUI/NON ? [ par charles84 ]
Bonjour,Je cherche comment créer une boite de dialogue OUI / NON en C#. Existe t-il une fonction deja toute faite <br /
VC# Express Boite de dialogue à la compilation [ par tahiti_bob ]
Bonsoir,Lorsqu'on lance le débogage d'un projet à partir de Visual C# Express et que le code contient une ou plusieurs erreurs, une boîte de dialogue
créer une boite de dialogue [ par uzu ]
bonjour!j'ai un MenuStrip Fichier, Edition ....... ( dans Form1)et dans le menu Fichier; il y a sous menu Ouvrir et Créer;en cliquant sur ce sous menu
boite de dialogue [ par ddove53 ]
Salut, j'ai une boite de dialogue: Si mondialogresult.OK la boite de dialog reste si mondialogresult.NO la boite de dialogue, j'ai un messagebox
|
Derniers Blogs
TECHDAYS PARIS 2012 : COMMENT SHAREPOINT A SAUVé MES TECHDAYSTECHDAYS PARIS 2012 : COMMENT SHAREPOINT A SAUVé MES TECHDAYS par ROMELARD Fabrice
Speakers : Lionel Limozin et Alain Marty La session commence par une découverte de SharePoint à travers la mise en place d'un environnement SharePoint pour la gestion des Sessions animées par BeWise. Le besoin est très ba...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice PERSPECTIVE 3.0 POUR SILVERLIGHT 5.0PERSPECTIVE 3.0 POUR SILVERLIGHT 5.0 par odewit
Je viens de publier la version 3.0 de Perspective pour Silverlight, qui regroupe un portage sous Silverlight 5.0 des fonctionnalités de Perspective 2.0, le framework 3D de haut-niveau introduit récemment et de nouveaux exemples de code. En voici la li...
Cliquez pour lire la suite de l'article par odewit TECHDAYS PARIS 2012 : TOP 10 DES BEST PRACTICES POUR SQL SERVERTECHDAYS PARIS 2012 : TOP 10 DES BEST PRACTICES POUR SQL SERVER par ROMELARD Fabrice
Speaker : Nadia Ben El Kadi Configuration machine La session commence par la toute première question à se poser lors de la mise en place d'environnement SQL Server, la configuration des machines : Type de mac...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : KINECT + OFFICE 365 UN BON GESTE POUR VOTRE SITECHDAYS PARIS 2012 : KINECT + OFFICE 365 UN BON GESTE POUR VOTRE SI par ROMELARD Fabrice
Speakers : Fabrice Barbin, Samuel Blanchard, Julien Lo Presti Titre Prometteur et attractif invitant à voir comment lier le composant ludique Kinect dans le cadre d'une structure IT classique, notamment au travers de la plat...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : PLEINIèRE DU PREMIER JOURTECHDAYS PARIS 2012 : PLEINIèRE DU PREMIER JOUR par ROMELARD Fabrice
KeyNotes du premier jour pour les développeurs. La session est principalement axée sur une des principales directions prise par Microsoft à travers tous ses nouveaux produits : Cloud privé ou public (Solution Azure) ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|