|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
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
Sources en rapport avec celle ci
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
|
Téléchargements
Logiciels à télécharger sur le même thème :
|