拖放鼠标捕获

时间:2013-03-05 16:53:21

标签: c# .net winforms drag-and-drop panel

我有一个Panel并且里面有一些自定义控件的拖放操作,当拖动开始时我会panel.Capture = True;让面板触发正确的事件,即使用户释放鼠标左键面板边界外的按钮(例如:panel_DragDrop)但DragDrop事件未被触发,除非在光标位于面板边界内时释放鼠标左键。

我认为panel.Capture会解决这个问题,但它没有起到任何作用。有什么想法我在这里缺少什么?

编辑:好的我想我知道我现在要做什么。我想我对DragDrop事件有误解。我在我的应用程序中只是拖动面板内的控件(将其视为移动块),而用户拖动一个块并超出界限,我自动滚动,如果光标移出面板,{ {1}}永远不会被调用,如果鼠标被释放,则不会发生块的放置。我认为我的解决方案是这样的: panel_DragDrop

这会使光标在拖动时绑定到面板边界,因此无法将光标移开边界。

对不起任何麻烦

1 个答案:

答案 0 :(得分: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;
    }


}

一旦你拖过Panel,它就会被捕获,但主表单仍需要拖放处理程序