제목은 거창하나.....하하...지식인 답변달기 위해 작성한 프로젝트입니다.

사용자가 폴더 경로를 선택하면 해당 폴더의 하위 폴더들과 파일들의 아이콘들을 가져와서

ListView에 추가하는 로직입니다.

더불어 아이콘을 더블 클릭하면 폴더 및 파일이 실행됩니다.

 

결과 화면부터 보겠습니다.

 

[이미지 1] 결과화면

 

 

 

Icon은

http://bbangwon.tistory.com/20

위의 블로그에서 Icon 얻어오는 소스 사용하였습니다.

 

 

프로젝트 설명하겠습니다.

디자이너는 아래의 이미지와 같이 구성하였습니다.

 

 

[이미지 2] 디자이너 화면

 

 

 

 

소스입니다. 이거 원 전체 소스를 올려버렸네요..ㅡㅡ;

흠...주석 달기가 애매해서 일단 올립니다.

문의 사항은 ehdrnsep@nate.com 메일주세요!

 

HowToImageListView.zip

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HowToImageListView
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 현재 폴더 경로입니다.
        /// </summary>
        string selectedFolderPath;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //ListView의 표시방법을 LrageIcon으로 설정
            this.listView.View = View.LargeIcon;
            this.listView.MouseDoubleClick += new MouseEventHandler(listView_MouseDoubleClick);
        }


        /// <summary>
        /// ListView에서 Mouse DoubleClick 하였을 때
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void listView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.listView.SelectedItems.Count != 0)
            {
                string path = this.listView.SelectedItems[0].Text;

                //폴더라면
                if (System.IO.Directory.Exists(path) == true)
                {
                    System.Diagnostics.Process.Start(path);
                }
                else //파일이라면
                {
                    System.Diagnostics.Process.Start(string.Format(@"{0}\{1}",this.selectedFolderPath,path));
                }
            }
        }

        /// <summary>
        /// 폴더 경로 설정
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripMenuItem_Open_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fb = new FolderBrowserDialog();
            if (fb.ShowDialog() == DialogResult.OK)
            {
                this.selectedFolderPath = fb.SelectedPath;
                AddListViewLargeImageItem(fb.SelectedPath);
            }
        }

        /// <summary>
        /// 해당 폴더 경로의 아이콘들을 ListView에 추가한다.
        /// </summary>
        /// <param name="p"></param>
        private void AddListViewLargeImageItem(string folderPath)
        {
            this.listView.Items.Clear();

            ImageList imageList = new ImageList();
            imageList.ImageSize = new Size(32, 32);

            AddFolderIcon(ref imageList, folderPath);
            AddFileIcon(ref imageList, folderPath);
            
            this.listView.LargeImageList = imageList;
        }


        /// <summary>
        /// 파일의 아이콘을 추가합니다.
        /// </summary>
        /// <param name="imageList"></param>
        /// <param name="folderPath"></param>
        private void AddFileIcon(ref ImageList imageList, string folderPath)
        {
            string[] files = System.IO.Directory.GetFiles(folderPath);
            foreach (string file in files)
            {
                //아이콘 추가
                Icon icon = Etier.IconHelper.IconReader.GetFileIcon(file, Etier.IconHelper.IconReader.IconSize.Large, false);
                imageList.Images.Add(icon);

                this.listView.Items.Add(System.IO.Path.GetFileName(file), imageList.Images.Count - 1);
            }
        }

        /// <summary>
        /// 폴더의 아이콘을 추가합니다.
        /// </summary>
        /// <param name="imageList"></param>
        /// <param name="folderPath"></param>
        private void AddFolderIcon(ref ImageList imageList, string folderPath)
        {
            string[] folders = System.IO.Directory.GetDirectories(folderPath);

            foreach (string folder in folders)
            {
                //아이콘 추가
                Icon icon = Etier.IconHelper.IconReader.GetFolderIcon(Etier.IconHelper.IconReader.IconSize.Large, Etier.IconHelper.IconReader.FolderType.Open);
                imageList.Images.Add(icon);

                this.listView.Items.Add(System.IO.Path.GetDirectoryName(folder), imageList.Images.Count - 1);
            }
        }

        
    }
}


 

'.Net > Winform' 카테고리의 다른 글

C# Winform에서 DaumAPI 사용하기[1]_WebBrowser Control 사용  (0) 2012.09.10
C# MS Chart  (2) 2012.08.30
C# 자신의 IP 및 MAC ADRESS 확인하기  (0) 2012.08.29
C# Ping 확인  (0) 2012.08.28
C# 계정을 이용한 Directory Lock & Unlock  (0) 2012.08.28

+ Recent posts