📄 controlsform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace mcaoWinAppAdvance
{
/// <summary>
/// ControlsForm 的摘要说明。
/// </summary>
public class ControlsForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.Splitter vSplitter;
private System.Windows.Forms.Splitter hSplitter;
private System.Windows.Forms.ImageList fileIconList;
private System.Windows.Forms.ImageList imageListLarge;
private System.ComponentModel.IContainer components;
public ControlsForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ControlsForm));
this.treeView1 = new System.Windows.Forms.TreeView();
this.vSplitter = new System.Windows.Forms.Splitter();
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.hSplitter = new System.Windows.Forms.Splitter();
this.listView1 = new System.Windows.Forms.ListView();
this.fileIconList = new System.Windows.Forms.ImageList(this.components);
this.imageListLarge = new System.Windows.Forms.ImageList(this.components);
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Dock = System.Windows.Forms.DockStyle.Left;
this.treeView1.ImageList = this.fileIconList;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("我的电脑")});
this.treeView1.Size = new System.Drawing.Size(112, 285);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
//
// vSplitter
//
this.vSplitter.Location = new System.Drawing.Point(112, 0);
this.vSplitter.Name = "vSplitter";
this.vSplitter.Size = new System.Drawing.Size(3, 285);
this.vSplitter.TabIndex = 1;
this.vSplitter.TabStop = false;
//
// panel1
//
this.panel1.Controls.Add(this.button1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.panel1.Location = new System.Drawing.Point(115, 237);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(301, 48);
this.panel1.TabIndex = 2;
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 24);
this.button1.TabIndex = 1;
this.button1.Text = "Exit";
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// hSplitter
//
this.hSplitter.Dock = System.Windows.Forms.DockStyle.Bottom;
this.hSplitter.Location = new System.Drawing.Point(115, 234);
this.hSplitter.Name = "hSplitter";
this.hSplitter.Size = new System.Drawing.Size(301, 3);
this.hSplitter.TabIndex = 3;
this.hSplitter.TabStop = false;
//
// listView1
//
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.LargeImageList = this.imageListLarge;
this.listView1.Location = new System.Drawing.Point(115, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(301, 234);
this.listView1.TabIndex = 4;
//
// fileIconList
//
this.fileIconList.ImageSize = new System.Drawing.Size(16, 16);
this.fileIconList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("fileIconList.ImageStream")));
this.fileIconList.TransparentColor = System.Drawing.Color.Transparent;
//
// imageListLarge
//
this.imageListLarge.ImageSize = new System.Drawing.Size(16, 16);
this.imageListLarge.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListLarge.ImageStream")));
this.imageListLarge.TransparentColor = System.Drawing.Color.Transparent;
//
// ControlsForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(416, 285);
this.Controls.Add(this.listView1);
this.Controls.Add(this.hSplitter);
this.Controls.Add(this.panel1);
this.Controls.Add(this.vSplitter);
this.Controls.Add(this.treeView1);
this.HelpButton = true;
this.Name = "ControlsForm";
this.Text = "测试分割控件并浏览C盘和所有硬盘上的文件/文件夹";
this.Load += new System.EventHandler(this.ControlsForm_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
this.Close ();
}
private void pictureBox1_Click(object sender, System.EventArgs e)
{
}
private void button1_Click_1(object sender, System.EventArgs e)
{
this.Close();
}
private void EnumDrives(TreeNode ParentNode)
{
if(ParentNode.Nodes.Count == 0)
{
foreach(string drive in Directory.GetLogicalDrives())
{
treeView1.SelectedNode = ParentNode;
TreeNode TempNode = new TreeNode();
TempNode.Text = drive.Substring(0,drive.Length-1);
TempNode.Tag = drive;
TempNode.ImageIndex = 1;
TempNode.SelectedImageIndex = 1;
treeView1.SelectedNode.Nodes.Add(TempNode);
treeView1.SelectedNode.Nodes[treeView1.SelectedNode.Nodes.Count-1].EnsureVisible();
}
}
}
private void EnumDirectories(TreeNode ParentNode)
{
treeView1.SelectedNode = ParentNode;
string DirectoryPath = ParentNode.Tag.ToString();
if(ParentNode.Nodes.Count == 0)
{
if(DirectoryPath.Substring(DirectoryPath.Length-1)!=@"\")
DirectoryPath+=@"\";
try
{
foreach(string directory in Directory.GetDirectories(DirectoryPath))
{
TreeNode TempNode = new TreeNode();
TempNode.Text = directory.Substring(directory.LastIndexOf(@"\")+1);
TempNode.Tag = directory;
TempNode.ImageIndex = 3;
TempNode.SelectedImageIndex = 2;
treeView1.SelectedNode.Nodes.Add(TempNode);
treeView1.SelectedNode.Nodes[treeView1.SelectedNode.Nodes.Count-1].EnsureVisible();
}
}
catch(Exception)
{
}
}
}
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
if(e.Node.Text.ToString() != "我的电脑")
EnumDirectories(e.Node);
else
EnumDrives(e.Node);
}
//加载窗体时,检查C:\\盘上的所有文件和文件夹,以图标的方式显示在ListView中
private void ControlsForm_Load(object sender, System.EventArgs e)
{
foreach (string dirName in Directory.GetDirectories (@"C:\") )
{
ListViewItem aItem = new ListViewItem(dirName,0);
aItem.ImageIndex = 0;
string dirInfo;
dirInfo = File.GetAttributes(dirName).ToString () ;
aItem.SubItems.Add(dirInfo);
dirInfo = File.GetLastAccessTime(dirName).ToString () ;
aItem.SubItems.Add(dirInfo);
aItem.SubItems.Add("目录");
listView1.Items.Add (aItem);
}
foreach (string fileName in Directory.GetFiles (@"C:\"))
{
ListViewItem aItem = new ListViewItem(fileName,0);
aItem.ImageIndex = 1;
string fileInfo;
fileInfo = File.GetAttributes(fileName).ToString () ;
aItem.SubItems.Add(fileInfo);
fileInfo = File.GetLastAccessTime(fileName).ToString () ;
aItem.SubItems.Add(fileInfo);
aItem.SubItems.Add("文件");
listView1.Items.Add (aItem);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -