C#Drag&;在画布中放置多个图像
我有以下代码可以在画布中拖放图像:C#Drag&;在画布中放置多个图像,c#,wpf,canvas,drag-and-drop,draggable,C#,Wpf,Canvas,Drag And Drop,Draggable,我有以下代码可以在画布中拖放图像: img.AllowDrop = true; img.PreviewMouseLeftButtonDown += this.MouseLeftButtonDown; img.PreviewMouseMove += this.MouseMove; img.PreviewMouseLeftButtonUp += this.PreviewMouseLeftButtonUp; private object movingObject; private double f
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);
}
}
img.AllowDrop=true;
img.PreviewMouseLeftButtonDown+=this.MouseLeftButtonDown;
img.PreviewMouseMove+=this.MouseMove;
img.PreviewMouseLeftButtonUp+=此.PreviewMouseLeftButtonUp;
私有对象移动对象;
私人双FirstXPO、FirstYPO;
私有void MouseLeftButtonDown(对象发送器,MouseButtonEventArgs e){
//在本事件中,我们获取控件上的当前鼠标位置,以便在MouseMove事件中使用它。
图像img=发送者作为图像;
Canvas Canvas=img.Parent作为画布;
firstXPos=e.GetPosition(img).X;
firstYPos=e.GetPosition(img).Y;
movingObject=发送方;
//将当前正在拖动的图像置于其他图像之上
inttop=Canvas.GetZIndex(img);
foreach(canvas.Children中的图像子对象)
if(顶部Canvas.GetZIndex(子级))
top=Canvas.GetZIndex(子对象);
Canvas.SetZIndex(img,top+1);
}
私有void MouseMove(对象发送方,MouseEventArgs e){
如果(e.LeftButton==鼠标按钮状态。按下&&sender==移动对象){
图像img=发送者作为图像;
Canvas Canvas=img.Parent作为画布;
double newLeft=e.GetPosition(canvas.X-firstXPos-canvas.Margin.Left;
//新左内画布右边框?
if(newLeft>canvas.Margin.Left+canvas.ActualWidth-img.ActualWidth)
newLeft=canvas.Margin.Left+canvas.ActualWidth-img.ActualWidth;
//新左内画布左边框?
else if(newLeftcanvas.Margin.Top+canvas.ActualHeight-img.ActualHeight)
newTop=canvas.Margin.Top+canvas.ActualHeight-img.ActualHeight;
//画布顶部边框内的newTop?
else if(newTop
这段代码允许我在画布中拖放图像,而无需离开画布本身
现在我只需要再做两件事:
您需要将鼠标锁定在图像上,以防止光标从鼠标上滑落。捕获(imageReference),您可以在鼠标上用鼠标释放它。捕获(null)-安迪2月18日15:58 在使用Andy的建议后,添加:
Mouse.Capture(img);
在MouseLeftButtonDown函数的底部,以及
Mouse.Capture(null);
在PreviewMouseLeftButtonUp函数的底部,它就像一个符咒<所以非常感谢安迪您需要将鼠标锁定在图像上,以防止光标从鼠标上滑落。捕获(imageReference),您可以在鼠标上用鼠标释放它。捕获(null)@Andy非常感谢,这解决了我的问题!将其添加为下面的答案。