Пример #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;
                }
            }
        }
Пример #2
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);
                }
            }
        }