.NET 2.0 C# Treeview 在 TreeNode 内拖放

发布于 2024-09-18 07:28:07 字数 1340 浏览 0 评论 0原文

我感兴趣的是捕获拖/放事件,该事件将从用户将现有的 TreeNode 拖动到 TreeView 中的某处开始。当用户拖动 TreeNode 时,我感兴趣的是捕获节点何时在两个树节点之间拖动。当用户执行此操作时,我想在树节点之间显示一个哈希标记,以指定该节点是否作为子节点或同级节点在节点内删除。该哈希标记将显示: - 在目标节点下方(指示源节点将作为目标节点的子节点被删除) 或者 - 在目标节点下方左侧(指示源节点将作为目标节点的同级节点删除)之前或之后...

我使用 DragOver 事件取得了一些进展。我正在计算鼠标位置,并在拖动鼠标时推导出顶部和底部节点的位置。

        int threshold = 8;  //Joe(hack)
        Point mouseLocation = mouseLocation = treeViewConditions.PointToClient(new Point(e.X, e.Y - threshold));
        TreeNode topNode = treeViewConditions.GetNodeAt(mouseLocation);
        mouseLocation = treeViewConditions.PointToClient(new Point(e.X + threshold, e.Y));
        TreeNode bottomNode = treeViewConditions.GetNodeAt(mouseLocation);

        if (topNode != null && bottomNode == null)
        {
            textBoxDescription.Text = "handling top node";
        }
        else if (topNode == null && bottomNode != null)
        {
            textBoxDescription.Text = "handling bottom node";
        }
        else if (topNode != null && bottomNode != null)
        {
            if (topNode != bottomNode)
            {
                textBoxDescription.Text = "between!";
            }
            else if (topNode == bottomNode)
            {
            }
        }

但是这样做时,感觉很脏。我想知道是否有人知道更好的方法来实现这一目标。

提前非常感谢!

I am interested in capturing a drag/drop event that will start with the user dragging an existing TreeNode somewhere within the TreeView. While the user is dragging the TreeNode around, I am interested in capturing when the node has been dragged between two tree nodes. When the user does this, I wanted to display a hash mark in-between the tree nodes to designate if the node would be dropped within the node as a child or as a sibling. This hash mark would display either:
- underneath the destination node (to indicate the source node will be dropped as a child of the destination node
OR
- underneath the destination node to the left (to indicate the source node will be dropped as a sibling of the destination node), before or after...

I have made some headway using the DragOver event. I am calculating the mouse location and deriving what the top and bottom nodes are as I drag the mouse around..

        int threshold = 8;  //Joe(hack)
        Point mouseLocation = mouseLocation = treeViewConditions.PointToClient(new Point(e.X, e.Y - threshold));
        TreeNode topNode = treeViewConditions.GetNodeAt(mouseLocation);
        mouseLocation = treeViewConditions.PointToClient(new Point(e.X + threshold, e.Y));
        TreeNode bottomNode = treeViewConditions.GetNodeAt(mouseLocation);

        if (topNode != null && bottomNode == null)
        {
            textBoxDescription.Text = "handling top node";
        }
        else if (topNode == null && bottomNode != null)
        {
            textBoxDescription.Text = "handling bottom node";
        }
        else if (topNode != null && bottomNode != null)
        {
            if (topNode != bottomNode)
            {
                textBoxDescription.Text = "between!";
            }
            else if (topNode == bottomNode)
            {
            }
        }

However in doing this, it just feels dirty. I am wondering if anyone knew of a better way to do accomplish this.

Thanks a ton in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双手揣兜 2024-09-25 07:28:07

绘制“哈希标记”将是真正的问题。 TreeView 有一个 DrawMode 属性,但它的 DrawItem 事件不允许您在节点之间绘制。

您需要通过更改光标来指示将要发生的情况来处理此问题。使用 GiveFeedback 事件,将 e.UseCustomCursors 设置为 false,并将 Cursor.Current 分配给指示操作的自定义光标。

Drawing the 'hash mark' is going to be the real problem. TreeView has a DrawMode property but its DrawItem event doesn't let you draw between the nodes.

You need to handle this by changing the cursor to indicate what is going to happen. Use the GiveFeedback event, set e.UseCustomCursors to false and assign Cursor.Current to a custom cursor that indicates the operation.

爱人如己 2024-09-25 07:28:07

本文阐明了相同的问题,并提供了一种与您已经遵循的方法有些相似的方法(除了阈值本质上是树节点高度的百分比)。基于此,以及当我之前这样做时,这是我能找到的最佳方法这一事实,我认为您基本上已经步入正轨。

This article articulates the same issue and provides an approach somewhat similar to the one you are already following (with the exception that the thresholds are essentially percentages of the height of the tree node). Based on this, and the fact that when I was doing this before, that was the best approach I could find, I think you're basically on track.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文