开发者

C# DragEnter event of the Winforms DragDrop

Why do we actually need to handle DragEnter event of the drop destination?

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.Li开发者_如何学运维nk);
        }
    }

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:

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.

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. 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). So that's why the image doesn't drop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜