前田稔(Maeda Minoru)の超初心者のプログラム入門
ファイル名 | 説明 |
---|---|
FolderView.cs | TreeView と ListView を並べてフォルダを表示 |
/**************************************************************/ /*★ TreeView と ListView を並べてフォルダを表示 前田 稔 ★*/ /**************************************************************/ using System; using System.Drawing; using System.Windows.Forms; using System.IO; using System.Management; // 参照の追加 要! public class MyForm : Form { private SplitContainer splitContainer1; private TreeView treeView1; private ListView listView1; int[] width = { 360, 120, 80 }; string[] head = { "名前", "更新日付", "サイズ" }; string path = "c:\\Test"; ImageList imageListSmall = new ImageList(); string[] iconFiles = { "C:\\DATA\\Test\\null.ico", "C:\\DATA\\Test\\folder.ico", "C:\\DATA\\Test\\fdd.ico", "C:\\DATA\\Test\\hdd.ico", "C:\\DATA\\Test\\cd.ico", "C:\\DATA\\Test\\remove.ico" }; public MyForm() { InitializeComponent(); Load += new System.EventHandler(MyForm_Load); } private void InitializeComponent() { this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.treeView1 = new System.Windows.Forms.TreeView(); this.listView1 = new System.Windows.Forms.ListView(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); this.splitContainer1.SuspendLayout(); this.SuspendLayout(); // // splitContainer1 // this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill; this.splitContainer1.Location = new System.Drawing.Point(0, 0); this.splitContainer1.Name = "splitContainer1"; // // splitContainer1.Panel1 // this.splitContainer1.Panel1.Controls.Add(this.treeView1); // // splitContainer1.Panel2 // this.splitContainer1.Panel2.Controls.Add(this.listView1); this.splitContainer1.Size = new System.Drawing.Size(598, 245); this.splitContainer1.SplitterDistance = 104; this.splitContainer1.TabIndex = 0; // // treeView1 // this.treeView1.BackColor = System.Drawing.SystemColors.ControlLightLight; this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill; this.treeView1.Location = new System.Drawing.Point(0, 0); this.treeView1.Name = "treeView1"; this.treeView1.Size = new System.Drawing.Size(104, 245); this.treeView1.TabIndex = 0; // // listView1 // this.listView1.BackColor = System.Drawing.SystemColors.ControlLight; this.listView1.Dock = System.Windows.Forms.DockStyle.Fill; this.listView1.FullRowSelect = true; this.listView1.GridLines = true; this.listView1.Location = new System.Drawing.Point(0, 0); this.listView1.Name = "listView1"; this.listView1.Size = new System.Drawing.Size(490, 245); this.listView1.TabIndex = 0; this.listView1.UseCompatibleStateImageBehavior = false; this.listView1.View = System.Windows.Forms.View.Details; // // MyForm // this.ClientSize = new System.Drawing.Size(700, 245); this.Controls.Add(this.splitContainer1); this.Name = "MyForm"; this.splitContainer1.Panel1.ResumeLayout(false); this.splitContainer1.Panel2.ResumeLayout(false); this.splitContainer1.ResumeLayout(false); this.ResumeLayout(false); } // Tree, List にノード(Item)を設定 private void MyForm_Load(object sender, System.EventArgs e) { ColumnHeader[] columnHead = new ColumnHeader[3]; for (int i=0; i<3; i++) { columnHead[i] = new ColumnHeader(); columnHead[i].Width = width[i]; columnHead[i].Text = head[i]; } listView1.Columns.AddRange(columnHead); imageListSmall.ImageSize = new Size(16, 16); listView1.SmallImageList = imageListSmall; treeView1.ImageList = imageListSmall; listView1.Items.Clear(); imageListSmall.Images.Clear(); for (int i = 0; i < iconFiles.GetLength(0); i++) { imageListSmall.Images.Add(new Bitmap(iconFiles[i])); } path= System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); DirectoryInfo di = new DirectoryInfo(path); //パスを指定 string[] item = new string[3]; //List にディレクトリ一覧を登録 DirectoryInfo[] dis = di.GetDirectories("*.*"); //パターンを指定 for (int i = 0; i < dis.Length; i++) { item[0] = dis[i].FullName; item[1] = dis[i].LastWriteTime.ToString(); item[2] = ""; listView1.Items.Add(new ListViewItem(item)); listView1.Items[i].ImageIndex = 1; } //List にファイル一覧を登録 FileInfo [] fis = di.GetFiles("*.*"); //パターンを指定 for( int i=0;i<fis.Length;i++) { item[0]= fis[i].FullName; item[1]= fis[i].LastWriteTime.ToString(); item[2]= fis[i].Length.ToString(); listView1.Items.Add(new ListViewItem(item)); Icon appIcon = System.Drawing.Icon.ExtractAssociatedIcon(fis[i].FullName); listView1.Items[listView1.Items.Count-1].ImageIndex = imageListSmall.Images.Count; imageListSmall.Images.Add(appIcon); } //Tree にドライブ一覧を登録 string[] drives = Directory.GetLogicalDrives() ; foreach(string drive in drives) { string drive2 = drive.Substring(0, 2); // 先頭の2文字を取り出す ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive2 + "\""); disk.Get(); TreeNode tn = new TreeNode(drive2) ; if (drive2=="A:") tn.ImageIndex = tn.SelectedImageIndex = 2; else if (disk["DriveType"].ToString()=="3") tn.ImageIndex = tn.SelectedImageIndex = 3; else if (disk["DriveType"].ToString()=="5") tn.ImageIndex = tn.SelectedImageIndex = 4; else if (disk["DriveType"].ToString()=="2") tn.ImageIndex = tn.SelectedImageIndex = 5; else tn.ImageIndex = tn.SelectedImageIndex = 1; tn.Nodes.Add("dummy"); // +を表示するためにダミーノード追加 treeView1.Nodes.Add(tn); } } } class form01 { public static void Main() { MyForm mf = new MyForm(); Application.Run(mf); } } |
[Next Chapter ↓] Handler
[Previous Chapter ↑] SplitContainer