简体   繁体   English

拖放鼠标捕获

[英]Drag and drop mouse capture

I got a Panel and inside it some drag and drop operations of custom controls, when the dragging begins I do panel.Capture = True; 我得到一个Panel并在其中添加了自定义控件的一些拖放操作,当开始拖动时,我执行了panel.Capture = True; for the panel to fire the proper events even when the user releases the left mouse button outside the panel bounds (eg: 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. 即使用户在面板边界之外释放鼠标左键(例如: panel_DragDrop ),面板也将触发适当的事件,但是除非光标在边界内时释放鼠标左键,否则不会触发DragDrop事件面板的。

I thought panel.Capture would solve this but it's not doing any effect. 我以为panel.Capture可以解决这个问题,但是没有任何效果。 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. 我认为我对DragDrop事件有误解。 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. 我在应用程序中所拥有的只是在面板内部拖动控件(将其视为移动的块),而当用户拖动一个块并超出范围时,我会自动滚动,如果光标移到面板之外,则panel_DragDrop如果释放鼠标,则永远不会调用,并且不会放置该块。 I think my solution is this: Cursor.Clip = panelDiagram.RectangleToScreen(panelDiagram.ClientRectangle); 我认为我的解决方案是这样的: 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 对不起有什么麻烦

I just created a little test project with just a form and a panel and this worked for me: 我刚刚创建了一个仅包含表单和面板的小测试项目,这对我有用:

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


}

Once you've dragged over the Panel, its been captured, but the Main Form still needs to have drag/drop handlers 一旦将鼠标拖到面板上,便会捕获它,但是主窗体仍需要具有拖放处理程序

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

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