2013-03-05 119 views
1

我得到Panel并在其中有一些自定义控件的拖放操作,当拖动开始时,我做了panel.Capture = True;让面板触发正确的事件,即使用户在鼠标外部释放鼠标左键(例如:panel_DragDrop),但DragDrop事件不会被触发,除非在光标位于面板范围内时释放鼠标左键。拖放鼠标捕获

我以为panel.Capture会解决这个问题,但它没有任何效果。任何想法我在这里想念什么?

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

这将使光标在拖动时绑定到面板边界,所以没有办法让光标离开边界。

对不起任何麻烦

回答

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


} 

一旦你拖动面板上,它被捕获,但主窗体仍然需要拖/丢处理程序

+0

对不起,但我不能做任何事情从你的例子。作为一个孩子尝试使用'Button'作为'Panel'。在按钮上启用D&D,启用面板上的捕获并将面板上的按钮拖出,panel1_DragDrop事件是否会触发?这是我想要实现的。 – prettyvoid 2013-03-05 20:11:58

+0

请阅读第一篇文章中的修改内容。 – prettyvoid 2013-03-05 20:39:00