简体   繁体   English

Winforms DragDrop的C#DragEnter事件

[英]C# DragEnter event of the Winforms DragDrop

Why do we actually need to handle DragEnter event of the drop destination? 为什么我们实际上需要处理放置目标的DragEnter事件?

What is its effect at the destination? 它在目的地的影响是什么?

At Source 来源

public partial class ToolBoxForm : System.Windows.Forms.Form
    {
        public ToolBoxForm()
        {
            InitializeComponent();
        }

        private void lbl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Label lbl = (Label)sender;
            lbl.DoDragDrop(lbl.Image, DragDropEffects.Link);
        }
    }

At Destination: 在目的地:

public partial class DrawingArea : Form
    {
        public DrawingArea()
        {
            InitializeComponent();
        }

        private void DrawingArea_Load(object sender, System.EventArgs e)
        {
            ToolBoxForm toolBoxForm = new ToolBoxForm();
            this.AddOwnedForm(toolBoxForm);
            toolBoxForm.Show();

            pictureBox1.AllowDrop = true;
        }

        private void picDrawingArea_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                e.Effect = DragDropEffects.Copy;                
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void picDrawingArea_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            Graphics g = pictureBox1.CreateGraphics();
            g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap), new Point(e.X - this.Left - 12, e.Y - this.Top - 30));
        }
    }

When I am commenting out the code: 当我评论代码时:

if (e.Data.GetDataPresent(DataFormats.Bitmap))
                {
                    e.Effect = DragDropEffects.Copy;                
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }

The image is not being dropped. 图像未被删除。

From to the DragDropEffects MSDN Page : DragDropEffects MSDN页面

Member 会员
name 名称 Description 描述
None 没有 The drop target does not accept the data. 放置目标不接受数据。
Copy 复制 The data from the drag source is copied to the drop target. 来自拖动源的数据将复制到放置目标。
Move 移动 The data from the drag source is moved to the drop target. 来自拖动源的数据将移动到放置目标。
Link 链接 The data from the drag source is linked to the drop target. 来自拖动源的数据链接到放置目标。
Scroll 滚动 The target can be scrolled while dragging to locate a drop position that is not currently visible in the target. 拖动时可以滚动目标以找到当前在目标中不可见的放置位置。
All 所有 The combination of the Copy, Move, and Scroll effects. 复制,移动和滚动效果的组合。

So you've got to set it something other than None if you want to accept the drop. 所以如果你想接受掉落,你必须设置除None以外的东西。

However, the next quote led me to believe that it was just used for feedback: 但是,下一个引用让我相信它只是用于反馈:

You can use DragDropEffects to display different mouse pointers for drag-and-drop operations. 您可以使用DragDropEffects显示不同的鼠标指针以进行拖放操作。 For example, you can display a plus symbol for a Copy drag-and-drop operation, an arrow symbol for a Move drag-and-drop operation, or a red circle with a line through it symbol for a None drag-and-drop operation. 例如,您可以显示“ 复制”拖放操作的加号,“ 移动”拖放操作的箭头符号,或者带有通过它的线符号的红色圆圈,用于“ 拖放”操作操作。

I really think the Drop event won't fire if DragDropEvents is left at None (which is the default). 我真的认为如果DragDropEvents保留为None(这是默认值),Drop事件将不会触发。 So that's why the image doesn't drop. 这就是图像不会掉落的原因。

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

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