c# – 使用拖放方式时,是否可以使TreeView展开用户悬停的节点?

简单来说:

当拖放操作正在进行中时,.Net 2.0中是否有任何内置函数在悬停时展开TreeNode?

我在Visual Studio 2005中使用C#.

更详细地说:

我已经使用多层次的多重树(考虑组织图或文件/文件夹对话框)填充了Treeview控件,我想使用拖放来移动树中的节点.

拖放代码工作正常,我可以放到任何可见的节点上,但是我希望我的控件像Windows Explorer一样在文件夹窗格中拖动文件时的行为.具体来说,我希望每个文件夹打开,如果悬停在1/2秒左右.

我已经开始使用线程和睡眠方法开发解决方案,但是我遇到了问题,并且想知道是否已经有了某些功能,如果不是我会指点并学习如何使用线程(这是关于时间,但我是希望快速得到这个应用程序)

在拖放模式下悬停时,需要编写自己的代码来处理扩展树节点吗?

解决方法

您可以使用DragOver事件;当您拖动对象时,它会反复触发
延迟后打开可以非常容易地使用两个额外的变量来记录鼠标下的最后一个对象和时间.不需要线程或其他技巧(在我的例子中,lastDragDestination和lastDragDestinationTime)

从我自己的代码

TreeNode lastDragDestination = null;
DateTime lastDragDestinationTime;

private void tvManager_DragOver(object sender,DragEventArgs e)
{
    IconObject dragDropObject = null;
    TreeNode dragDropNode = null;

    //always disallow by default
    e.Effect = DragDropEffects.None;

    //make sure we have data to transfer
    if (e.Data.GetDataPresent(typeof(TreeNode)))
    {
        dragDropNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
        dragDropObject = (IconObject)dragDropNode.Tag;
    }
    else if (e.Data.GetDataPresent(typeof(ListViewItem)))
    {
        ListViewItem temp (ListViewItem)e.Data.GetData(typeof(ListViewItem));
        dragDropObject = (IconObject)temp.Tag;
    }

    if (dragDropObject != null)
    {
        TreeNode destinationNode = null;
        //get current location
        Point pt = new Point(e.X,e.Y);
        pt = tvManager.PointToClient(pt);
        destinationNode = tvManager.GetNodeAt(pt);
        if (destinationNode == null)
        {
            return;
        }

        //if we are on a new object,reset our timer
        //otherwise check to see if enough time has passed and expand the destination node
        if (destinationNode != lastDragDestination)
        {
            lastDragDestination = destinationNode;
            lastDragDestinationTime = DateTime.Now;
        }
        else
        {
            TimeSpan hoverTime = DateTime.Now.Subtract(lastDragDestinationTime);
            if (hoverTime.TotalSeconds > 2)
            {
                destinationNode.Expand();
            }
        }
    }
}

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...