怎麼秀出系統的Folder 類似Delphi的ShellTreeView? |
尚未結案
|
lcsboy
版主 發表:87 回覆:622 積分:394 註冊:2002-06-18 發送簡訊給我 |
|
turboted
版主 發表:95 回覆:754 積分:452 註冊:2002-07-23 發送簡訊給我 |
送你一個以前用的例子 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;
}
}
}
|
lcsboy
版主 發表:87 回覆:622 積分:394 註冊:2002-06-18 發送簡訊給我 |
|
anpino
版主 發表:31 回覆:477 積分:231 註冊:2003-01-02 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |