简体   繁体   English

C#WinForm TreeView拖放效果

[英]c# winform treeview Dragdrop effect

I would like to indicate to the user that they are trying to drag to wrong node level by changing the icon on the mouse. 我想向用户表明,他们试图通过更改鼠标上的图标将其拖到错误的节点级别。 I can't seem to get a handle on this...any suggestions? 我似乎无法解决这个问题……有什么建议吗?

Dropping on a treeview is an iffy proposition, the node that the user would want to drop on might not be visible. 放到树视图上是一个有争议的主张,用户想要放到其上的节点可能不可见。 Either because it is collapsed and hidden or scrolled off the screen. 因为它已折叠,隐藏或滚动到屏幕之外。 Anyhoo, you want to use the DragOver event and check where the mouse is located. 不管怎么说,您想使用DragOver事件并检查鼠标的位置。 Here's a sample form that does this. 这是执行此操作的示例表单。 Drop a treeview on it and add some nodes. 在其上放置一个treeview并添加一些节点。 Click on the form and drag. 单击表格并拖动。 It only allows drops on the 2nd level nodes: 它仅允许在第二级节点上丢弃:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        treeView1.AllowDrop = true;
        treeView1.DragEnter += treeView1_DragEnter;
        treeView1.DragOver += treeView1_DragOver;
    }
    protected override void OnMouseDown(MouseEventArgs e) {
        DoDragDrop("foo", DragDropEffects.Copy);
        base.OnMouseDown(e);
    }
    private void treeView1_DragEnter(object sender, DragEventArgs e) {
        // TODO: check e.Data
        e.Effect = DragDropEffects.Copy;
    }
    private void treeView1_DragOver(object sender, DragEventArgs e) {
        Point pos = treeView1.PointToClient(new Point(e.X, e.Y));
        var hit = treeView1.HitTest(pos);
        TreeNode node = hit.Node;
        if (hit.Node != null) {
            node.Expand();
            if (node.Level != 1) node = null;
        }
        e.Effect = node != null ? DragDropEffects.Copy : DragDropEffects.None;
    }
}

Check the http://msdn.microsoft.com/en-us/library/system.windows.forms.drageventargs.aspx for the documentation of DragEventArgs. 检查http://msdn.microsoft.com/zh-cn/library/system.windows.forms.drageventargs.aspx ,以获取DragEventArgs的文档。 You should set the effect to none: 您应该将效果设置为none:

  e.Effect = DragDropEffects.None;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM