2014-02-18 51 views
3

多张图片我有下面的代码来拖拽图片我的画布里面:C#拖放内帆布

img.AllowDrop = true; 
img.PreviewMouseLeftButtonDown += this.MouseLeftButtonDown; 
img.PreviewMouseMove += this.MouseMove; 
img.PreviewMouseLeftButtonUp += this.PreviewMouseLeftButtonUp; 


private object movingObject; 
private double firstXPos, firstYPos; 
private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { 
    // In this event, we get the current mouse position on the control to use it in the MouseMove event. 
    Image img = sender as Image; 
    Canvas canvas = img.Parent as Canvas; 

    firstXPos = e.GetPosition(img).X; 
    firstYPos = e.GetPosition(img).Y; 

    movingObject = sender; 

    // Put the image currently being dragged on top of the others 
    int top = Canvas.GetZIndex(img); 
    foreach (Image child in canvas.Children) 
     if (top < Canvas.GetZIndex(child)) 
      top = Canvas.GetZIndex(child); 
    Canvas.SetZIndex(img, top + 1); 
} 
private void PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { 
    Image img = sender as Image; 
    Canvas canvas = img.Parent as Canvas; 

    movingObject = null; 

    // Put the image currently being dragged on top of the others 
    int top = Canvas.GetZIndex(img); 
    foreach (Image child in canvas.Children) 
     if (top > Canvas.GetZIndex(child)) 
      top = Canvas.GetZIndex(child); 
    Canvas.SetZIndex(img, top + 1); 
} 
private void MouseMove(object sender, MouseEventArgs e) { 
    if (e.LeftButton == MouseButtonState.Pressed && sender == movingObject) { 
     Image img = sender as Image; 
     Canvas canvas = img.Parent as Canvas; 

     double newLeft = e.GetPosition(canvas).X - firstXPos - canvas.Margin.Left; 
     // newLeft inside canvas right-border? 
     if (newLeft > canvas.Margin.Left + canvas.ActualWidth - img.ActualWidth) 
      newLeft = canvas.Margin.Left + canvas.ActualWidth - img.ActualWidth; 
     // newLeft inside canvas left-border? 
     else if (newLeft < canvas.Margin.Left) 
      newLeft = canvas.Margin.Left; 
     img.SetValue(Canvas.LeftProperty, newLeft); 

     double newTop = e.GetPosition(canvas).Y - firstYPos - canvas.Margin.Top; 
     // newTop inside canvas bottom-border? 
     if (newTop > canvas.Margin.Top + canvas.ActualHeight - img.ActualHeight) 
      newTop = canvas.Margin.Top + canvas.ActualHeight - img.ActualHeight; 
     // newTop inside canvas top-border? 
     else if (newTop < canvas.Margin.Top) 
      newTop = canvas.Margin.Top; 
     img.SetValue(Canvas.TopProperty, newTop); 
    } 
} 

此代码让我可以拖动和删除画布中的图像,无需离开Canvas本身。

现在我只需要能够做两两件事:

  1. 修正了一个小错误在我的形象的鼠标滑动时,我拖累他们身边快速。这种情况经常发生,即使我甚至没有快速移动拖动图像。
  2. 使其能够一次拖放多个图像,最好先选择多个图像,然后拖放并拖放图像,在帆布内停留时,将其全部放入其中。

PS:我以前的问题可以找到here

+2

您需要将鼠标锁定到图像上以停止光标滑动Mouse.Capture(imageReference),您可以使用Mouse.Capture(null)在鼠标上释放它(安装) – Andy

+0

@Andy非常感谢,这解决了我的问题!在下面添加它作为答案。 –

回答

3

您需要将鼠标锁定到图像上以停止光标滑动Mouse.Capture(imageReference),您可以通过Mouse.Capture(null)在鼠标上释放它 - Andy Feb 18在15:58

后使用Andy的建议,通过添加:

Mouse.Capture(img); 

在的MouseLeftButtonDown函数的底部,并

Mouse.Capture(null); 

位于PreviewMouseLeftButtonUp函数的底部,它的工作原理类似于魅力。 非常感谢Andy!