简体   繁体   English

我正在尝试使用Mouse事件在Canvas(在WPF应用程序)中的Rectangle中移动,但是它不起作用

[英]I'm trying to move a Rectangle in a Canvas ( in a WPF app) using the Mouse event but it don't work

I develop a wpf application and I have to move a rectangle in a canvas (and the end goal is to make sure my rectangle can not get out of the canvas). 我开发了一个wpf应用程序,我必须在画布中移动一个矩形(最终目标是确保我的矩形不能脱离画布)。 So I searched and tried many solutions on the Web, but I was not working yet. 因此,我在Web上搜索并尝试了许多解决方案,但是我还没有工作。 Then I tried to rewrite a solution, but it did not work. 然后,我尝试重新编写一个解决方案,但没有成功。

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

}

When I click on MovableShape, the mouse becomes a hand and when I release the left mouse button, it becomes an arrow, but when I drag the rectangle, it does not follow the mouse and when I release the click, it do not move. 当我单击MovableShape时,鼠标变成手,而当我释放鼠标左键时,它变成了箭头,但是当我拖动矩形时,它不跟随鼠标,而当我单击时,它不动。

There are several things. 有几件事。

  1. you need to set the Fill on the shape otherwise your click events only fire on the very thin border 您需要在形状上设置“填充”,否则您的点击事件只会在非常细的边框上触发
  2. the mouse move and mouse up event needs to be the Canvas events, not the shape 鼠标移动和鼠标向上事件需要是“画布”事件,而不是形状
  3. you need to set startPt to the new position each time in the move event handler 您需要在移动事件处理程序中每次将startPt设置为新位置
  4. you need to remove the margin and horizontal/vertical position for the shape 您需要删除形状的边距和水平/垂直位置
  5. you need to capture the mouse using the Canvas not the shape 您需要使用画布而不是形状来捕获鼠标

This works for me: 这对我有用:

    <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>

And the handlers: 和处理程序:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM