开发者

HotTracking in TreeView is not working with Drag&Drop

开发者 https://www.devze.com 2023-04-05 00:14 出处:网络
I have a simple TreeView with HotTracking Property set to True. It works when I move the mouse over an item. But it doesn\'t work when I use Drag&Drop and Drag something over a TreeView Item. Is t

I have a simple TreeView with HotTracking Property set to True. It works when I move the mouse over an item. But it doesn't work when I use Drag&Drop and Drag something over a TreeView Item. Is there a way to use the HotTrack开发者_高级运维ing with DragDrop? Or any workarounds?


By design, the control only gets D+D notifications, no mouse messages. You could simply select the node yourself:

    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;
        }
    }

You typically need to do more work to let the user scroll the view and expand nodes.


Selecting a Node as @Hans suggested is a simple solution but can be unusable if there is some events on node selection, or if you just need keeping the selected node. The only way I found is custumDraw the node using VisualStylesRenderer.

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]);
    }

this post helps a lot with theme drawing: VisualStyleRenderer and themes (WinForms)

0

精彩评论

暂无评论...
验证码 换一张
取 消