编程语言
首页 > 编程语言> > C#在画布中拖放多个图像

C#在画布中拖放多个图像

作者:互联网

我有以下代码将图像拖放到Canvas中:

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内,而无需离开Canvas本身.

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

>修复一个小错误,当我快速拖动鼠标时,鼠标会滑过图像.即使我什至没有快速移动拖动图像,也经常发生这种情况.
>使其能够一次拖放多个图像,最好是先选择多个图像,然后再将整堆图像拖放到画布中,同时保留在画布内.

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

解决方法:

You’ll want to lock the mouse on to the image to stop the cursor slipping off it Mouse.Capture (imageReference), you can release it on mouse up with Mouse.Capture (null) – Andy Feb 18 at 15:58

在使用Andy的建议之后,添加以下内容:

Mouse.Capture(img);

在MouseLeftButtonDown函数的底部,然后

Mouse.Capture(null);

在PreviewMouseLeftButtonUp函数底部,它就像一个超级按钮.非常感谢Andy!

标签:canvas,drag-and-drop,draggable,wpf,c
来源: https://codeday.me/bug/20191122/2057020.html