如何强制树视图滚动条拇指在拖动任何节点时向上/向下滚动

Jiale Xue - MSFT 36,471 信誉分 Microsoft 供应商
2024-03-26T08:25:52.35+00:00

我的树视图有巨大的节点。我使拖放节点成为可能。当我在最后一个节点上并尝试将该节点向上拖动时,TreeView 滚动条不会向上移动。因此,当我将底部节点拖动到向上时,请指导我如何强制 TreeView 滚动条向上移动,并且我想将 TreeView 顶部节点向下拖动,然后滚动条应该向下移动。

请帮助我使用示例代码。

Note:此问题总结整理于:How to force Treeview scrollbar thumb scroll up/down when dragging any node

Windows 窗体
Windows 窗体
一组用于开发图形用户界面的 .NET Framework 托管库。
99 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 41,256 信誉分 Microsoft 供应商
    2024-03-26T11:20:17.1966667+00:00

    首先,我使用以下代码将您所取得的成就拖放到节点上。

    
    private void Form1_Load(object sender, EventArgs e)  
    {  
        this.treeView1.ItemDrag += new ItemDragEventHandler(this.treeView1_ItemDrag);  
        this.treeView1.DragDrop += new DragEventHandler(this.treeView1_DragDrop);  
        this.treeView1.DragEnter += new DragEventHandler(this.treeView1_DragEnter);  
    }  
    private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)  
    {  
        DoDragDrop(e.Item, DragDropEffects.Move);  
        var _selectedNode = (TreeNode)e.Item;  
    }  
    private void treeView1_DragEnter(object sender, DragEventArgs e)  
    {  
        e.Effect = DragDropEffects.Move;  
    }  
    private void treeView1_DragDrop(object sender, DragEventArgs e)  
    {  
        // Retrieve the client coordinates of the drop location.  
        Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));  
      
        // Retrieve the node at the drop location.  
        TreeNode targetNode = treeView1.GetNodeAt(targetPoint);  
      
        // Retrieve the node that was dragged.  
        TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode));  
      
        // Confirm that the node at the drop location is not   
        // the dragged node and that target node isn't null  
        // (for example if you drag outside the control)  
        if (!draggedNode.Equals(targetNode) && targetNode != null)  
        {  
            // Remove the node from its current   
            // location and add it to the node at the drop location.  
            draggedNode.Remove();  
            targetNode.Nodes.Add(draggedNode);  
      
            // Expand the node at the location   
            // to show the dropped node.  
            targetNode.Expand();  
        }  
    }  
    

    然后,为了告诉树视图向上或向下滚动,您需要调用 Windows API SendMessage() 函数。 接下来,确定鼠标光标相对于 DragScroll 事件中树视图控件的顶部和底部的位置。然后调用 SendMessage 以适当方式滚动。 下面是一个可以参考的代码示例。

    public static class NativeMethods  
    {  
        [DllImport("user32.dll", CharSet = CharSet.Auto)]  
        internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);  
      
        public static void Scroll(this Control control)  
        {  
            var pt = control.PointToClient(Cursor.Position);  
      
            if ((pt.Y + 20) > control.Height)  
            {  
                // scroll down  
                SendMessage(control.Handle, 277, (IntPtr)1, (IntPtr)0);  
            }  
            else if (pt.Y < 20)  
            {  
                // scroll up  
                SendMessage(control.Handle, 277, (IntPtr)0, (IntPtr)0);  
            }  
        }  
    } 
     
    

    测试结果:

    41781-1123.gif


    如果回复有帮助,请点击“接受答案”并点赞。 注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助