(1). Description
Use the Treeview control to implement the resource manager, display all folders and files on the local hard disk, and view images
(2). Image example
(3). Code
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. IO;
Convert namespace image format to II
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class formmain: system. Windows. Forms. Form
{
Private system. Windows. Forms. Treeview treeviewdir;
Private system. Windows. Forms. splitter splitter1;
Private system. Windows. Forms. Panel Panel1;
Private system. Windows. Forms. picturebox;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Private Bitmap bitmap;
// Public void Bitmap (string filename, bool useicm );//**********
Public formmain ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
//
// Todo: add Any constructor code after initializecomponent calls
//
Picturebox. Top = treeviewdir. Top;
Picturebox. Left = treeviewdir. Left;
Picturebox. width = panel1.width;
Picturebox. Height = panel1.height;
Bitmap = NULL;
Filldirtree ();
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region windows Form Designer generated code
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. treeviewdir = new system. Windows. Forms. Treeview ();
This. splitter1 = new system. Windows. Forms. Splitter ();
This. Panel1 = new system. Windows. Forms. Panel ();
This. picturebox = new system. Windows. Forms. picturebox ();
This. panel1.suspendlayout ();
This. suspendlayout ();
//
// Treeviewdir
//
This. treeviewdir. backcolor = system. drawing. color. fromargb (system. byte) (0), (system. byte) (192), (system. byte) (192 )));
This. treeviewdir. Dock = system. Windows. Forms. dockstyle. Left;
This. treeviewdir. imageindex =-1;
This. treeviewdir. Location = new system. Drawing. Point (0, 0 );
This. treeviewdir. Name = "treeviewdir ";
This. treeviewdir. selectedimageindex =-1;
This. treeviewdir. size = new system. Drawing. Size (136,373 );
This. treeviewdir. tabindex = 0;
This. treeviewdir. afterselect + = new system. Windows. Forms. treevieweventhandler (this. treeviewdir_afterselect );
This. treeviewdir. beforeexpand + = new system. Windows. Forms. treeviewcanceleventhandler (this. treeviewdir_beforeexpand );
//
// Splitter1
//
This. splitter1.location = new system. Drawing. Point (136, 0 );
This. splitter1.name = "splitter1 ";
This. splitter1.size = new system. Drawing. Size (8,373 );
This. splitter1.tabindex = 1;
This. splitter1.tabstop = false;
//
// Panel1
//
This. panel1.controls. Add (this. picturebox );
This. panel1.dock = system. Windows. Forms. dockstyle. Fill;
This. panel1.location = new system. Drawing. Point (144, 0 );
This. panel1.name = "Panel1 ";
This. panel1.size = new system. Drawing. Size (360,373 );
This. panel1.tabindex = 2;
This. panel1.resize + = new system. eventhandler (this. paneltailresize );
//
// Picturebox
//
This. picturebox. backcolor = system. drawing. color. fromargb (system. byte) (192), (system. byte) (192), (system. byte) (255 )));
This. picturebox. Dock = system. Windows. Forms. dockstyle. Fill;
This. picturebox. Location = new system. Drawing. Point (0, 0 );
This. picturebox. Name = "picturebox ";
This. picturebox. size = new system. Drawing. Size (360,373 );
This. picturebox. sizemode = system. Windows. Forms. pictureboxsizemode. stretchimage;
This. picturebox. tabindex = 0;
This. picturebox. tabstop = false;
//
// Formmain
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (504,373 );
This. Controls. Add (this. Panel1 );
This. Controls. Add (this. splitter1 );
This. Controls. Add (this. treeviewdir );
This. Name = "formmain ";
This. startposition = system. Windows. Forms. formstartposition. centerscreen;
This. Text = "Image Browsing ";
This. panel1.resumelayout (false );
This. resumelayout (false );
}
# Endregion
Private void filldirtree ()
{
String [] drivers = environment. getlogicaldrives ();
For (INT I = 0; I <drivers. length; I ++)
{
Console. writeline ("I = {0}", I );
If (platforminvokekernel32.getdrivetype (drivers [I]) = platforminvokekernel32.drive _ fixed)
{
Dirnode root = new dirnode (drivers [I]);
Treeviewdir. nodes. Add (Root );
Adddirs (Root );
}
}
}
Private void adddirs (treenode node)
{
Try
{
Directoryinfo dir = new directoryinfo (getpathfromnode (node ));
Directoryinfo [] e = dir. getdirectories ();
Fileinfo [] f = dir. getfiles ();
String name;
For (INT I = 0; I <E. length; I ++)
{
Name = E [I]. Name;
If (! Name. Equals (".")&&! Name. Equals (".."))
{
Node. nodes. Add (New dirnode (name ));
}
}
For (INT I = 0; I <F. length; I ++)
{
Name = f [I]. Name;
Node. nodes. Add (New dirnode (name ));
}
}
Catch
{
}
}
Private string getpathfromnode (treenode node)
{
If (node. Parent = NULL)
{
Return node. text;
}
Return Path. Combine (getpathfromnode (node. Parent), node. Text );
}
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New formmain ());
}
Private void paneltailresize (Object sender, system. eventargs E)
{
Picturebox. Top = treeviewdir. Top;
Picturebox. Left = treeviewdir. Left;
If (Bitmap! = NULL)
{
If (bitmap. width> bitmap. Height)
{
Picturebox. width = panel1.width;
Picturebox. Height =
(INT) (double) bitmap. Height * panel1.width/bitmap. width );
}
Else
{
Picturebox. Height = panel1.height;
Picturebox. width =
(INT) (double) bitmap. Width * panel1.height/bitmap. Height );
}
}
Else
{
Picturebox. width = panel1.width;
Picturebox. Height = panel1.height;
}
Picturebox. Refresh ();
}
Private void treeviewdir_afterselect (Object sender, system. Windows. Forms. treevieweventargs E)
{
If (Bitmap! = NULL)
{
Bitmap. Dispose ();
}
Try
{
String filename = getpathfromnode (E. node );
Bitmap = new Bitmap (filename );
Fileinfo F = new fileinfo (filename );
If (bitmap. width> bitmap. Height)
{
Picturebox. width = panel1.width;
Picturebox. Height =
(INT) (double) bitmap. Height * panel1.width/bitmap. width );
}
Else
{
Picturebox. Height = panel1.height;
Picturebox. width =
(INT) (double) bitmap. Width * panel1.height/bitmap. Height );
}
Picturebox. Image = bitmap;
This. Text = "Image Browsing" + F. Name;
}
Catch
{
Picturebox. width = panel1.width;
Picturebox. Height = panel1.height;
}
}
Private void treeviewdir_beforeexpand (Object sender, system. Windows. Forms. treeviewcanceleventargs E)
{
Dirnode nodeexpanding = (dirnode) E. node;
If (! Nodeexpanding. subdirectoriesadded)
{
Addsubdirs (nodeexpanding );
}
}
Private void addsubdirs (dirnode node)
{
For (INT I = 0; I <node. nodes. Count; I ++)
{
Adddirs (node. nodes [I]);
}
Node. subdirectoriesadded = true;
}
}
}
(4) Download Sample Code
Http://www.cnblogs.com/Files/ChengKing/TreeView_WinForm.rar