C#在画布内拖放多张图片

4

我有以下代码,用于在我的画布中拖放图像:

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

这段代码允许我在画布内拖放图像,而不会离开画布本身。
现在我只需要做两件事:
1.修复一个小错误,在我快速拖动图像时我的鼠标会滑出图片。即使我没有移动拖动的图像也经常发生这种情况。
2.使其能够一次性拖放多个图像,最好是先选择多个,然后在保持在画布内的同时拖放整个组。
PS: 我之前的问题可以在此处找到。

2
你需要锁定鼠标到图像上,以防止光标滑出。使用Mouse.Capture(imageReference)进行捕获,你可以在鼠标抬起时释放它,使用Mouse.Capture(null)。 - Andy
@Andy 非常感谢,这解决了我的问题!我已将其添加为下面的答案。 - Kevin Cruijssen
1个回答

4

按照Andy的建议,添加以下代码:

你会希望锁定鼠标在图像上,以防止光标滑出。使用Mouse.Capture (imageReference),你可以在鼠标抬起时释放它,使用Mouse.Capture (null)。

Mouse.Capture(img);

在MouseLeftButtonDown函数的底部,以及
Mouse.Capture(null);

在PreviewMouseLeftButtonUp函数底部,它可以完美地工作。所以非常感谢Andy!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接