我正在尝试使用鼠标事件在 Canvas(在 WPF 应用程序中)中移动一个矩形,但它不起作用
I'm trying to move a Rectangle in a Canvas ( in a WPF app) using the Mouse event but it don't work
我开发了一个 wpf 应用程序,我必须在 canvas 中移动一个矩形(最终目标是确保我的矩形不能移出 canvas)。所以我在网上搜索并尝试了很多解决方案,但我还没有工作。然后我尝试重写一个解决方案,但是没有成功。
<Grid x:Name="mainGrid">
<Canvas x:Name="CanvasImplant" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="27,27,26,0" Height="415">
<Rectangle x:Name="MovableShape" Opacity="0.85" Width="93" Height="62" HorizontalAlignment="left" VerticalAlignment="Top" Margin="930,140" Stroke="Black" StrockThickness="1" MouseDown="MovShp_MouseDown" MouseUp="MovShp_MouseUp" MouseMove="MovShp_MouseMove"/>
</Canvas>
</Grid>
private bool drag = true;
private Point StartPt;
private double newX, newY;
private void MovShp_MouseDown(object sender, MouseButtonEventArgs e)
{
drag = true;
Cursor = Cursors.Hand;
startPt = e.Getposition(CanvasImplant);
Mouse.Capture((UIElement)sender);
}
private void MovShp_MouseUp(object sender, MouseButtonEventArgs e)
{
drag = false;
Cursor = Cursors.Arrow;
Mouse.Capture(null);
}
private void MovShp_MouseMove(object sender, MouseButtonEventArgs e)
{
if (drag)
{
double deltaX = e.GetPosition(CanvasImplant).X - startPt.X;
double deltaY = e.GetPosition(CanvasImplant).Y - startPt.Y;
newX = deltaX + Canvas.GetLeft(MovableShape);
newY = deltaY + Canvas.GetTop(MovableShape);
if (newX < 0)
newX = 0;
else if (newX + MovableShape.ActualWidth > CanvasImplant.ActualWidth)
newX = CanvasImplant.ActualWidth - MovableShape.ActualWidth;
if (newY < 0)
newY = 0;
else if (newY + MovableShape.ActualHeight > CanvasImplant.ActualHeight )
newY = CanvasImplant.ActualHeight - MovableShape.ActualHeight ;
MovableShape.SetValue(Canvas.LeftProperty, newX);
MovableShape.SetValue(Canvas.TopProperty, newY);
}
}
当我点击MovableShape时,鼠标变成一只手,当我松开鼠标左键时,它变成一个箭头,但是当我拖动矩形时,它不跟随鼠标,当我释放鼠标时,它不动。
有几件事。
- 你需要在形状上设置填充,否则你点击
事件仅在非常细的边界上触发
- 鼠标移动和鼠标弹起事件需要是Canvas事件,而不是
形状
- 每次都需要将startPt设置到新的位置
移动事件处理程序
- 您需要删除形状的边距和 horizontal/vertical 位置
- 您需要使用 Canvas 而不是形状
来捕获鼠标
这对我有用:
<Grid x:Name="mainGrid" >
<Canvas x:Name="CanvasImplant" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MouseMove="MovShp_MouseMove" MouseUp="MovShp_MouseUp" Background="Transparent">
<Rectangle Canvas.Left="10" Canvas.Top="20" x:Name="MovableShape" Fill="Transparent" Opacity="0.85" Width="93" Height="62" Stroke="Black"
StrokeThickness="1" MouseDown="MovShp_MouseDown" />
</Canvas>
</Grid>
处理人员:
private void MovShp_MouseDown(object sender, MouseButtonEventArgs e)
{
drag = true;
Cursor = Cursors.Hand;
startPt = e.GetPosition(CanvasImplant);
Mouse.Capture(CanvasImplant);
}
private void MovShp_MouseUp(object sender, MouseButtonEventArgs e)
{
drag = false;
Cursor = Cursors.Arrow;
Mouse.Capture(null);
}
private void MovShp_MouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
var mp = e.GetPosition(CanvasImplant);
double deltaX = mp .X - startPt.X;
double deltaY = mp .Y - startPt.Y;
var newX = deltaX + Canvas.GetLeft(MovableShape);
var newY = deltaY + Canvas.GetTop(MovableShape);
if (newX < 0)
newX = 0;
else if (newX + MovableShape.ActualWidth > CanvasImplant.ActualWidth)
newX = CanvasImplant.ActualWidth - MovableShape.ActualWidth;
if (newY < 0)
newY = 0;
else if (newY + MovableShape.ActualHeight > CanvasImplant.ActualHeight)
newY = CanvasImplant.ActualHeight - MovableShape.ActualHeight;
Canvas.SetLeft(MovableShape, newX);
Canvas.SetTop(MovableShape, newY);
startPt = mp ;
}
}
我开发了一个 wpf 应用程序,我必须在 canvas 中移动一个矩形(最终目标是确保我的矩形不能移出 canvas)。所以我在网上搜索并尝试了很多解决方案,但我还没有工作。然后我尝试重写一个解决方案,但是没有成功。
<Grid x:Name="mainGrid">
<Canvas x:Name="CanvasImplant" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="27,27,26,0" Height="415">
<Rectangle x:Name="MovableShape" Opacity="0.85" Width="93" Height="62" HorizontalAlignment="left" VerticalAlignment="Top" Margin="930,140" Stroke="Black" StrockThickness="1" MouseDown="MovShp_MouseDown" MouseUp="MovShp_MouseUp" MouseMove="MovShp_MouseMove"/>
</Canvas>
</Grid>
private bool drag = true;
private Point StartPt;
private double newX, newY;
private void MovShp_MouseDown(object sender, MouseButtonEventArgs e)
{
drag = true;
Cursor = Cursors.Hand;
startPt = e.Getposition(CanvasImplant);
Mouse.Capture((UIElement)sender);
}
private void MovShp_MouseUp(object sender, MouseButtonEventArgs e)
{
drag = false;
Cursor = Cursors.Arrow;
Mouse.Capture(null);
}
private void MovShp_MouseMove(object sender, MouseButtonEventArgs e)
{
if (drag)
{
double deltaX = e.GetPosition(CanvasImplant).X - startPt.X;
double deltaY = e.GetPosition(CanvasImplant).Y - startPt.Y;
newX = deltaX + Canvas.GetLeft(MovableShape);
newY = deltaY + Canvas.GetTop(MovableShape);
if (newX < 0)
newX = 0;
else if (newX + MovableShape.ActualWidth > CanvasImplant.ActualWidth)
newX = CanvasImplant.ActualWidth - MovableShape.ActualWidth;
if (newY < 0)
newY = 0;
else if (newY + MovableShape.ActualHeight > CanvasImplant.ActualHeight )
newY = CanvasImplant.ActualHeight - MovableShape.ActualHeight ;
MovableShape.SetValue(Canvas.LeftProperty, newX);
MovableShape.SetValue(Canvas.TopProperty, newY);
}
}
当我点击MovableShape时,鼠标变成一只手,当我松开鼠标左键时,它变成一个箭头,但是当我拖动矩形时,它不跟随鼠标,当我释放鼠标时,它不动。
有几件事。
- 你需要在形状上设置填充,否则你点击 事件仅在非常细的边界上触发
- 鼠标移动和鼠标弹起事件需要是Canvas事件,而不是 形状
- 每次都需要将startPt设置到新的位置 移动事件处理程序
- 您需要删除形状的边距和 horizontal/vertical 位置
- 您需要使用 Canvas 而不是形状 来捕获鼠标
这对我有用:
<Grid x:Name="mainGrid" >
<Canvas x:Name="CanvasImplant" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MouseMove="MovShp_MouseMove" MouseUp="MovShp_MouseUp" Background="Transparent">
<Rectangle Canvas.Left="10" Canvas.Top="20" x:Name="MovableShape" Fill="Transparent" Opacity="0.85" Width="93" Height="62" Stroke="Black"
StrokeThickness="1" MouseDown="MovShp_MouseDown" />
</Canvas>
</Grid>
处理人员:
private void MovShp_MouseDown(object sender, MouseButtonEventArgs e)
{
drag = true;
Cursor = Cursors.Hand;
startPt = e.GetPosition(CanvasImplant);
Mouse.Capture(CanvasImplant);
}
private void MovShp_MouseUp(object sender, MouseButtonEventArgs e)
{
drag = false;
Cursor = Cursors.Arrow;
Mouse.Capture(null);
}
private void MovShp_MouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
var mp = e.GetPosition(CanvasImplant);
double deltaX = mp .X - startPt.X;
double deltaY = mp .Y - startPt.Y;
var newX = deltaX + Canvas.GetLeft(MovableShape);
var newY = deltaY + Canvas.GetTop(MovableShape);
if (newX < 0)
newX = 0;
else if (newX + MovableShape.ActualWidth > CanvasImplant.ActualWidth)
newX = CanvasImplant.ActualWidth - MovableShape.ActualWidth;
if (newY < 0)
newY = 0;
else if (newY + MovableShape.ActualHeight > CanvasImplant.ActualHeight)
newY = CanvasImplant.ActualHeight - MovableShape.ActualHeight;
Canvas.SetLeft(MovableShape, newX);
Canvas.SetTop(MovableShape, newY);
startPt = mp ;
}
}