简体   繁体   English

允许DragDrop在表单中的任何位置

[英]Allow DragDrop anywhere in a form

Is there a way to allow Drag and Drop anywhere in a form full of controls? 有没有办法允许在一个充满控件的表单中的任何位置拖放?

The idea is to allow user to drag a file anywhere in a form in order to "load" it. 我们的想法是允许用户将文件拖放到表单中的任何位置以“加载”它。 I will not need any other DragDrop behavior but this. 我不需要任何其他DragDrop行为,但这。

By setting AllowDrop=True to the form only, I get DragEnter events but not DragDrop ones. 通过将AllowDrop=True设置为仅表单,我得到DragEnter事件但不是DragDrop事件。

An idea would be to make a topmost panel visible on DragEnter and handle DragDrop events there, but I wonder if I miss something obvious here since I have little experience in the field. 一个想法是让一个最顶层的面板在DragEnter可见并在那里处理DragDrop事件,但我想知道我是否错过了一些明显的东西,因为我在该领域的经验很少。

Another Idea would be to iterate through all controls and subscribe to Drag-related events. 另一个想法是迭代所有控件并订阅与Drag相关的事件。 I really don't like this approach, though. 不过,我真的不喜欢这种方法。

Sure, iterating the controls will work, it doesn't take much code: 当然,迭代控件将起作用,它不需要太多代码:

    public Form1() {
        InitializeComponent();
        WireDragDrop(this.Controls);
    }
    private void WireDragDrop(Control.ControlCollection ctls) {
        foreach (Control ctl in ctls) {
            ctl.AllowDrop = true;
            ctl.DragEnter += ctl_DragEnter;
            ctl.DragDrop += ctl_DragDrop;
            WireDragDrop(ctl.Controls);
        }
    }

    void ctl_DragDrop(object sender, DragEventArgs e) {
        // etc..
    }

    void ctl_DragEnter(object sender, DragEventArgs e) {
        // etc..
    }

If you still don't like the approach then use a recognizable single drop target that the user will always hit. 如果您仍然不喜欢该方法,那么使用用户将始终点击的可识别的单个放置目标。 Could be as simple as a label that says "Drop here". 可以像标签“Drop here”一样简单。

I'm not sure what kinds of control you have on your form. 我不确定你在表单上有什么样的控制权。 But I've tested with a Button, a GroupBox, a PictureBox and a TextBox. 但我已经使用Button,GroupBox,PictureBox和TextBox进行了测试。 All these controls have AllowDrop = false by default. 默认情况下,所有这些控件都具有AllowDrop = false And I can drag-n-drop something from outside onto the form OK. 而且我可以将外部的东西拖放到表单上。 The DragDrop is fired OK. DragDrop被解雇了。 Everything is OK. 一切都好。 What is actually your problem? 你的问题到底是什么? I guess your controls have AllowDrop = true . 我想你的控件有AllowDrop = true

In the case the DragDrop event is not fired (which I think only happens if the target is one of your Control with AllowDrop = true ). 在没有触发DragDrop事件的情况下(我认为只有当目标是您的Control with AllowDrop = true才会发生)。 I think the following may work. 我认为以下可能有效。 But if the target is one of your Control with AllowDrop = true , the effect icon is gone away. 但是如果目标是您的Control with AllowDrop = true ,效果图标就会消失。

public Form1(){
    InitializeComponents();
    t.Interval = 1;
    t.Tick += Tick;
}
IDataObject data;
Timer t = new Timer();
int i = 0;
private void Tick(object sender, EventArgs e)
{
     Text = (i++).ToString();
     if (ClientRectangle.Contains(PointToClient(new Point(MousePosition.X, MousePosition.Y))) && MouseButtons == MouseButtons.None)
     {
        t.Stop();
        if (data != null)
        {
           //Process data here
           //-----------------               
           data = null;
        }                                
     }
     else if (MouseButtons == MouseButtons.None)
     {
        data = null;
        t.Stop();
     }
}

private void Form1_DragEnter(object sender, DragEventArgs e)
{
   e.Effect = e.AllowedEffect;
   if (data == null)
   {
       data = e.Data;
       t.Start();
   }
}

And I think you may have to use the loop through all the Controls to add appropriate event handlers. 我认为您可能必须使用所有控件的循环来添加适当的事件处理程序。 There is no other better way. 没有其他更好的方法。

In the Drop event. Drop事件中。

string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
        foreach (string file in files) Console.WriteLine(file);

In the DragEnter event. DragEnter事件中。

if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effects = DragDropEffects.Copy;

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

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