2014-02-18 90 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!