Отображать изображения в окне для просмотра в виде дерева - PullRequest
0 голосов
/ 13 декабря 2018

Я новичок в этой области, и я пытаюсь разработать приложение, и после поиска, пробуя любые возможности, которые я все еще не могу решить мою проблему ...: (

У меня есть формы WindowsВ этом у меня есть toolStripMenu , treeView и pictureBox . При нажатии на этот пункт меню появляется FolderBrowserDialog , чтобы принять ввод для пути к каталогу.Затем, после выбора пути к каталогу и нажатия кнопки «Загрузить каталог», каталоги и файлы будут отображаться в treeView. Вот мой код для отображения каталогов и файлов (изображений) в элементе управления treeView:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string strSelectedPath;
        FolderBrowserDialog FBD = new FolderBrowserDialog();

        private void wczytajŚcieżkęToolStripMenuItem_Click(object sender, EventArgs e)
        {
            *// Code for the FolderBrowserDialog button*
            DialogResult drResult = FBD.ShowDialog();
            if (drResult == System.Windows.Forms.DialogResult.OK)
                strSelectedPath = FBD.SelectedPath;
        }

        *// Code for the 'Load Directory'*
        private void DirectoryLoad_btn_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 0;
            treeView1.Nodes.Clear();
            toolTip1.ShowAlways = true;
            if (strSelectedPath != "" && Directory.Exists(FBD.SelectedPath))
                LoadDirectory(FBD.SelectedPath);
            else
                MessageBox.Show("Wczytaj folder!", "Brak ścieżki do załadowania");
        }

        public void LoadDirectory(string Dir)
        {
            DirectoryInfo di = new DirectoryInfo(Dir);
            DirectoryInfo[] directories = di.GetDirectories();
            progressBar1.Maximum = Directory.GetFiles(Dir, "*.*", SearchOption.AllDirectories).Length + Directory.GetDirectories(Dir, "**", SearchOption.AllDirectories).Length;
            TreeNode tds = treeView1.Nodes.Add(di.Name);
            tds.Tag = di.FullName;
            tds.StateImageIndex = 0;
            LoadFiles(Dir, tds);
            LoadSubDirectories(Dir, tds);
        }

        private void LoadSubDirectories(string dir, TreeNode td)
        {
            string[] subdirectoryEntries = Directory.GetDirectories(dir);
            foreach (string subdirectory in subdirectoryEntries)
            {
                DirectoryInfo di = new DirectoryInfo(subdirectory);
                TreeNode tds = td.Nodes.Add(di.Name);
                tds.StateImageIndex = 0;
                tds.Tag = di.FullName;
                LoadFiles(subdirectory, tds);
                LoadSubDirectories(subdirectory, tds);
                UpdateProgress();
            }
        }

        private void LoadFiles(string dir, TreeNode td)
        {
            string[] Files = Directory.GetFiles(dir, "*.*");
            foreach (string file in Files)
            {
                FileInfo fi = new FileInfo(file);
                TreeNode tds = td.Nodes.Add(fi.Name);
                tds.Tag = fi.FullName;
                tds.StateImageIndex = 1;
                UpdateProgress();
            }
        }

        private void UpdateProgress()
        {
            if (progressBar1.Value < progressBar1.Maximum)
            {
                progressBar1.Value++;
                int percent = (int)
                (((double)progressBar1.Value / (double)progressBar1.Maximum) * 100);
                progressBar1.CreateGraphics().DrawString(percent.ToString() + "%", new Font("Adobe Caslon Pro", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7));
            }
            Application.DoEvents();
        }

        private void treeView1_MouseMove(object sender, MouseEventArgs e)
        {
            TreeNode theNode = this.treeView1.GetNodeAt(e.X, e.Y);
            if (theNode != null && theNode.Tag != null)
            {
                if (theNode.Tag.ToString() != this.toolTip1.GetToolTip(this.treeView1))
                    this.toolTip1.SetToolTip(this.treeView1, theNode.Tag.ToString());
            }
            else
            {
                this.toolTip1.SetToolTip(this.treeView1, "");
            }
        }

Этот кодработает нормально, но теперь я не могу загрузить изображения в pictureBox из treeView . Я пробовал много способов решить эту проблему, и я застрял с этим ...

Любое предложение с примером кода будет здорово! Спасибо!

...