⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 folderbrowserdialog.cs

📁 POCKET PC,照片管理系统!提供了真正意义上的目录打开功能
💻 CS
字号:
using System;
using System.IO;
using System.Windows.Forms;

namespace Addot.Windows.Forms
{
	/// <summary>
	/// This class doesn't exist in the .Net Compact Framework.
	/// Allow the user to choose a folder. 
    /// Please, refer to the .Net framework for the complete documentation.
    /// </summary>
	public sealed class  FolderBrowserDialog : System.Windows.Forms.Form
	{
        #region --- Fields ---
        private string selected;
        private Environment.SpecialFolder root;
        #endregion

        #region --- Controls ---
        private ImageList ilTreeIcons;
        private System.Windows.Forms.Button btCancel;
        private TreeView tvFolderTree;
        #endregion
    
        #region --- Properties ---
        /// <summary>
        /// The complete path of the selected folder.
        /// </summary>
        public string SelectedPath
        {
            get
            {
                return this.selected;
            }

            set
            {
                this.selected = value;
            }
        }

        /// <summary>
        /// Description of the window. As we are with Pocket PC,
        /// so with a tiny window, this description is displayed
        /// as the title of the window.
        /// </summary>
        public string Description
        {
            get
            {
                return this.Text;
            }

            set
            {
                this.Text = value;
            }
        }

        /// <summary>
        /// If someone knows how to rename a TreeView item, please contact me and this
        /// feature will be implemented.
        /// </summary>
        public bool ShowNewFolderButton
        {
            get
            {
                return false;
            }
            set
            {
                System.Resources.ResourceManager rm = new System.Resources.ResourceManager("Addot.Addot", System.Reflection.Assembly.GetExecutingAssembly());
                throw new NotSupportedException(rm.GetString("FolderBrowserDialogException"));
            }
        }
        public Environment.SpecialFolder RootFolder
        {
            get
            {
                return root;
            }

            set
            {
                root = value;
            }
        }
        #endregion

		public FolderBrowserDialog()
		{
            Cursor.Current = Cursors.WaitCursor;
            //
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// Put default values as specified in the MSDN
			//
            this.Menu = null;
            root = Environment.SpecialFolder.MyComputer;
            selected = "";
            this.Text = "";
        }

		#region Windows Form Designer generated code
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FolderBrowserDialog));
            // 
            // FolderBrowserDialog
            // 
            this.ClientSize = ((System.Drawing.Size)(resources.GetObject("$this.ClientSize")));
            this.Font = ((System.Drawing.Font)(resources.GetObject("$this.Font")));
            this.Location = new System.Drawing.Point(0, 0);
            // 
            // btCancel
            // 
            this.btCancel = new System.Windows.Forms.Button();
            this.btCancel.Parent = this;
            this.btCancel.Location = ((System.Drawing.Point)(resources.GetObject("btCancel.Location")));
            this.btCancel.Size = ((System.Drawing.Size)(resources.GetObject("btCancel.Size")));
            this.btCancel.Text = resources.GetString("btCancel.Text");
            this.btCancel.Click += new System.EventHandler(this.OnCancel);
            // 
            // ilTreeIcons
            // 
            this.ilTreeIcons = new System.Windows.Forms.ImageList();
            this.ilTreeIcons.ImageSize = new System.Drawing.Size(16, 16);
            this.ilTreeIcons.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
            this.ilTreeIcons.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
            // 
            // tvFolderTree
            // 
            this.tvFolderTree = new System.Windows.Forms.TreeView();
            this.tvFolderTree.Parent = this;
            this.tvFolderTree.ImageList = this.ilTreeIcons;
            this.tvFolderTree.Visible = false;
            this.tvFolderTree.Location = ((System.Drawing.Point)(resources.GetObject("tvFolderTree.Location")));
            this.tvFolderTree.Size = ((System.Drawing.Size)(resources.GetObject("tvFolderTree.Size")));
            this.tvFolderTree.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.OnSelectedChanged);
        }
		#endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad (e);

            //
            // Fill the tree
            //
            System.Resources.ResourceManager rm = new System.Resources.ResourceManager("Addot.Addot", this.GetType().Assembly);
            string folder = Environment.GetFolderPath(root);
            DirectoryInfo rootFolder = new DirectoryInfo(folder);
            if( !rootFolder.Exists )
            {
                root = Environment.SpecialFolder.MyComputer;
                rootFolder = new DirectoryInfo(Environment.GetFolderPath(root));
            }

            TreeNode node;

            if( root == Environment.SpecialFolder.MyComputer )
            {
                node = new TreeNode(rm.GetString("RootDevice"));
                node.ImageIndex = 0;
                node.SelectedImageIndex = 0;
            }
            else
            {
                node = new TreeNode(rootFolder.Name);
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;
            }

            node.Tag = rootFolder;
            this.tvFolderTree.Nodes.Add(node);
            ProcessDirectory(rootFolder, node);
            node.Expand();
            this.tvFolderTree.Visible = true;
            this.tvFolderTree.Focus();
            Cursor.Current = Cursors.Default;
        }

        private void ProcessDirectory(DirectoryInfo currentDir, TreeNode currentNode)
        {
            TreeNode node;
            DirectoryInfo[] subs = currentDir.GetDirectories();
            foreach(DirectoryInfo sub in subs)
            {
                node = new TreeNode(sub.Name);
                node.ImageIndex = 1;
                node.SelectedImageIndex = 1;
                node.Tag = sub;
                currentNode.Nodes.Add(node);
                if( sub.FullName.Equals(selected) )
                {
                    currentNode.Expand();
                    tvFolderTree.SelectedNode = node;
                }

                //
                // Add sub-folders
                //
                ProcessDirectory(sub, node);
            }
        }

        private void OnSelectedChanged(object sender, TreeViewEventArgs e)
        {
            DirectoryInfo diSelected = (DirectoryInfo)this.tvFolderTree.SelectedNode.Tag;
            selected = diSelected.FullName;
        }

        protected override void OnDeactivate(EventArgs e)
        {
            base.OnDeactivate (e);

            if( this.DialogResult == DialogResult.OK )
            {
                this.OnSelectedChanged(this, null);
            }
        }

        private void OnCancel(object sender, System.EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -