C# TreeView实现拖动节点时滚动条自动滚动

本文介绍了如何在C#中利用Windows API的SendMessage函数,在用户拖动TreeView节点时,使滚动条根据鼠标位置自动滚动。通过判断鼠标在TreeView中的位置,调用API进行上滚或下滚操作。
摘要由CSDN通过智能技术生成

You need to call the Windows API SendMessage() function.

 

 

        //using System.Runtime.InteropServices;

        [DllImport("user32.dll")]

        private static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
        


        private void treeView1_DragOver(object sender, DragEventArgs e)
        {
            // Set a constant to define the autoscroll region
            const Single scrollRegion = 20;

            // See where the cursor is
            Point pt = treeView1.PointToClient(Cursor.Position);

            // See if we need to scroll up or down
            if ((pt.Y + scrollRegion) > treeView1.Height)
            {
                // Call the API to scroll down
                SendMessage(treeView1.Handle, (int)277, (int)1, 0);
            }
            else if (pt.Y < (treeView1.Top + scrollRegion))
            {
                // Call thje API to scroll up
                SendMessage(treeView1.Handle, (int)277, (int)0, 0);
            }
        }

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值