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 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Arnault Nouvel et Antoine Dongois Le processus à prendre : Apprendre (découvrir la plateforme) Préparer (documenter l'historique et choisir la méthode de MAJ) Test (Test de MAJ) Implémenter (Effectuer la MAJ) Valid...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : LA PLEINIèRE DU SECOND JOURTECHDAYS PARIS 2010 : LA PLEINIèRE DU SECOND JOUR par ROMELARD Fabrice
Après un retour sur l'histoire des TechDays de Paris et le fait que ce soit le plus gros event MS au monde (du fait de sa gratuité), le président de MS France (Eric Boustoullier) a fait une présentation de la vision Microsoft pour les années à venir...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|