첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 소스 노드
        /// </summary>
        private TreeNode sourceNode;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            #region 트리뷰를 설정한다.

            this.treeView.AllowDrop = true;

            #endregion
            #region 이벤트를 설정한다.

            Load                    += Form_Load;
            this.treeView.ItemDrag  += treeView_ItemDrag;
            this.treeView.DragEnter += treeView_DragEnter;
            this.treeView.DragDrop  += treeView_DragDrop;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            TreeNode rootNode = null;

            for(int i = 0; i < 101; i++)
            {
                if(i % 10 == 0)
                {
                    if(rootNode != null)
                    {
                        this.treeView.Nodes.Add(rootNode);
                    }

                    rootNode = new TreeNode(i.ToString());
                }
                else
                {
                    TreeNode childNode = new TreeNode(i.ToString());

                    rootNode.Nodes.Add(childNode);
                }
            }

            this.treeView.ExpandAll();
        }

        #endregion
        #region 트리뷰 항목 DRAG 처리하기 - treeView_ItemDrag(sender, e)

        /// <summary>
        /// 트리뷰 항목 DRAG 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void treeView_ItemDrag(object sender, ItemDragEventArgs e)
        {
            this.sourceNode = e.Item as TreeNode;

            DoDragDrop(e.Item.ToString(), DragDropEffects.Move | DragDropEffects.Copy);
        }

        #endregion
        #region 트리뷰 DRAG ENTER 처리하기 - treeView_DragEnter(sender, e)

        /// <summary>
        /// 트리뷰 DRAG ENTER 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void treeView_DragEnter(object sender, DragEventArgs e)
        {
            if(e.Data.GetDataPresent(DataFormats.Text))          
            {
                e.Effect = DragDropEffects.Move;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        #endregion
        #region 트리뷰 DRAG DROP 처리하기 - treeView_DragDrop(sender, e)

        /// <summary>
        /// 트리뷰 DRAG DROP 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void treeView_DragDrop(object sender, DragEventArgs e)
        {
            TreeView treeView = sender as TreeView;

            Point dropPoint = treeView.PointToClient(new Point(e.X, e.Y));

            TreeNode targetNode = this.treeView.GetNodeAt(dropPoint);

            TreeNode copyNode;

            if(targetNode != null)
            {
                copyNode = new TreeNode
                (
                    sourceNode.Text,
                    sourceNode.ImageIndex,
                    sourceNode.SelectedImageIndex
                );

                if(sourceNode.Index > targetNode.Index)
                {
                    targetNode.Parent.Nodes.Insert(targetNode.Index, copyNode);
                }
                else
                {
                    targetNode.Parent.Nodes.Insert(targetNode.Index + 1, copyNode);
                }

                this.sourceNode.Remove();

                this.treeView.Invalidate();
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요