TreeView控件中实现拖拽的功能

 

#region 节点拖拽事件
        //当用户开始拖动节点时
        private void tvModel_ItemDrag(object sender, ItemDragEventArgs e)
        {
            TreeNode selectNode = e.Item as TreeNode;
            this.tvModel.SelectedNode = selectNode;

            this.form.DoDragDrop(e.Item, DragDropEffects.Move);
        }

        //将对象拖入控件的边界时
        private void tvModel_DragEnter(object sender, DragEventArgs e)
        {
            TreeNode enterNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));
            if (enterNode != null)
                e.Effect = DragDropEffects.Move;
            else
                e.Effect = DragDropEffects.None;
        }

        //在完成拖放操作时
        private void tvModel_DragDrop(object sender, DragEventArgs e)
        {
            TreeNode selectNode = (TreeNode)(e.Data.GetData(typeof(TreeNode)));

            //0.如果选择源为空 或者不是二级节点则退出
            if (selectNode == null || selectNode.Level != 1)
                return;

            //1.根据鼠标坐标获得目标节点
            Position.X = e.X;
            Position.Y = e.Y;
            Position = this.tvModel.PointToClient(Position);
            TreeNode targetNode = this.tvModel.GetNodeAt(Position);
            foreach (TreeNode node in this.tvModel.Nodes[0].Nodes)
                node.NodeFont = new Font(this.form.Font, FontStyle.Regular);

            //2.获得要插入的目标节点索引
            int index = 0;
            if (targetNode == null)
                index = selectNode.Parent.Nodes.Count - 1;      //目标节点为null, 直接插入到最后
            else if (targetNode.Level == 1)
                index = targetNode.Index + 1;                   //目标节点为同级, 直接在之后插入
            else if (targetNode.Level == 0)
                index = 0;                                      //目标为根节点, 直接插入到最前
            else if (targetNode.Level > 1)
            {                                                   //目标节点为二级节点以后, 遍历到其二级节点
                while (targetNode.Level > 1)
                    targetNode = targetNode.Parent;
                index = index = targetNode.Index + 1;
            }

            //3.源节点删除, 目标节点在索引出插入, 并高亮选中
            selectNode.Remove();
            this.tvModel.Nodes[0].Nodes.Insert(index, selectNode);
            this.tvModel.SelectedNode = selectNode;
        }

        //将对象拖过控件边缘时
        private void tvModel_DragOver(object sender, DragEventArgs e)
        {
            Position.X = e.X;
            Position.Y = e.Y;
            Position = this.tvModel.PointToClient(Position);

            //在拖过的控件前后显示划线效果
            TreeNode targetNode = this.tvModel.GetNodeAt(Position);
            if (targetNode != null && targetNode.Level == 1)
            {
                if (targetNode.PrevNode != null)
                    targetNode.PrevNode.NodeFont = new Font(this.form.Font, FontStyle.Regular);
                if (targetNode.NextNode != null)
                    targetNode.NextNode.NodeFont = new Font(this.form.Font, FontStyle.Regular);
                targetNode.NodeFont = new Font(this.form.Font, FontStyle.Underline);
            }
        }
        #endregion

 

posted @ 2012-04-28 17:46  跟着阿笨一起玩.NET  阅读(1928)  评论(0编辑  收藏  举报