C# 拖放鼠标捕获

C# 拖放鼠标捕获,c#,.net,winforms,drag-and-drop,panel,C#,.net,Winforms,Drag And Drop,Panel,我得到了一个面板,其中包含一些自定义控件的拖放操作,当拖动开始时,我执行Panel.Capture=True,即使用户在面板边界外释放鼠标左键(例如:panel_DragDrop),面板也会触发适当的事件,但除非在光标位于面板边界内时释放鼠标左键,否则不会触发DragDrop事件 我原以为panel.Capture可以解决这个问题,但没有任何效果。你知道我在这里遗漏了什么吗 编辑:好的,我想我知道我现在要做什么了。我想我对DragDrop事件有误解。在我的应用程序中,我只在面板内拖动控件(将其视

我得到了一个
面板
,其中包含一些自定义控件的拖放操作,当拖动开始时,我执行
Panel.Capture=True
,即使用户在面板边界外释放鼠标左键(例如:
panel_DragDrop
),面板也会触发适当的事件,但除非在光标位于面板边界内时释放鼠标左键,否则不会触发
DragDrop
事件

我原以为
panel.Capture
可以解决这个问题,但没有任何效果。你知道我在这里遗漏了什么吗

编辑:好的,我想我知道我现在要做什么了。我想我对DragDrop事件有误解。在我的应用程序中,我只在面板内拖动控件(将其视为移动块),当用户拖动块并超出边界时,如果光标超出面板,我会自动滚动,
面板\u DragDrop
永远不会被调用,如果释放鼠标,块的放置也不会发生。我认为我的解决方案是:
Cursor.Clip=panelDiagram.RectangleToScreen(panelDiagram.ClientRectangle)

这将使光标在拖动时绑定到面板边界,因此无法使光标脱离边界


很抱歉出现任何问题

我刚刚创建了一个小测试项目,它只包含一个表单和一个面板,这对我来说很有用:

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;
    }


}

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

很抱歉,我不能从你的示例中得出任何结论。作为孩子,尝试使用带有
按钮的
面板
。在按钮上启用D&D,在面板上启用捕获并将按钮拖到面板外,panel1_DragDrop事件是否会触发?这就是我想要达到的目标。