TreeView中的HotTracking无法使用Drag& Drop

时间:2011-09-14 12:29:51

标签: .net vb.net drag-and-drop treeview

我有一个简单的TreeView,其HotTracking属性设置为True。当我将鼠标移到项目上时它会起作用。但是当我在TreeView项目上使用Drag& Drop和Drag某些东西时,它不起作用。有没有办法使用DragDrop的HotTracking?或者任何解决方法?

2 个答案:

答案 0 :(得分:1)

按照设计,控件只能获得D + D通知,没有鼠标消息。您可以自己选择节点:

    private void treeView1_DragOver(object sender, DragEventArgs e) {
        if (e.Data.GetDataPresent("something")) {
            var pos = treeView1.PointToClient(new Point(e.X, e.Y));
            var hit = treeView1.HitTest(pos);
            if (hit.Location == TreeViewHitTestLocations.Label) {
                treeView1.SelectedNode = hit.Node;
                e.Effect = DragDropEffects.Copy;
            }
            else e.Effect = DragDropEffects.None;
        }
    }

您通常需要做更多工作才能让用户滚动视图并展开节点。

答案 1 :(得分:0)

选择节点为@Hans建议是一个简单的解决方案,但如果节点选择上有某些事件,或者您只需要保留所选节点,则可能无法使用。 我发现的唯一方法是使用VisualStylesRenderer对该节点进行custumDraw。

private void plTree_DragOver(object sender, DragEventArgs e)
    {            
        Point pt = plTree.PointToClient(new Point(e.X, e.Y));

        var overNode = plTree.GetNodeAt(pt);
        bool canMoveTo = overNode != null && 
                         TreeNodeIsFolder(overNode) && 
                         !IsFirstNodeParentToSecond(GetDraggedNodes(e)[0], overNode);// all dragged nodes are of the same parent
        e.Effect = canMoveTo ? DragDropEffects.Move : DragDropEffects.None;

        if (overNode == null) return;

        if (_lastCustomDrawNode != overNode)
        {
            NormalDrawNode(_lastCustomDrawNode);
            CustomDrawNode(overNode);
        }
        _lastCustomDrawNode = overNode;
    }

    private void NormalDrawNode(TreeNode aNode)
    {
        if (aNode == null) return;

        if (_normalNodeRenderer == null)
            _normalNodeRenderer = new VisualStyleRenderer("Explorer::TreeView", 1, 1);
        Debug.WriteLine("normal render" + DateTime.Now);
        RenderTreeNode(_normalNodeRenderer, aNode, true);

        _lastCustomDrawNode = null;
    }

    private void CustomDrawNode(TreeNode aNode)
    {
        if (_hotNodeRenderer == null)
            _hotNodeRenderer = new VisualStyleRenderer("Explorer::TreeView", 1, 2);
        Debug.WriteLine("custom render" + DateTime.Now);
        RenderTreeNode(_hotNodeRenderer, aNode, false);
    }

    private void RenderTreeNode(VisualStyleRenderer renderer, TreeNode aNode, bool drawDefaultBck)
    {
        if(_treeGraphics == null)
             _treeGraphics = plTree.CreateGraphics();

        const int iconShift = 21;
        Rectangle aRec = aNode.Bounds;

        var bckRec = new Rectangle(aRec.Location.X - iconShift, aRec.Location.Y, aRec.Width + iconShift, aRec.Height);

        if (!drawDefaultBck)
            renderer.DrawBackground(_treeGraphics, bckRec);
        else
            _treeGraphics.FillRectangle(_treebckgBrush, bckRec);

        var textRec = new Rectangle(aRec.Location.X + 1, aRec.Location.Y - 1, aRec.Width, aRec.Height);
        TextRenderer.DrawText(_treeGraphics, aNode.Text, GetTreeNodeFont(aNode), textRec, Color.FromKnownColor(KnownColor.ControlText));

        var imgRec = new Rectangle(aRec.Location.X + 2 - iconShift, aRec.Location.Y, 16, 16);
        renderer.DrawImage(_treeGraphics, imgRec, ImageListTree.Images[aNode.ImageIndex]);
    }

这篇文章对主题绘图有很大帮助: VisualStyleRenderer and themes (WinForms)