Exemplo n.º 1
0
        /// <summary>
        /// 在将对象拖到控件的边界上发生。
        /// </summary>
        private void treeView_DragOver(object sender, DragEventArgs e)
        {
            // 计算拖动的位置并移动图片
            Point point = this.tempTreeView.PointToClient(new Point(e.X, e.Y));

            DragHelper.ImageList_DragMove(point.X - this.tempTreeView.Left, point.Y - this.tempTreeView.Top);

            // 取得实际拖拽到目标节点
            TreeNode nodeAt = this.tempTreeView.GetNodeAt(this.tempTreeView.PointToClient(new Point(e.X, e.Y)));

            if (nodeAt == null)
            {
                e.Effect = DragDropEffects.None;
            }
            else
            {
                e.Effect = DragDropEffects.Move;
                if (this.tempDropNode != nodeAt)
                {
                    DragHelper.ImageList_DragShowNolock(false);
                    this.tempTreeView.SelectedNode = nodeAt;
                    DragHelper.ImageList_DragShowNolock(true);
                    this.tempDropNode = nodeAt;
                }
                TreeNode treeNode = nodeAt;
                while (treeNode.Parent != null)
                {
                    if (treeNode.Parent == this.dragNode)
                    {
                        e.Effect = DragDropEffects.None;
                    }
                    treeNode = treeNode.Parent;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 当用户开始拖动节点时发生。
        /// </summary>
        private void treeView_ItemDrag(object sender, ItemDragEventArgs e)
        {
            // 取得拖动的节点,并选中它。
            this.dragNode = (TreeNode)e.Item;
            this.tempTreeView.SelectedNode = this.dragNode;
            //this.nodeImage =  //知道被拖动的节点,就应该知道节点的图标是什么?

            // 重新设置拖动节点的图标
            this.imageListDrag.Images.Clear();
            this.imageListDrag.ImageSize = new Size(this.dragNode.Bounds.Size.Width + this.tempTreeView.Indent, this.dragNode.Bounds.Height);

            // 创建新图标
            // 这个图标将包含被拖动的树节点的图象
            Bitmap bitmap = new Bitmap(this.dragNode.Bounds.Width + this.tempTreeView.Indent, this.dragNode.Bounds.Height);

            Graphics graphics = Graphics.FromImage(bitmap);

            // 在图标里包含被拖动节点的图象
            if (this.imageListTreeView != null)
            {
                graphics.DrawImage(this.imageListTreeView.Images[0], 0, 0);
            }

            // 把被拖动节点的标签
            graphics.DrawString(this.dragNode.Text,
                                this.tempTreeView.Font,
                                new SolidBrush(this.tempTreeView.ForeColor),
                                (float)this.tempTreeView.Indent, 1.0f);

            this.imageListDrag.Images.Add(bitmap);

            Point point     = this.tempTreeView.PointToClient(Control.MousePosition);
            int   dxHotspot = point.X + this.tempTreeView.Indent - this.dragNode.Bounds.Left;
            int   dyHotspot = point.Y - this.dragNode.Bounds.Top;

            // 开始拖拽图象
            if (DragHelper.ImageList_BeginDrag(this.imageListDrag.Handle, 0, dxHotspot, dyHotspot))
            {
                // 开始
                this.tempTreeView.DoDragDrop(bitmap, DragDropEffects.Move);
                // 结束
                DragHelper.ImageList_EndDrag();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 当指定的计时器间隔已过去而且计时器处于启用状态时发生。
        /// </summary>
        private void timer_Tick(object sender, EventArgs e)
        {
            // 取得鼠标位置的节点
            Point    pt   = tempTreeView.PointToClient(Control.MousePosition);
            TreeNode node = this.tempTreeView.GetNodeAt(pt);

            if (node == null)
            {
                return;
            }

            // 如果鼠标靠近顶端,设置向上滚动条
            if (pt.Y < 30)
            {
                if (node.PrevVisibleNode != null)
                {
                    node = node.PrevVisibleNode;

                    DragHelper.ImageList_DragShowNolock(false);
                    node.EnsureVisible();
                    this.tempTreeView.Refresh();
                    DragHelper.ImageList_DragShowNolock(true);
                }
            }
            // 如果鼠标靠近底端,设置向下滚动条
            else if (pt.Y > this.tempTreeView.Size.Height - 30)
            {
                if (node.NextVisibleNode != null)
                {
                    node = node.NextVisibleNode;

                    DragHelper.ImageList_DragShowNolock(false);
                    node.EnsureVisible();
                    this.tempTreeView.Refresh();
                    DragHelper.ImageList_DragShowNolock(true);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 在完成拖放操作时发生,所有具体移动操作都发生在这里。
        /// </summary>
        private void treeView_DragDrop(object sender, DragEventArgs e)
        {
            // Unlock updates
            DragHelper.ImageList_DragLeave(this.tempTreeView.Handle);

            // 取得目标节点
            TreeNode nodeAt = this.tempTreeView.GetNodeAt(this.tempTreeView.PointToClient(new Point(e.X, e.Y)));

            // 如果拖拽的节点不等于目标节点,则增加拖拽的节点为目标节点的子节点。
            if (this.dragNode != nodeAt)
            {
                //委托出去做持久化操作,如果持久化成功,才能移动,否则不移动。
                if (this.ProcessDragNode != null && this.ProcessDragNode(this.dragNode, nodeAt))
                {
                    if (this.dragNode.Parent == null)
                    {
                        this.tempTreeView.Nodes.Remove(this.dragNode);
                    }
                    else
                    {
                        this.dragNode.Parent.Nodes.Remove(this.dragNode);
                    }

                    // 把拖拽的节点增加到目标节点上。
                    nodeAt.Nodes.Add(this.dragNode);
                    nodeAt.ExpandAll();

                    this.dragNode      = null;
                    this.timer.Enabled = false;
                }
                else
                {
                    MessageUtil.ShowTips("持久化失败,不能移动节点!");
                }
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// 在将对象拖出控件的边界时发生。
 /// </summary>
 private void treeView_DragLeave(object sender, EventArgs e)
 {
     DragHelper.ImageList_DragLeave(this.tempTreeView.Handle);
     this.timer.Enabled = false;
 }
Exemplo n.º 6
0
 /// <summary>
 /// 在将对象拖入控件的边界时发生。
 /// </summary>
 private void treeView_DragEnter(object sender, DragEventArgs e)
 {
     DragHelper.ImageList_DragEnter(this.tempTreeView.Handle, e.X - this.tempTreeView.Left, e.Y - this.tempTreeView.Top);
     this.timer.Enabled = true;
 }