1

I got a Panel and inside it some drag and drop operations of custom controls, when the dragging begins I do panel.Capture = True; for the panel to fire the proper events even when the user releases the left mouse button outside the panel bounds (e.g.: panel_DragDrop) but the DragDrop event isn't getting fired unless the left mouse button is released while the cursor is located within the bounds of the panel.

I thought panel.Capture would solve this but it's not doing any effect. Any ideas what am I missing here?

Edit: Ok I think I know what I have to do now. I think I had a misunderstanding of the DragDrop event. What I have in my application is dragging of controls inside the panel only ( think of it as moving blocks ), while the user is dragging a block and goes out of bounds, I auto scroll, if the cursor goes outside the panel, the panel_DragDrop never gets called and the placement of the block doesn't take place if the mouse gets released. I think my solution is this: Cursor.Clip = panelDiagram.RectangleToScreen(panelDiagram.ClientRectangle);

This will make the cursor bound to the panel boundaries while dragging, so no way to take the cursor off bounds.

Sorry for any troubles

4

1 回答 1

0

我刚刚创建了一个只有一个表单和一个面板的小测试项目,这对我有用:

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{

    public Form1()
    {
        DragDrop += DD;
        DragEnter += Panel1DragEnter;
        // This call is required by the designer.
        InitializeComponent();

        // Add any initialization after the InitializeComponent() call.
        Panel1.AllowDrop = true;
        AllowDrop = true;

    }

    private void Panel1DragEnter(System.Object sender, System.Windows.Forms.DragEventArgs e)
    {
        if (object.ReferenceEquals(sender, Panel1)) {
            Panel1.Capture = true;
        }

        if (Panel1.Capture) {
            if ((e.Data.GetDataPresent(DataFormats.FileDrop))) {
                e.Effect = DragDropEffects.Copy;
            } else {
                e.Effect = DragDropEffects.None;
            }
            Panel1.Capture = true;
        }

    }

    private void DD(object sender, DragEventArgs e)
    {
        if (Panel1.Capture) {
            Interaction.MsgBox("dropped");
        }
        Panel1.Capture = false;
    }


}

一旦你拖过面板,它就会被捕获,但主窗体仍然需要有拖放处理程序

于 2013-03-05T18:59:42.930 回答