本文介绍了C#拖&在画布中放置多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码可以在Canvas内拖放图像:
I've got the following code to drag and drop images inside my 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);
}
}
此代码允许我拖放画布中的图片,而不会离开画布本身。
This code allows me to drag-and-drop the Images inside the Canvas, without leaving the Canvas itself.
现在我只需要再做两件事:
Now I just need to be able to do two more things:
- 当我将鼠标拖动到快速位置时,修复了鼠标滑动图像的一个小错误。这种情况经常发生,即使我甚至不能快速移动拖动图像。
- 使其能够一次拖放多个图像,最好选择多个首先,然后将它们拖放到Canvas内。
PS:我以前的问题可以是发现。
PS: My previously question can be found here.
推荐答案
使用Andy的建议之后,添加:
After using Andy's suggestion, by adding:
Mouse.Capture(img);
位于MouseLeftButtonDown功能的底部,
at the bottom of the MouseLeftButtonDown-function, and
Mouse.Capture(null);
在PreviewMouseLeftButtonUp函数的底部,它的作用就像一个魅力。 所以非常感谢Andy!
at the bottom of the PreviewMouseLeftButtonUp-function, it works like a charm. So thanks a lot Andy!
这篇关于C#拖&在画布中放置多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!